Automated Telegram bot for extracting, editing, and storing invoices with local OCR
English · Русский · Documentation · Screenshots
Watch the demo with sound (MP4, 1600×1200) · Browse screenshots
InvoiceFlowBot accepts PDF invoices and photos in Telegram, extracts structured headers and line items locally with Docling and Tesseract (or Mindee as an optional backend), lets users review and edit drafts inline, and saves verified invoices to SQLite for period-based reporting. The bot can send long OCR item tables as CSV files.
graph LR
A[Upload PDF / Photo] --> B[Local OCR]
B --> C[Review & Edit Draft]
C --> D[Save to SQLite]
B --> F[CSV for Long Item Tables]
D --> E[Period Reports]
| Feature | Description |
|---|---|
| Local OCR by Default | Runs Docling + Tesseract on your own server with no per-document API fees (optional Mindee provider included). |
| Multiple Input Formats | Supports PDF, JPEG, PNG, and WebP. Convert HEIC/HEIF images to JPEG or PNG before uploading. |
| Multilingual Interface (7 Languages) | Auto-detects the user's Telegram language, persists choices in SQLite, and localizes Telegram command menus. |
| Interactive Draft Review | Edit supplier, customer, date, invoice number, totals, and individual line items via inline buttons or commands. |
| Automatic Sanity Checks | Flags uncertain OCR values in the draft and records a comment if the item totals differ from the invoice total at save time. |
| SQLite + Alembic Migrations | Stores invoices, line items, comments, drafts, and user language preferences with versioned schema migrations. |
| Period Queries & CSV Export | Filter saved invoices by date range and supplier. Long OCR item tables can arrive as UTF-8 CSV files before saving. |
The bot interface, inline keyboards, and Telegram command menu support 7 languages out of the box:
| Code | Language | Catalog |
|---|---|---|
ru |
Русский (default fallback) | ru.json |
en |
English | en.json |
de |
Deutsch | de.json |
es |
Español | es.json |
fr |
Français | fr.json |
it |
Italiano | it.json |
pt |
Português | pt.json |
Users can switch languages via /language or 🌐 Language. In personal chats, the bot also updates that chat’s command menu. Adding another language only requires a new ISO 639-1 JSON file in backend/handlers/locales/ and registering it in LANGUAGES in backend/handlers/texts.py — see the development guide.
Tip
Docker includes Python, Tesseract language packs, and all runtime dependencies out of the box.
# 1. Clone the repository and prepare .env
git clone https://github.com/AmaLS367/InvoiceFlowBot.git
cd InvoiceFlowBot
if [ ! -e .env ]; then cp .env.example .env; fi
# 2. Set BOT_TOKEN in .env and prepare the database bind mount
if [ ! -e ./data.sqlite ]; then touch ./data.sqlite; fi
# 3. Start only when the database path is a regular file
test -f ./data.sqlite && mkdir -p ./logs && docker compose up --build -d
# 4. Follow logs
docker compose logs -f bot
# 5. Stop
docker compose downThe database file and logs/ must be writable by the container user. If data.sqlite is a directory, see the Docker guide.
Requires Python 3.11+, Git, and Tesseract (with language data matching OCR_LANGUAGES, e.g., eng+rus).
Step-by-step local setup
-
Clone and create a virtual environment:
git clone https://github.com/AmaLS367/InvoiceFlowBot.git cd InvoiceFlowBot python -m venv .venv # Linux/macOS: source .venv/bin/activate # Windows PowerShell: # .\.venv\Scripts\Activate.ps1
-
Install dependencies:
pip install -e . -
Create
.envfrom.env.example:BOT_TOKEN=123456:ABCDEF_your_bot_token OCR_PROVIDER=local OCR_LANGUAGES=eng+rus
With
OCR_PROVIDER=local, documents are processed locally without external OCR credentials. The first OCR request downloads the Docling models. To use Mindee instead, setOCR_PROVIDER=mindee,MINDEE_API_KEY, andMINDEE_MODEL_ID. See the local setup guide. -
Apply migrations and run the bot:
python -m alembic -c backend/alembic.ini upgrade head python bot.py
On the first /start, choose a language to open the main menu. Once your choice is saved, /start opens the main menu directly.
| Command | Description | Example |
|---|---|---|
/start |
Choose a language on first use or open the main menu | /start |
/help |
Show quick help and available commands | /help |
/language |
Switch interface language and command menu | /language |
/show |
Display the current invoice draft | /show |
/save |
Save the current draft to SQLite | /save |
/edit |
Batch-edit header fields | /edit supplier=ACME; client=Corp; date=2025-01-15 |
/edititem |
Edit a specific line item (1-based index) | /edititem 1 name=Widget; qty=5; price=10.50 |
/comment |
Append a comment to the active draft | /comment Approved by manager |
/invoices |
Query saved invoices by date and supplier | /invoices 2025-01-01 2025-01-31 supplier=ACME |
Inline buttons mirror the main workflow: upload invoice, edit header or line items, add comments, save draft, query by period, view help, and switch language.
Environment variables reference
| Variable | Description | Example |
|---|---|---|
BOT_TOKEN |
Telegram bot token from @BotFather |
123456:ABCDEF... |
| Variable | Description | Default |
|---|---|---|
OCR_PROVIDER |
OCR backend (local or mindee) |
local |
OCR_LANGUAGES |
Tesseract language codes joined by + |
eng+rus |
MINDEE_API_KEY |
Mindee API key (when OCR_PROVIDER=mindee) |
empty |
MINDEE_MODEL_ID |
Mindee model ID (when OCR_PROVIDER=mindee) |
empty |
LOG_LEVEL |
Logging level | INFO |
LOG_ROTATE_MB |
Max log file size in MB | 10 |
LOG_BACKUPS |
Number of rotated log backups | 5 |
LOG_CONSOLE |
Enable console logging (0 or 1) |
0 |
LOG_DIR |
Directory for log files | logs/ in the project root |
INVOICE_DB_PATH |
SQLite file path relative to backend/ or absolute |
../data.sqlite |
SQLite schema is managed with Alembic (backend/alembic/versions/).
Tables & migrations
| Table | Description |
|---|---|
invoices |
Saved invoice headers (supplier, customer, date, number, totals) |
invoice_items |
Line items linked to each invoice |
comments |
User and automatic review comments |
invoice_drafts |
Active per-user drafts prior to saving |
user_languages |
Saved per-user interface language preference |
Run migrations from the project root:
python -m alembic -c backend/alembic.ini upgrade headSee Database Guide for backup, restore, and schema details.
InvoiceFlowBot/
├── bot.py # Application entry point
├── scripts/ # Cross-platform dev/ops scripts (python, linux, windows)
├── backend/
│ ├── config.py # Pydantic environment settings
│ ├── core/container.py # Dependency injection container
│ ├── domain/ # Domain models (invoices, drafts)
│ ├── handlers/
│ │ ├── commands*.py # Command handlers (/start, /show, /edit, /invoices, ...)
│ │ ├── callbacks*.py # Inline keyboard handlers
│ │ ├── file.py # PDF & image upload handlers
│ │ ├── language.py # Language picker & Telegram menu localization
│ │ ├── locale_middleware.py # Per-update locale resolution
│ │ ├── texts.py # Locale registry & text formatting helpers
│ │ └── locales/ # JSON translation catalogs (de, en, es, fr, it, pt, ru)
│ ├── ocr/
│ │ ├── async_client.py # OCR entry point used for Telegram uploads
│ │ ├── extract.py # Synchronous OCR helpers
│ │ ├── engine/ # OCR router, types, and utilities
│ │ └── providers/ # Local (Docling + Tesseract) and Mindee providers
│ ├── services/ # Invoice & draft application services
│ ├── storage/ # Async SQLite repositories & user language storage
│ └── alembic/ # Alembic schema migrations
└── docs/ # English & Russian documentation and screenshots
pip install -e .[dev]
python -m ruff check .
python -m mypy backend/
BOT_TOKEN=test-token python -m pytestHelper wrappers for setup, migrations, linting, formatting, and tests are also available in scripts/python/, scripts/linux/, and scripts/windows/ (see Scripts Guide).
- English: docs/en/index.md · Screenshots · Development
- Русский: README.ru.md · docs/ru/index.md · Скриншоты
Licensed under the Apache License 2.0. Copyright 2025 Ama.