Skip to content

Repository files navigation

Typing SVG

CIPython 3.11+License: Apache 2.0Code style: ruffTelegram

OCRSQLiteAiogram


🇷🇺 Русская документация:README.ru.mddocs/ru/index.md


A Telegram bot for automated invoice processing using OCR technology. The bot extracts structured data from PDF invoices and photos, allowing users to review, edit, and save invoice information to a database.

✨ Features

graph LR
A[📱 Upload] -->|PDF/Image| B[🔍 OCR Extract]
B -->|Parse Data| C[✏️ Edit Draft]
C -->|Confirm| D[💾 SQLite]
D -->|Query| E[📊 Reports]
style A fill:#4A90E2,stroke:#2c3e50,stroke-width:2px,color:#fff
style B fill:#FF6B6B,stroke:#2c3e50,stroke-width:2px,color:#fff
style C fill:#FFD93D,stroke:#2c3e50,stroke-width:2px,color:#333
style D fill:#50C878,stroke:#2c3e50,stroke-width:2px,color:#fff
style E fill:#B19CD9,stroke:#2c3e50,stroke-width:2px,color:#fff
Loading
FeatureDescriptionStatus
🤖 OCR ProcessingAutomatic extraction via Mindee API with provider abstraction
📎 Multiple FormatsPDF, JPEG, PNG, HEIC, HEIF, WebP
✏️ Interactive EditingEdit headers and line items via Telegram
💾 Data StorageSQLite with Alembic migrations
📅 Period QueriesFilter by date range and supplier
💬 Comment SystemAdd notes to invoices
📊 CSV ExportExport line items for analysis

📋 Requirements

  • 🐍 Python 3.11+
  • 🤖 Telegram Bot Token
  • 🔑 Mindee API Key

🚀 Quick Start with Docker

Tip

The fastest way to get started! Docker handles all dependencies automatically.

# 1. Clone and setup environment
git clone https://github.com/AmaLS367/InvoiceFlowBot.git
cd InvoiceFlowBot
Copy-Item .env.example .env
# 2. Edit .env with your tokens
notepad .env
# 3. Start the bot
docker-compose up --build -d
# 4. Check logs
docker-compose logs -f# 5. Stop when done
docker-compose down

💻 Installation

Note

Requires Python 3.11+ and Git installed on your system.

📦 Step-by-step installation guide

1. Clone the repository

git clone https://github.com/AmaLS367/InvoiceFlowBot.git
cd InvoiceFlowBot

2. Create a virtual environment

python -m venv .venv
.\.venv\Scripts\Activate.ps1

3. Install dependencies

pip install -e .

4. Create a .env file in the project root

BOT_TOKEN=your_telegram_bot_tokenMINDEE_API_KEY=your_mindee_api_keyMINDEE_MODEL_ID=your_mindee_model_id# Optional logging configurationLOG_LEVEL=INFOLOG_ROTATE_MB=10LOG_BACKUPS=5LOG_CONSOLE=0LOG_DIR=logs

⚙️ Configuration

The bot is configured via environment variables managed by pydantic settings in backend.config.

For local development you can create a .env file in the project root:

BOT_TOKEN=123456:ABCDEF_your_bot_tokenMINDEE_API_KEY=your-mindee-api-keyMINDEE_MODEL_ID=mindee/invoices/v4DB_FILENAME=data.sqlite

On startup the application reads these values into the Settings model.

5. Run the bot

python bot.py

[!TIP] Check logs/ directory for detailed application logs if you encounter any issues.

🧪 Tests

Run unit tests with pytest. On Windows PowerShell:

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -e .[dev]
pytest

You can also run a specific test file:

python -m pytest tests/test_invoice_service.py

🛠️ Scripts

The project includes utility scripts organized by platform in the scripts/ directory for common development tasks.

📜 Available Scripts

Python Scripts (scripts/python/)

ScriptDescription
setup.pyInstall dependencies and create .env file
migrate.pyRun database migrations
run.pyApply migrations and start the bot
test.pyRun tests with coverage
lint.pyRun code linting (ruff + mypy)
format.pyFormat code with ruff

Platform Wrappers

Linux/macOS (scripts/linux/):

./scripts/linux/setup.sh
./scripts/linux/migrate.sh
./scripts/linux/run.sh
./scripts/linux/test.sh
./scripts/linux/lint.sh
./scripts/linux/format.sh

Windows (scripts/windows/):

.\scripts\windows\setup.ps1
.\scripts\windows\migrate.ps1
.\scripts\windows\run.ps1
.\scripts\windows\test.ps1
.\scripts\windows\lint.ps1
.\scripts\windows\format.ps1

[!TIP] See Scripts Guide for detailed documentation on all available scripts.

📖 Usage

Tip

New to the bot? Start with /start to see the interactive menu!

🎯 Basic Commands
CommandDescription
/startStart the bot and see main menu
/helpShow help message
/showDisplay current draft invoice
/saveSave current draft to database
✏️ Editing Commands
CommandDescriptionExample
/editEdit header fields/edit supplier=ACME client=Corp date=2024-01-15
/edititemEdit specific line item/edititem 0 name=Widget qty=5 price=10.50
/commentAdd a comment/comment Approved by manager
🔍 Query Commands
/invoices YYYY-MM-DD YYYY-MM-DD [supplier=text]

Example:

/invoices 2024-01-01 2024-01-31 supplier=ACME
🔘 Interactive Buttons

The bot provides inline keyboard buttons for:

  • 📤 Upload invoice
  • ✏️ Edit invoice fields
  • 💬 Add comments
  • 💾 Save invoice
  • 📅 Query invoices by period
  • ❓ View help

📊 Project Stats

Top LanguageCode SizeLast Commit

📁 Project Structure

InvoiceFlowBot/
├── bot.py # Main bot entry point
├── scripts/ # Utility scripts
│ ├── python/ # Core Python scripts
│ ├── linux/ # Linux shell wrappers
│ └── windows/ # Windows PowerShell wrappers
├── backend/
│ ├── config.py # Configuration management
│ ├── domain/
│ │ └── invoices.py # Domain entities (Invoice, InvoiceHeader, InvoiceItem, etc.)
│ ├── services/
│ │ └── invoice_service.py # Service layer (OCR orchestration, domain conversion)
│ ├── handlers/
│ │ ├── commands.py # Text command handlers (/show, /edit, /invoices, etc.)
│ │ ├── callbacks.py # Callback query handlers (inline button actions)
│ │ ├── file.py # File upload handlers
│ │ ├── fsm.py # Global state management
│ │ └── utils.py # Utility functions and keyboards
│ ├── ocr/
│ │ ├── extract.py # Invoice extraction entry point
│ │ ├── mindee_client.py # Mindee API integration
│ │ ├── providers/ # OCR provider abstraction layer
│ │ │ ├── base.py # OcrProvider interface
│ │ │ └── mindee_provider.py # Mindee provider implementation
│ │ └── engine/
│ │ ├── router.py # OCR routing logic (uses providers)
│ │ ├── types.py # Data type definitions
│ │ └── util.py # OCR utilities and logging
│ ├── storage/
│ │ └── db.py # Database operations
│ └── alembic/ # Database migrations

⚙️ Configuration

The bot uses environment variables for configuration. See .env.example for available options.

📋 Environment Variables Reference

🔑 Required Variables

VariableDescriptionExample
BOT_TOKENTelegram bot token from @BotFather123456:ABCDEF...
MINDEE_API_KEYAPI key from Mindee platformyour-api-key
MINDEE_MODEL_IDMindee model ID for invoice processingmindee/invoices/v4

[!WARNING] The bot will not start without these required variables!

🔧 Optional Variables

VariableDescriptionDefault
LOG_LEVELLogging levelINFO
LOG_ROTATE_MBMax log file size in MB10
LOG_BACKUPSNumber of backup log files5
LOG_CONSOLEEnable console logging0
LOG_DIRCustom log directorylogs

🗄️ Database

The bot uses SQLite database to store invoices. The database schema is managed by Alembic.

🔨 Database Setup & Structure

Initial Setup

Run migrations from the project root so they apply to the same database the bot uses (e.g. backend/data.sqlite):

python -m alembic -c backend/alembic.ini upgrade head

Or: python scripts/python/migrate.py (see Database docs for details).

[!NOTE] The application also runs migrations on startup via backend.storage.db.init_db(). Running them once manually from the project root is recommended to avoid path issues (e.g. on Windows).

Database Tables

TableDescription
invoicesHeader information (supplier, client, dates, totals)
invoice_itemsLine items for each invoice
commentsUser comments associated with invoices
invoice_draftsTemporary drafts for editing

Backup & Restore

# BackupCopy-Item .\data.sqlite .\backup\data-$(Get-Date-Format yyyyMMddHHmmss).sqlite
# RestoreCopy-Item .\backup\data-20240115.sqlite .\data.sqlite

[!WARNING] Always backup data.sqlite before major updates!

📝 Logging

Logs are written to the logs/ directory by default:

  • ocr_engine.log - General application logs
  • errors.log - Error and warning logs
  • router.log - OCR routing logs
  • extract.log - Invoice extraction logs

📚 Documentation

📸 Screenshots

📄 License

Copyright 2025 Ama

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

👨‍💻 Development

🔍 Code Quality

The project uses the following tools for code quality:

  • ruff - Fast Python linter
  • mypy - Static type checking

🛠️ Local Development Setup

# Install dependencies
pip install -e .
pip install -e .[dev]
# Run linter
python -m ruff check .
# Run type checker
python -m mypy backend/# Run tests
python -m pytest

The CI pipeline automatically runs ruff, mypy, and pytest on every push and pull request.

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

💬 Support

For issues and questions, please open an issue on the repository.


🌟 Star History

Star History Chart

Made with ❤️ by Ama

About

Telegram bot for automated invoice processing using OCR. Extract, edit, and store invoice data from PDFs and images.

Topics

Resources

Contributing

Security policy

Stars

1 star

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages