← back to work
Automation2025

Accounting & Finance
Invoice Agent

Send any invoice — photo or PDF — to a Telegram bot and get back a confirmation within seconds. The system OCRs the file, extracts every field with AI, logs it to Google Sheets, and archives the original to Google Drive automatically.

Stages
3
File Types
JPG / PNG / PDF
LLM
GPT-4.1-mini
Fields Extracted
15+
Interface
Telegram

// the problem

Invoice processing is still mostly manual

Accounting teams receive invoices via email, WhatsApp, and Telegram — in photos, PDFs, and scans. Someone has to open each one, read the totals, re-type the data into a spreadsheet, and file the original somewhere. This workflow replaces all of that with a single Telegram message. The user sends the invoice; the system handles everything else.

// workflow diagram

Three stages at a glance

STAGE 1 — RECEIVE & DETECTTelegramTriggerDownloadInvoice FileDetectFile TypeRoute JPG /PNG / PDFSTAGE 2 — OCR & AI EXTRACTIONOCR.spaceText ExtractionGPT-4.1-miniStructured ExtractValidateJSON SchemaFormat Fieldsfor SheetsSTAGE 3 — SAVE, ARCHIVE & CONFIRMSave toGoogle SheetsArchive toGoogle DriveWrite Confirm-ation MessageReply onTelegramReceive & detectOCR & AI extractSave & confirm

// stage 1 — receive & detect

From Telegram message to classified file

01
Telegram Trigger
The n8n bot listens for incoming messages. Accepts both photo and document (PDF) messages.
02
Download Invoice File
Fetches the file from Telegram's servers using the file ID. Supports the highest-resolution photo or the raw document.
03
Detect File Type
JavaScript inspects the MIME type and classifies the file as JPG, PNG, or PDF.
04
Route by File Type
Switch node routes all three supported formats to the OCR step. Unsupported types are filtered out early.

// stage 2 — ocr & ai extraction

From raw file to structured data

01
OCR.space Text Extraction
Sends the binary file to OCR.space Engine 2 with the detected file type. Returns raw parsed text from the invoice.
02
GPT-4.1-mini Structured Extract
LLM agent receives the OCR text and extracts structured fields: invoice number, dates, vendor, customer, currency, subtotal, tax, total, and all line items.
03
Validate JSON Schema
Structured Output Parser enforces the schema — ensures the AI response is always a predictable, typed JSON object before it reaches Sheets.
04
Format Fields for Sheets
JavaScript flattens nested line items into separate columns (descriptions, quantities, unit prices, amounts) and sets null defaults for missing fields.

// stage 3 — save, archive & confirm

From structured data to filed and confirmed

01
Save to Google Sheets
All extracted invoice fields are appended as a new row in the designated spreadsheet.
02
Merge with Original File
The structured data is merged with the original binary file so both are available for the next steps.
03
Upload to Google Drive
The original invoice is archived with an auto-generated name: Invoice [number] [date | time].
04
Write Confirmation Message
GPT-4.1-mini generates a short, clear Telegram message summarising the key invoice details and confirming storage.
05
Send Confirmation to Telegram
The confirmation message is sent back to the user in the same Telegram chat.

// technical stack

Tools & technologies

n8n
Workflow orchestration across all three stages
Telegram Bot
User-facing interface — receives invoice photos and PDFs, sends back confirmation
OCR.space
Extracts raw text from JPG, PNG, and PDF invoice files (Engine 2)
GPT-4.1-mini
Converts OCR text into structured invoice fields and writes the confirmation message
Google Sheets
Stores every extracted invoice as a structured row with all fields
Google Drive
Archives the original invoice file with an auto-generated timestamped name

// engineering decisions

Why it was built this way

OCR.space Engine 2
Engine 2 is more accurate than Engine 1 for structured documents like invoices — handles columns, tables, and mixed fonts better.
GPT-4.1-mini over a larger model
Structured extraction from clean OCR text is a well-defined task that doesn't require a frontier model. GPT-4.1-mini is fast, cheap, and accurate enough for this use case.
Structured Output Parser
Guarantees a typed, predictable JSON object. Without it, the LLM could return valid-looking but incorrectly formatted data that would silently corrupt the sheet.
Separate extraction and confirmation agents
The same GPT-4.1-mini model handles both tasks but through separate agent nodes — keeping prompts focused and outputs clean.
Auto-named Google Drive archive
Files are named with invoice number, date, and time — making the archive self-organising and searchable without any manual renaming.

Need invoice automation for your team?

Let's build a pipeline tailored to your document types and accounting stack.

Get in touch →← Back to work