This guide explains how to set up and use the WhatsApp integration for automatic expense tracking in Kronyx.
Users can send transaction receipts (images or PDFs) via WhatsApp, and the system will:
- Download the media from Twilio
- Extract transaction details using OCR (Google Gemini AI)
- Find the user by phone number
- Add the transaction to their account automatically
- Send a confirmation message with transaction details
Your webhook URL is: https://15f85677a64e31.lhr.life/twilio/whatsapp/webhook
- Go to Twilio WhatsApp Sandbox
- In "WHEN A MESSAGE COMES IN" field, paste:
https://15f85677a64e31.lhr.life/twilio/whatsapp/webhook - Set method to: POST
- Click Save
Users must have their WhatsApp phone numbers registered in the database:
# List all users
python update_user_phone.py list
# Update user phone number (use international format with +)
python update_user_phone.py <user_id> <phone_number>
# Example for Indian number:
python update_user_phone.py 1 +919482698406Important: Phone numbers must be in international format starting with + (e.g., +919482698406 for India)
Users need to join the Twilio sandbox:
- Send the join code from Twilio Sandbox page to
+1 415 523 8886 - Typically something like:
join <your-code>
Users can send these commands via WhatsApp:
help- Show welcome message and instructionsbalance- Check current account balancerecent- View last 5 transactions
- Take a photo of your transaction receipt or bill
- Send it via WhatsApp to the Twilio number
- Wait 10-20 seconds for OCR processing
- Receive confirmation with extracted transaction details
The system automatically extracts:
- 💰 Amount
- 🏪 Merchant name
- 📅 Transaction date/time
- 📊 Transaction type (debit/credit)
- 🏷️ Category (if identifiable)
- 🔖 Transaction ID
- 🏦 Bank name
- 💳 Account number
User: [Sends receipt image via WhatsApp]
Bot: 📄 Receipt Received!
🔍 Processing your transaction...
This will take 10-20 seconds.
[After OCR processing]
Bot: ✅ Transaction Added Successfully!
💰 Amount: ₹2,500.00
🏪 Merchant: Amazon
📅 Date: 13 Dec 2025, 02:30 PM
📊 Type: DEBIT
🏷️ Category: Shopping
🔖 Txn ID: TXN123456789
💡 Your balance: ₹45,250.00
📸 Send another receipt to track more expenses!
Method: POST
Form Parameters:
From- WhatsApp number (format:whatsapp:+919482698406)Body- Text message (optional)NumMedia- Number of media attachmentsMediaUrl0- URL of first media attachmentMediaContentType0- MIME type of media
- Images: JPEG, PNG
- Documents: PDF
The system uses Google Gemini AI (via pydantic-ai) to:
- Extract text from images using vision capabilities
- Parse transaction details intelligently
- Structure data into transaction schema
- Validate extracted information
Transactions are stored in the transactions table linked to users via:
- User lookup by phone number
- Automatic user_id assignment
- Transaction timestamp recording
- All extracted fields preserved
- Matches WhatsApp sender with database user by phone number
- Shows error if phone number not registered
- Prevents unauthorized transaction creation
- Powered by Google Gemini 2.5 Flash
- Extracts multiple transaction fields
- Handles both images and PDFs
- Works with various receipt formats
- Immediate acknowledgment of receipt
- Processing status updates
- Detailed confirmation with extracted data
- Error messages with helpful suggestions
- Check balance instantly
- View recent transactions
- Get help and instructions
- All via WhatsApp chat
- Phone Number Validation: Only registered phone numbers can add transactions
- Twilio Authentication: Media downloads require Twilio credentials
- Request Validation: Can enable Twilio signature validation (currently commented out)
To enable request validation, uncomment in twilio_webhook.py:
signature = request.headers.get('X-Twilio-Signature', '')
url = str(request.url)
if not validator.validate(url, data, signature):
raise HTTPException(status_code=403, detail="Invalid Twilio signature")- Verify phone number is registered in database
- Check phone number format (must include + and country code)
- Run
python update_user_phone.py listto see all users
- Ensure good image quality and lighting
- Try sending as PDF instead of image
- Check if receipt has clear, readable text
- Verify Gemini API key is configured
- Verify tunnel is running (
ssh -R 80:localhost:8000 nokey@localhost.run) - Check webhook URL in Twilio console
- Ensure server is running (
docker ps) - Check logs:
docker logs kronyx-api
View logs for debugging:
# API logs
docker logs kronyx-api -f
# Check for webhook calls
docker logs kronyx-api | grep "Received WhatsApp message"
# Check OCR processing
docker logs kronyx-api | grep "OCR extracted"If you make changes to the webhook code:
# Restart API container
docker restart kronyx-api
# Restart all services
cd /media/D/programming/projects/kronyx/server
python run.py stop
python run.pyEnsure these are set in your .env file:
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=xxxxxxxxxxxxxxxxx
TWILIO_WHATSAPP_FROM=whatsapp:+14155238886
GEMINI_API_KEY=AIzaSyxxxxxxxxxxxxxxxxxYour WhatsApp expense tracker is now ready! Users can start sending receipts and tracking expenses automatically.
For support, contact the development team or check the logs for detailed error messages.