A complete production-ready chatbot system that processes natural language orders via Telegram and provides an admin dashboard for order management.
- Natural Language Processing: Customers send plain English messages via Telegram
- Intelligent Order Extraction: Uses Google Gemini AI with regex-based MCP fallback for reliability
- Bank Reference Extraction: Automatically extracts bank payment references from customer messages
- Instant Confirmation: Formatted order confirmation with order ID and total
- Error Handling: Clear guidance when order details are missing
- Real-time Dashboard: View all orders with formatted order forms
- Stock Reference: Quick view of available products and prices
- One-Click Actions: Verify payments, add waybills, mark as delivered
- Status Tracking: Clear order progression from pending โ delivered
- Auto-refresh: Dashboard updates every 30 seconds
- Bank Reference Visibility: Shows full bank references for easy payment verification
- Order Amount Display: Shows order amounts in the order list table
- 99%+ Reliability: Regex-based fallback ensures no orders are lost
- Pluggable Architecture: Swap LLM providers, add channels easily
- Production Ready: Docker containerized with health checks
- Real-time Updates: Webhook processing with async architecture
- Complete Audit Trail: Track every order change with timestamps
- Docker & Docker Compose
- Telegram Bot Token (from @BotFather)
- Google Gemini API Key (from Google AI Studio)
git clone <repository>
cd nlp-order-chatbot# Copy environment template
cp .env.example .env
# Edit .env with your API keys
nano .envRequired environment variables:
TELEGRAM_TOKEN=your_telegram_bot_token_here
GEMINI_API_KEY=your_gemini_api_key_here# Run the setup script
./scripts/setup.sh
# This will:
# - Build all Docker containers
# - Start PostgreSQL database
# - Launch MCP tools service
# - Start FastAPI backend
# - Launch admin dashboard
# - Seed the database with products# Configure webhook for your bot
./scripts/setup_webhook.sh# Run comprehensive system tests
./scripts/test_system.shCustomer sends:
"Hi, I'm Sarah Johnson, I want 1 case, phone 0821234567, bank ref :fnb1234, Pick n Pay Rosebank, Johannesburg, Gauteng"
Bot responds:
โ
Order received!
Most simple order form for team SA
*Order form*
Names: Sarah Johnson
Order (ID): OR1700123456
Stock to collect: 1x 24 bottles
Cell nr: 0821234567
*Pudo Locker name:* Pick n Pay Rosebank
*Your suburb:* Johannesburg
Gauteng
Total: R180
Please transfer R180 to our banking details and send us the reference number.
- View Orders: See all incoming orders with formatted forms and bank references
- Verify Payment: Click "โ Verify Payment", enter or confirm bank reference
- Add Waybill: Click "๐ฆ Add Waybill", enter Pudo tracking number
- Mark Delivered: Click "โ Mark Delivered" after drop-off
graph TD
A[Telegram User] -->|Messages| B[Telegram API]
B --> C[FastAPI Backend]
C --> D[(PostgreSQL Database)]
C --> E[LLM Service - Gemini]
E -->|Fallback| F[MCP Tools Service]
F --> D
C --> G[Admin Dashboard]
H[Admin User] --> G
G --> C
style A fill:#4285F4,stroke:#333
style B fill:#34A853,stroke:#333
style C fill:#FBBC05,stroke:#333
style D fill:#EA4335,stroke:#333
style E fill:#8E44AD,stroke:#333
style F fill:#2C3E50,stroke:#333
style G fill:#E67E22,stroke:#333
style H fill:#9B59B6,stroke:#333
-
Telegram Integration Layer
- Receives messages via webhook
- Processes natural language orders
- Sends formatted confirmations
-
FastAPI Backend Service
- Main API server (port 8000)
- Order processing and validation
- Admin endpoints for order management
- Health checks and monitoring
-
LLM Service with Fallback
- Primary: Google Gemini AI for order extraction
- Fallback: Regex-based MCP tools for reliability
- Product catalog management
-
MCP Tools Service
- Lightweight regex-based extraction
- Product database access
- Order validation utilities
- Fallback when LLM fails
-
PostgreSQL Database
- Stores orders, customers, products
- Order status tracking
- Payment and fulfillment data
-
Admin Dashboard
- Next.js React application (port 3000)
- Real-time order monitoring
- Payment verification interface
- Fulfillment management
-
Order Creation:
- Customer sends Telegram message
- FastAPI receives webhook
- LLM Service extracts order details
- Fallback to MCP Tools if needed
- Order saved to PostgreSQL
- Confirmation sent to customer
-
Order Management:
- Admin views orders in dashboard
- Payment verified with bank reference
- Waybill added for fulfillment
- Order marked as delivered
- Customer notifications sent
| Product | Price | Aliases |
|---|---|---|
| 24 Bottles Case | R180 | "1 case", "24 bottles", "case" |
| 36 Bottles | R220 | "36 bottles" |
| 20 Sprays | R150 | "20 sprays", "20 spray bottles" |
| 24 Bottles + 20 Sprays | R180 | "24 bottles plus 20 sprays" |
| 48 Bottles | R360 | "48 bottles", "2 cases" |
nlp-order-chatbot/
โโโ backend/ # FastAPI application
โ โโโ main.py # Main API server
โ โโโ models.py # Database models
โ โโโ telegram_handler.py # Telegram integration
โ โโโ llm_service.py # Gemini + MCP integration
โ โโโ admin_api.py # Admin endpoints
โ โโโ database.py # DB connection & utilities
โโโ mcp-tools/ # Fallback extraction service
โ โโโ simple_tools.py # Regex-based extraction
โโโ admin-dashboard/ # Next.js admin interface
โ โโโ pages/index.tsx # Main dashboard page
โโโ scripts/ # Utility scripts
โโโ docker-compose.yml # Service orchestration
โโโ README.md
# Start services in development mode
docker-compose up --build
# View logs
docker-compose logs -f backend
docker-compose logs -f mcp-tools
# Access services
# Admin Dashboard: http://localhost:3000
# API Documentation: http://localhost:8000/docs
# API Health: http://localhost:8000/health
# MCP Tools: http://localhost:8080/health# Connect to PostgreSQL
docker-compose exec postgres psql -U orderuser -d orders
# View orders
SELECT order_display_id, customer_name, status, total_amount, payment_reference FROM orders;
# View products
SELECT sku, name, price FROM products;GET /admin/orders- List all orders with bank references and amountsPOST /admin/orders/{id}/verify-payment- Verify payment with bank referencePOST /admin/orders/{id}/waybill- Add waybill numberPOST /admin/orders/{id}/mark-delivered- Mark as deliveredGET /admin/dashboard/stats- Dashboard statisticsGET /admin/products- List available products
GET /health- System health checkPOST /webhook/telegram- Telegram webhookPOST /admin/test-extraction- Test order extractionGET /admin/mcp-status- MCP service status
# Production database
DATABASE_URL=postgresql://user:pass@prod-db:5432/orders
# Production webhook
TELEGRAM_WEBHOOK_URL=https://yourdomain.com/webhook/telegram
# CORS configuration
CORS_ORIGINS=https://yourdomain.com,https://admin.yourdomain.com
# SSL/Security
SSL_VERIFY=true
SECRET_KEY=your-secret-key-hereAll services include comprehensive health checks:
- Database connectivity
- API responsiveness
- MCP tools availability
- LLM service status
Built-in endpoints for monitoring:
/health- Overall system health/admin/dashboard/stats- Business metrics/admin/mcp-status- Fallback service status
Services not starting:
# Check logs
docker-compose logs
# Rebuild containers
docker-compose down && docker-compose up --buildDatabase connection errors:
# Reset database
docker-compose down -v
docker-compose up postgresTelegram webhook issues:
# Remove and reset webhook
curl -X POST "https://api.telegram.org/bot<TOKEN>/deleteWebhook"
./scripts/setup_webhook.shOrder extraction failures:
- Check Gemini API key is valid
- Verify MCP tools service is running
- Test with
/admin/test-extractionendpoint
If bank references are not being extracted from customer messages:
-
Check the message format: Ensure customers include bank reference in formats like:
- "bank ref :fnb1234"
- "bank reference: abc123"
- "payment ref : xyz789"
-
Test extraction: Use the admin test endpoint:
POST /admin/test-extraction { "text": "Your test message with bank ref :test123" } -
Check MCP tools logs:
docker-compose logs -f mcp-tools
- Check service logs:
docker-compose logs -f - Test individual components with provided scripts
- Verify environment variables are correctly set
- Ensure all required ports are available
The system is designed for horizontal scaling:
- Stateless services: All components can be replicated
- Database connection pooling: Ready for multiple instances
- Queue-ready: Easy to add Redis for background processing
- Load balancer ready: Standard HTTP interfaces
- Input validation on all endpoints
- SQL injection protection via SQLAlchemy
- Rate limiting ready (add middleware)
- CORS configuration for production
- Environment-based configuration
- No secrets in code or containers
- Check service health:
./scripts/test_system.sh - View logs:
docker-compose logs -f - Test individual components: Use the provided scripts
- API documentation: http://localhost:8000/docs
System is ready for production use with minimal configuration changes.