🤖 AI Invoice Extraction Agent
Turn your messy invoice images into structured data and organized files automatically! 🚀
📑 Table of Contents
- 🌟 Overview
- ✨ Key Features
- 🛠️ Architecture & Workflow
- 📋 Prerequisites
- ⚙️ Installation & Setup
- 🚀 Usage Guide
- 💾 Data Structure & Customization
- 🔧 Troubleshooting & FAQ
- 🤝 Contributing
- 📄 License
🌟 Overview
The AI Invoice Extraction Agent is a state-of-the-art, no-code automation workflow built on n8n. It acts as your personal finance assistant, living right inside your Telegram messenger.
Imagine this: You're at a lunch meeting, you pay the bill, and instead of stuffing the receipt in your wallet to be forgotten, you simply snap a photo and send it to your Telegram bot. Done.
This workflow kicks into gear, automatically extracting all the crucial details (Invoice Number, Date, Amount, etc.), saving the data to a Google Sheet, archiving the original file to Google Drive, and even replying with a friendly, AI-generated summary.
Say goodbye to manual data entry and lost receipts! 👋
✨ Key Features
| Feature | Description | Tech Stack |
|---|---|---|
| 📸 Instant OCR | Instantly converts images into machine-readable text using advanced optical character recognition. | OCR.space API |
| 🧠 Intelligent Parsing | Uses regex and logic to identify specific fields like Total Amount, Due Date, and Billing Address with high precision. | Javascript |
| 🗄️ Auto-Database | Automatically appends structured data to Google Sheets for easy tracking, accounting, and visualization. | Google Sheets |
| ☁️ Cloud Archiving | Uploads the original image file to a designated Google Drive folder for audit trails and safekeeping. | Google Drive |
| 💬 AI Assistant | Powered by OpenAI, the bot understands context and replies with a natural language summary, making interactions feel human. | OpenAI GPT-4 |
| 📱 Mobile First | Works entirely within Telegram, making it accessible from anywhere, anytime—no new apps to install. | Telegram API |
🛠️ Architecture & Workflow
Here is a visual deep-dive into how the automation flows from start to finish:
Step-by-Step Process
- Trigger: You send an image (invoice) to your Telegram Bot.
- Download: The workflow downloads the image file from Telegram's servers to the n8n instance.
- OCR Analysis: The image is sent to the OCR.space API, which returns the raw text found in the document.
- Data Extraction: A custom code node parses the raw text to find key information (Invoice #, Date, Amount, etc.).
- Database Update: The extracted data is added as a new row in your Google Sheet.
- File Storage: The original image is uploaded to a specific folder in your Google Drive.
- AI Processing: An AI Agent (LangChain) reviews the extracted info and constructs a helpful response.
- Response: The bot replies to you on Telegram with a summary and a link to the database.
📋 Prerequisites
Before you begin, ensure you have the following:
- n8n Instance: You need n8n installed and running (Self-hosted or Cloud).
- Telegram Account: To create a bot.
- Google Account: For Sheets and Drive access.
- OCR.space API Key: Free key available here.
- OpenAI API Key: For the AI agent functionality.
⚙️ Installation & Setup
Follow these detailed steps to get your agent up and running.
1. Telegram Bot Setup
- Open Telegram and search for @BotFather.
- Send the command
/newbot. - Follow the prompts to name your bot and get your HTTP API Token.
- Save this token; you will need it for n8n credentials.
2. Google Cloud Setup
- Google Cloud Console: Create a new project and enable Google Sheets API and Google Drive API.
- Credentials: Create OAuth2 credentials (Client ID and Secret).
- Note: For n8n, you'll need to set the redirect URI to your n8n instance's callback URL.
- Google Sheet: Create a new Google Sheet.
- Name the first sheet
Sheet1. - Create the following headers in the first row:
Invoice NumberDateTotal Amount ($)Billing AddressDue DateNotes
- Name the first sheet
- Google Drive: Create a folder named
Invoices(or whatever you prefer) to store the images.
3. OCR.space Setup
- Visit OCR.space.
- Register for a free API key.
- You will receive the key via email.
4. OpenAI Setup
- Go to platform.openai.com.
- Generate a new API Key.
- Ensure you have credits/billing set up.
5. n8n Configuration
- Import Workflow:
- Copy the JSON content of the workflow file.
- In n8n, go to Workflows > Import from JSON and paste the code.
- Configure Credentials:
- Telegram Trigger: Create new credentials using your Bot Token.
- Google Sheets: Connect your Google account using OAuth2.
- Google Drive: Connect the same Google account.
- OpenAI: Add your OpenAI API Key.
- Update Node Settings:
- Update Database Node: Select your specific Google Sheet and Sheet Name.
- Add Invoice Image to Drive Node: Select your specific Google Drive folder.
- Analyze Image Node: In the
Header Parameters, replaceYOUR API KEY HEREwith your actual OCR.space API key.
- Activate: Toggle the workflow to Active.
🚀 Usage Guide
Using the agent is incredibly simple:
- Open your Telegram app.
- Navigate to your new bot.
- Upload a photo of an invoice or forward a file (PDF/Image) to the bot.
- Wait a few seconds... ⏳
- Ding! 🔔 You will receive a reply confirming the data extraction, along with a link to your dashboard.
Tip: Ensure the image is clear and the text is legible for the best OCR results.
💾 Data Structure & Customization
The system is designed to extract the following fields. You can modify the Parse Text node (Javascript) to add more fields if needed.
| Field | Regex Logic | Description |
|---|---|---|
Invoice Number | Invoice Number:\s*(\S+) | Captures the ID of the invoice. |
Date | Date:\s*(\S+) | Captures the invoice date. |
Total Amount | Total Amount:\s*([\d,.]+) | Captures the final total. |
Billing Address | Billing Address:\s*(.+) | Captures the address line. |
Due Date | Due Date:\s*(\S+) | Captures when payment is due. |
Notes | Notes:\s*([\s\S]*) | Captures any additional text/notes. |
Adding New Fields
To add a new field (e.g., "Tax Amount"):
- Open the Parse Text node in n8n.
- Add a new line to the
parsedDataobject:javascripttaxAmount: text.match(/Tax:\s*([\d,.]+)/)?.[1], - Add a new column to your Google Sheet.
- Update the Update Database node to map the new field.
🔧 Troubleshooting & FAQ
❌ The bot isn't responding!
- Check if the workflow is Active in n8n.
- Verify your Telegram credentials are correct.
- Check the n8n Executions tab for any error logs.
❌ OCR is missing data!
- The image might be blurry. Try a higher quality image.
- The regex patterns in the "Parse Text" node might need adjustment to match your specific invoice format.
❌ Google Drive/Sheets errors!
- Ensure your OAuth2 credentials haven't expired.
- Verify that the specific Sheet ID and Folder ID in the nodes match your actual files.
❓ Can I use a different OCR provider?
- Yes! You can replace the OCR.space node with Google Cloud Vision, AWS Textract, or any other OCR service supported by n8n.
🤝 Contributing
Contributions are welcome! If you have ideas for improvements, feel free to fork this repository and submit a pull request.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📄 License
Distributed under the MIT License. See LICENSE for more information.
Made with ❤️ by the Ai agent bros team
Website • Documentation • Support