# shivammitra/ai-lead-automation architecture

> Architecture diagram of the GitHub repository shivammitra/ai-lead-automation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shivammitra/ai-lead-automation
- Repository: https://github.com/shivammitra/ai-lead-automation

## Overview

#### 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`](https://github.com/shivammitra/ai-lead-automation/blob/main/bot.py) (Telegram bot), [`app.py`](https://github.com/shivammitra/ai-lead-automation/blob/main/app.py) (Flask dashboard), [`db.py`](https://github.com/shivammitra/ai-lead-automation/blob/main/db.py) (Database access), [`templates/dashboard.html`](https://github.com/shivammitra/ai-lead-automation/blob/main/templates/dashboard.html) (Dashboard view)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_qualification["Lead qualification"]
  node_prospect(("Prospect<br/>user"))
  node_telegram["Telegram Bot API<br/>external API"]
  node_bot["Telegram bot<br/>Python orchestrator<br/>[bot.py]"]
  node_gemini["Gemini API<br/>AI classification"]
  node_db["Database access<br/>persistence layer<br/>[db.py]"]
  node_sqlite[("Lead store<br/>SQLite database")]
  node_env["Runtime secrets<br/>environment config<br/>[.env.example]"]
end

subgraph group_dashboard["Dashboard"]
  node_browser(("Dashboard browser<br/>web client"))
  node_app["Flask dashboard<br/>Python HTTP server<br/>[app.py]"]
  node_template["Dashboard view<br/>HTML template<br/>[dashboard.html]"]
  node_chartjs["Chart.js<br/>browser visualization"]
end

subgraph group_automation["Hot-lead automation"]
  node_n8n["n8n webhook<br/>automation service"]
  node_workflow["Notification workflow<br/>n8n workflow"]
end

node_prospect -->|"sends messages"| node_telegram
node_telegram -->|"delivers updates"| node_bot
node_bot -->|"qualification context"| node_gemini
node_gemini -->|"score and response"| node_bot
node_bot -->|"writes leads and history"| node_db
node_db -->|"persists"| node_sqlite
node_bot -->|"sends reply"| node_telegram
node_env -.->|"API configuration"| node_bot
node_bot -->|"POST when Hot"| node_n8n
node_n8n -->|"triggers"| node_workflow
node_browser -->|"HTTP request"| node_app
node_app -->|"reads lead data"| node_db
node_app -->|"renders"| node_template
node_template -->|"dashboard response"| node_browser
node_chartjs -->|"visualizes scores"| node_template
node_env -.->|"webhook URL"| node_n8n

click node_bot "https://github.com/shivammitra/ai-lead-automation/blob/main/bot.py"
click node_db "https://github.com/shivammitra/ai-lead-automation/blob/main/db.py"
click node_env "https://github.com/shivammitra/ai-lead-automation/blob/main/.env.example"
click node_workflow "https://github.com/shivammitra/ai-lead-automation/blob/main/n8n/lead_notification_workflow.json"
click node_app "https://github.com/shivammitra/ai-lead-automation/blob/main/app.py"
click node_template "https://github.com/shivammitra/ai-lead-automation/blob/main/templates/dashboard.html"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_prospect,node_telegram,node_bot,node_gemini,node_db,node_sqlite,node_env toneBlue
class node_browser,node_app,node_template,node_chartjs toneAmber
class node_n8n,node_workflow toneMint
```

## Components

### Lead qualification

- **Prospect**: user
- **Telegram Bot API**: external API
- [Telegram bot](https://github.com/shivammitra/ai-lead-automation/blob/main/bot.py): Python orchestrator · Runs polling and manages qualification conversations. · `bot.py`
- **Gemini API**: AI classification · Extracts lead details and assigns Hot/Warm/Cold.
- [Database access](https://github.com/shivammitra/ai-lead-automation/blob/main/db.py): persistence layer · `db.py`
- **Lead store**: SQLite database · Stores lead state, history, and scores.
- [Runtime secrets](https://github.com/shivammitra/ai-lead-automation/blob/main/.env.example): environment config · Provides Telegram, Gemini, and n8n credentials. · `.env.example`

### Dashboard

- **Dashboard browser**: web client
- [Flask dashboard](https://github.com/shivammitra/ai-lead-automation/blob/main/app.py): Python HTTP server · `app.py`
- [Dashboard view](https://github.com/shivammitra/ai-lead-automation/blob/main/templates/dashboard.html): HTML template · Shows metrics, lead records, and conversation history. · `templates/dashboard.html`
- **Chart.js**: browser visualization · Renders Hot/Warm/Cold charts.

### Hot-lead automation

- **n8n webhook**: automation service
- [Notification workflow](https://github.com/shivammitra/ai-lead-automation/blob/main/n8n/lead_notification_workflow.json): n8n workflow · Filters Hot leads and routes notifications. · `n8n/lead_notification_workflow.json`

## Connections

- 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
