# abdulaleemdev/telegram-ai-bot architecture

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

- Interactive diagram: https://gitdiagram.com/abdulaleemdev/telegram-ai-bot
- Repository: https://github.com/abdulaleemdev/telegram-ai-bot

## Overview

Telegram AI Bot receives Telegram messages through a FastAPI webhook, processes them with a LangGraph support agent, and replies through Telegram. The agent can answer FAQs, track orders, and handle support tickets with order verification; chat history and order/ticket records provide persistence. The graph includes the documented PostgreSQL and Groq integrations. Relationships for unsampled modules are limited to references in the source index and README; those modules were not inspected in full.

Main entry points: [`app/graph/agent.py`](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/graph/agent.py) (LangGraph agent), [`app/core/database.py`](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/database.py) (Database connection), [`app/main.py`](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/main.py) (FastAPI app), [`app/database/models.py`](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/database/models.py) (Order and ticket models)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingress["Webhook and Telegram"]
  node_fastapi["FastAPI app<br/>[main.py]"]
  node_webhook["Webhook handler<br/>[webhook.py]"]
  node_telegram["Telegram integration<br/>[telegram.py]"]
end

subgraph group_conversation["AI Conversations"]
  node_bot_service["Message service<br/>[bot_service.py]"]
  node_agent["LangGraph agent<br/>[agent.py]"]
  node_llm["Groq language model<br/>[ai.py]"]
  node_memory["Chat memory<br/>[chat_memory.py]"]
end

subgraph group_support["Customer Support Tools"]
  node_faq["FAQ lookup<br/>[faq_tool.py]"]
  node_tracking["Order tracking"]
  node_verification["Order verification"]
  node_tickets["Ticket creation<br/>[ticket_tool.py]"]
end

subgraph group_persistence["Configuration and Data"]
  node_database_config["Database connection<br/>[database.py]"]
  node_models["Order and ticket models<br/>[models.py]"]
  node_base["ORM base<br/>[base.py]"]
  node_config["Application config<br/>[config.py]"]
  node_logging["Logging setup<br/>[logging_config.py]"]
end

node_customer(("Telegram user"))
node_telegram_api["Telegram API"]
node_groq["Groq API"]
node_postgres[("PostgreSQL")]

node_customer -->|"sends update"| node_webhook
node_fastapi -->|"mounts router"| node_webhook
node_fastapi -->|"registers webhook"| node_telegram
node_fastapi -->|"validates settings"| node_config
node_fastapi -->|"sets up logging"| node_logging
node_webhook -->|"dispatches message"| node_bot_service
node_bot_service -->|"runs agent"| node_agent
node_bot_service -->|"sends reply"| node_telegram
node_telegram -->|"calls API"| node_telegram_api
node_telegram_api -.->|"delivers reply"| node_customer
node_agent -->|"invokes model"| node_llm
node_llm -.->|"uses provider"| node_groq
node_agent -->|"loads and saves"| node_memory
node_agent -->|"dispatches tool"| node_faq
node_agent -->|"dispatches tool"| node_tracking
node_agent -->|"dispatches tool"| node_verification
node_agent -->|"dispatches tool"| node_tickets
node_tracking -->|"opens session"| node_database_config
node_tracking -->|"queries orders"| node_models
node_tickets -->|"opens session"| node_database_config
node_tickets -->|"reads and writes"| node_models
node_verification -->|"uses database"| node_database_config
node_verification -->|"checks orders"| node_models
node_database_config -->|"reads URL"| node_config
node_database_config -.->|"connects to"| node_postgres
node_models -->|"inherits from"| node_base

click node_fastapi "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/main.py"
click node_webhook "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/api/webhook.py"
click node_telegram "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/telegram.py"
click node_bot_service "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/services/bot_service.py"
click node_agent "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/graph/agent.py"
click node_llm "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/ai.py"
click node_memory "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/services/chat_memory.py"
click node_faq "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/tools/faq_tool.py"
click node_tracking "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/tools/track_order_tool.py"
click node_verification "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/tools/verify_order_tool.py"
click node_tickets "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/tools/ticket_tool.py"
click node_database_config "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/database.py"
click node_models "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/database/models.py"
click node_base "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/database/base.py"
click node_config "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/config.py"
click node_logging "https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/logging_config.py"

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_fastapi,node_webhook,node_telegram,node_customer toneBlue
class node_bot_service,node_agent,node_llm,node_memory,node_postgres toneAmber
class node_faq,node_tracking,node_verification,node_tickets,node_telegram_api,node_groq toneMint
class node_database_config,node_models,node_base,node_config,node_logging toneRose
```

## Components

### Webhook and Telegram

- [FastAPI app](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/main.py): `app/main.py`
- [Webhook handler](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/api/webhook.py): `app/api/webhook.py`
- [Telegram integration](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/telegram.py): `app/core/telegram.py`

### AI Conversations

- [Message service](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/services/bot_service.py): `app/services/bot_service.py`
- [LangGraph agent](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/graph/agent.py): `app/graph/agent.py`
- [Groq language model](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/ai.py): `app/core/ai.py`
- [Chat memory](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/services/chat_memory.py): `app/services/chat_memory.py`

### Customer Support Tools

- [FAQ lookup](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/tools/faq_tool.py): `app/tools/faq_tool.py`
- [Order tracking](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/tools/track_order_tool.py): `app/tools/track_order_tool.py`
- [Order verification](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/tools/verify_order_tool.py): `app/tools/verify_order_tool.py`
- [Ticket creation](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/tools/ticket_tool.py): `app/tools/ticket_tool.py`

### Configuration and Data

- [Database connection](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/database.py): `app/core/database.py`
- [Order and ticket models](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/database/models.py): `app/database/models.py`
- [ORM base](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/database/base.py): `app/database/base.py`
- [Application config](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/config.py): `app/core/config.py`
- [Logging setup](https://github.com/abdulaleemdev/telegram-ai-bot/blob/main/app/core/logging_config.py): `app/core/logging_config.py`

### Other components

- **Telegram user**
- **Telegram API**
- **Groq API**
- **PostgreSQL**

## Connections

- Telegram user → Webhook handler: sends update (evidence: `README.md`)
- FastAPI app → Webhook handler: mounts router (evidence: `app/main.py`)
- FastAPI app → Telegram integration: registers webhook (evidence: `app/main.py`)
- FastAPI app → Application config: validates settings (evidence: `app/main.py`)
- FastAPI app → Logging setup: sets up logging (evidence: `app/main.py`)
- Webhook handler → Message service: dispatches message (evidence: `app/api/webhook.py`)
- Message service → LangGraph agent: runs agent (evidence: `app/services/bot_service.py`)
- Message service → Telegram integration: sends reply (evidence: `app/services/bot_service.py`)
- Telegram integration → Telegram API: calls API (evidence: `app/core/telegram.py`)
- Telegram API → Telegram user: delivers reply
- LangGraph agent → Groq language model: invokes model (evidence: `app/graph/agent.py`)
- Groq language model → Groq API: uses provider (evidence: `README.md`)
- LangGraph agent → Chat memory: loads and saves (evidence: `app/graph/agent.py`)
- LangGraph agent → FAQ lookup: dispatches tool (evidence: `app/graph/agent.py`)
- LangGraph agent → Order tracking: dispatches tool (evidence: `app/graph/agent.py`)
- LangGraph agent → Order verification: dispatches tool (evidence: `app/graph/agent.py`)
- LangGraph agent → Ticket creation: dispatches tool (evidence: `app/graph/agent.py`)
- Order tracking → Database connection: opens session (evidence: `app/tools/track_order_tool.py`)
- Order tracking → Order and ticket models: queries orders (evidence: `app/tools/track_order_tool.py`)
- Ticket creation → Database connection: opens session (evidence: `app/tools/ticket_tool.py`)
- Ticket creation → Order and ticket models: reads and writes (evidence: `app/tools/ticket_tool.py`)
- Order verification → Database connection: uses database (evidence: `app/tools/verify_order_tool.py`)
- Order verification → Order and ticket models: checks orders (evidence: `app/tools/verify_order_tool.py`)
- Database connection → Application config: reads URL (evidence: `app/core/database.py`)
- Database connection → PostgreSQL: connects to (evidence: `README.md`)
- Order and ticket models → ORM base: inherits from (evidence: `app/database/models.py`)
