How shivammitra/ai-lead-automation works
Purpose and entry points
This is a Python lead-qualification system: a Telegram chatbot collects prospect details, Gemini classifies lead quality, SQLite persists conversations and scores, and a Flask dashboard exposes the pipeline. Run bot.py for Telegram polling and app.py for the dashboard. Core paths: bot.py, app.py, README.md.
Telegram conversation and AI qualification
bot.py is the primary ingestion and orchestration boundary. It receives Telegram messages, maintains the qualification conversation, sends context to Google Gemini, and returns the generated follow-up or response to Telegram. Gemini extracts/assesses name, need, budget, and timeline, producing Hot/Warm/Cold qualification. Paths: bot.py, .env.example.
Persistence model
db.py owns database access and the SQLite-backed lead/conversation persistence boundary. Bot writes lead state, conversation history, and scores to the local leads.db database; dashboard reads the same store. The design is intended to permit replacing SQLite with MySQL while preserving schema-level behavior. Paths: db.py, bot.py, app.py.
Hot-lead automation boundary
When a lead qualifies as Hot, the bot posts lead data to a configured n8n webhook. This decouples conversational qualification from downstream notification delivery. The imported n8n workflow receives the webhook, filters for Hot scores, and routes notification actions such as email, Slack, or logging. Paths: bot.py, n8n/lead_notification_workflow.json, .env.example.
Dashboard delivery
app.py runs the Flask HTTP server and queries persisted lead data for dashboard views. templates/dashboard.html renders pipeline metrics, Hot/Warm/Cold charting, lead records, and conversation history; Chart.js is the browser-side visualization dependency. Paths: app.py, templates/dashboard.html, db.py.
Runtime and external services
Python 3.9+ is the application runtime, with dependencies declared in requirements.txt. Required external integrations are Telegram Bot API, Google Gemini API, and optionally a locally or remotely hosted n8n instance. Runtime secrets/configuration include TELEGRAM_BOT_TOKEN, GEMINI_API_KEY, and N8N_WEBHOOK_URL. Paths: requirements.txt, .env.example, n8n/lead_notification_workflow.json.
Primary control flow
A prospect message flows Telegram → bot.py → Gemini qualification → db.py/SQLite persistence → Telegram reply. If the resulting score is Hot, bot.py additionally POSTs to n8n for notification automation. Independently, browser requests flow Flask (app.py) → db.py → dashboard template/chart rendering. Paths: bot.py, db.py, app.py.
Main entry points: bot.py Telegram bot · app.py Flask dashboard · db.py Database access · dashboard.html Dashboard view
Components 13
Lead qualification
- Prospect
- Telegram Bot API
- Telegram bot · Runs polling and manages qualification conversations.
bot.py - Gemini API · Extracts lead details and assigns Hot/Warm/Cold.
- Database access
db.py - Lead store · Stores lead state, history, and scores.
- Runtime secrets · Provides Telegram, Gemini, and n8n credentials.
.env.example
Dashboard
- Dashboard browser
- Flask dashboard
app.py - Dashboard view · Shows metrics, lead records, and conversation history.
templates/dashboard.html - Chart.js · Renders Hot/Warm/Cold charts.
Hot-lead automation
- n8n webhook
- Notification workflow · Filters Hot leads and routes notifications.
n8n/lead_notification_workflow.json
Connections 16
- Prospect → Telegram Bot API: sends messages
- Telegram Bot API → Telegram bot: delivers updates
- Telegram bot → Gemini API: qualification context
- Gemini API → Telegram bot: score and response
- Telegram bot → Database access: writes leads and history
- Database access → Lead store: persists
- Telegram bot → Telegram Bot API: sends reply
- Runtime secrets → Telegram bot: API configuration
- Telegram bot → n8n webhook: POST when Hot
- n8n webhook → Notification workflow: triggers
- Dashboard browser → Flask dashboard: HTTP request
- Flask dashboard → Database access: reads lead data
- Flask dashboard → Dashboard view: renders
- Dashboard view → Dashboard browser: dashboard response
- Chart.js → Dashboard view: visualizes scores
- Runtime secrets → n8n webhook: webhook URL