# donbarbos/telegram-bot-template architecture

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

- Interactive diagram: https://gitdiagram.com/donbarbos/telegram-bot-template
- Repository: https://github.com/donbarbos/telegram-bot-template

## Overview

1. This repository is a two-process Telegram bot platform: a Telegram bot service under `bot/` and a separate Flask-based admin panel under `admin/`. They share the same database and operational infrastructure, but they are deployed and run independently.

2. The bot is the primary runtime. Its entry point is `bot/__main__.py`, and the app is organized around aiogram-style responsibilities: update handlers, filters, middlewares, keyboards, services, and utility modules. The bot is the only component that talks to Telegram.

3. The bot’s request path is layered: incoming Telegram updates pass through middlewares first, then route into handlers, which delegate non-trivial logic to service modules. That separation is important: handlers manage conversation flow, while `bot/services/` contains business logic such as user management and analytics recording.

4. Persistence is centered on PostgreSQL through SQLAlchemy v2. The database layer lives in `bot/database/`, with model definitions under `bot/database/models/` and Alembic migrations in `migrations/`. The schema currently includes a user model and a later migration that adds a referrer relation, indicating referral tracking is part of the domain.

5. Redis is used as a secondary state layer. The cache package and Redis middleware suggest it supports caching and bot state/FSM-style workflows, while a serialization helper indicates values are stored and retrieved in structured form rather than as ad hoc strings.

6. Observability is built into the bot process. There is dedicated middleware for logging, throttling, i18n, Prometheus metrics, authentication, and channel-subscription checks. In practice, this means cross-cutting concerns are enforced before handler execution rather than inside business logic.

7. Product analytics is abstracted behind a small client/service boundary. The `bot/analytics/` package contains separate clients for Amplitude, PostHog, and Google Analytics, so the rest of the bot can emit events without being coupled to one vendor. Sentry is listed in the environment and README as the error-tracking layer for failures.

8. The admin panel is a separate Flask application served with Gunicorn. Its code lives in `admin/app.py`, with configuration in `admin/config.py` and custom views in `admin/views/`. It uses Flask-Admin/AdminLTE-style templates and static assets to present a browser UI over the same backend data.

9. The admin panel’s main role is operational access to bot data, especially users. The presence of `admin/views/users.py` and the README’s export features indicate it is used to inspect and export user records in several formats, acting as an internal control plane rather than a user-facing product surface.

10. Deployment is containerized and environment-driven. The root `Dockerfile`, `admin/Dockerfile`, and `docker-compose.yml` define the runtime layout, while `.env` variables configure bot token, database, Redis, analytics keys, webhook settings, and admin credentials. This makes the system portable across local development and production.

11. Monitoring and dashboards are part of the same stack. `configs/prometheus/` defines Prometheus scraping, and `configs/grafana/` defines Grafana datasources and dashboards. These components observe the bot and related services rather than being embedded in the bot code.

12. The `scripts/` directory and Alembic tooling show operational support for database lifecycle tasks such as migrations and backups/restores across PostgreSQL, MySQL, and Mongo-style scripts. Even though PostgreSQL is the primary database in this repo, the scripts folder is clearly meant to standardize maintenance workflows.

13. Overall data flow is: Telegram update → bot middlewares → handler → service layer → PostgreSQL/Redis and optional analytics emission; separately, browser requests → admin panel → shared database → export/view responses. Monitoring and error tracking run alongside both services and do not sit in the main request path.

Main entry points: [`bot/database`](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/database) (DB layer), [`admin/views/users.py`](https://github.com/donbarbos/telegram-bot-template/blob/main/admin/views/users.py) (Admin views), [`docker-compose.yml`](https://github.com/donbarbos/telegram-bot-template/blob/main/docker-compose.yml) (Docker stack), [`bot/handlers`](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/handlers) (Handlers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_bot["Bot service"]
  node_bot_entry["Bot main<br/>aiogram app<br/>[__main__.py]"]
  node_bot_middlewares["Middlewares<br/>cross-cutting"]
  node_bot_handlers["Handlers<br/>update routing"]
  node_bot_services["Services<br/>business logic"]
  node_bot_keyboards["Keyboards<br/>ui builders"]
  node_bot_filters["Filters<br/>routing rules"]
  node_bot_analytics["Analytics clients<br/>event emitters"]
end

subgraph group_admin["Admin panel"]
  node_admin_app["Admin app<br/>flask app<br/>[app.py]"]
  node_admin_views["Admin views<br/>control views<br/>[users.py]"]
  node_admin_templates["Templates"]
  node_admin_static["Static assets<br/>frontend assets"]
end

subgraph group_data["Data layer"]
  node_bot_cache["Redis cache<br/>state cache"]
  node_bot_db[("DB layer<br/>sqlalchemy persistence")]
  node_bot_user_model["User model<br/>domain model<br/>[user.py]"]
  node_migrations["Migrations<br/>alembic schema"]
end

subgraph group_ops["Ops stack"]
  node_prometheus{{"Prometheus<br/>metrics scrape<br/>[prometheus.yml]"}}
  node_grafana["Grafana<br/>dashboards"]
  node_scripts["Ops scripts<br/>maintenance tools"]
  node_compose["Docker stack<br/>deployment<br/>[docker-compose.yml]"]
end

node_bot_entry -->|"loads"| node_bot_middlewares
node_bot_middlewares -->|"routes to"| node_bot_handlers
node_bot_handlers -->|"delegates"| node_bot_services
node_bot_handlers -->|"renders"| node_bot_keyboards
node_bot_handlers -->|"selects by"| node_bot_filters
node_bot_services -->|"reads/writes"| node_bot_db
node_bot_services -->|"uses"| node_bot_cache
node_bot_services -->|"emits events"| node_bot_analytics
node_bot_db -->|"maps"| node_bot_user_model
node_bot_db -->|"evolves via"| node_migrations
node_migrations -.->|"shapes"| node_bot_user_model
node_admin_app -->|"mounts"| node_admin_views
node_admin_views -->|"renders"| node_admin_templates
node_admin_views -->|"serves"| node_admin_static
node_admin_views -->|"queries"| node_bot_db
node_admin_views -->|"exports"| node_bot_user_model
node_bot_entry -->|"reports"| node_prometheus
node_bot_entry -.->|"observed by"| node_grafana
node_compose -->|"runs"| node_bot_entry
node_compose -->|"runs"| node_admin_app
node_compose -->|"connects to"| node_bot_db
node_compose -->|"connects to"| node_bot_cache
node_scripts -.->|"supports"| node_migrations
node_scripts -.->|"backs up"| node_bot_db

click node_bot_entry "https://github.com/donbarbos/telegram-bot-template/blob/main/bot/__main__.py"
click node_bot_middlewares "https://github.com/donbarbos/telegram-bot-template/tree/main/bot/middlewares"
click node_bot_handlers "https://github.com/donbarbos/telegram-bot-template/tree/main/bot/handlers"
click node_bot_services "https://github.com/donbarbos/telegram-bot-template/tree/main/bot/services"
click node_bot_keyboards "https://github.com/donbarbos/telegram-bot-template/tree/main/bot/keyboards"
click node_bot_filters "https://github.com/donbarbos/telegram-bot-template/tree/main/bot/filters"
click node_bot_analytics "https://github.com/donbarbos/telegram-bot-template/tree/main/bot/analytics"
click node_bot_cache "https://github.com/donbarbos/telegram-bot-template/tree/main/bot/cache"
click node_bot_db "https://github.com/donbarbos/telegram-bot-template/tree/main/bot/database"
click node_bot_user_model "https://github.com/donbarbos/telegram-bot-template/blob/main/bot/database/models/user.py"
click node_migrations "https://github.com/donbarbos/telegram-bot-template/tree/main/migrations"
click node_admin_app "https://github.com/donbarbos/telegram-bot-template/blob/main/admin/app.py"
click node_admin_views "https://github.com/donbarbos/telegram-bot-template/blob/main/admin/views/users.py"
click node_admin_templates "https://github.com/donbarbos/telegram-bot-template/tree/main/admin/templates"
click node_admin_static "https://github.com/donbarbos/telegram-bot-template/tree/main/admin/static"
click node_prometheus "https://github.com/donbarbos/telegram-bot-template/blob/main/configs/prometheus/prometheus.yml"
click node_grafana "https://github.com/donbarbos/telegram-bot-template/tree/main/configs/grafana"
click node_scripts "https://github.com/donbarbos/telegram-bot-template/tree/main/scripts"
click node_compose "https://github.com/donbarbos/telegram-bot-template/blob/main/docker-compose.yml"

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_bot_entry,node_bot_middlewares,node_bot_handlers,node_bot_services,node_bot_keyboards,node_bot_filters,node_bot_analytics toneBlue
class node_admin_app,node_admin_views,node_admin_templates,node_admin_static toneAmber
class node_bot_cache,node_bot_db,node_bot_user_model,node_migrations toneMint
class node_prometheus,node_grafana,node_scripts,node_compose toneRose
```

## Components

### Bot service

Telegram bot runtime and its internal request path

- [Bot main](https://github.com/donbarbos/telegram-bot-template/blob/main/bot/__main__.py): aiogram app · Telegram bot process entry point · `bot/__main__.py`
- [Middlewares](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/middlewares): cross-cutting · Auth, logging, throttling, i18n, database, Prometheus, and channel checks · `bot/middlewares`
- [Handlers](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/handlers): update routing · Conversation and command handlers for user-facing bot flows · `bot/handlers`
- [Services](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/services): business logic · User management and analytics orchestration · `bot/services`
- [Keyboards](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/keyboards): ui builders · Reply and inline keyboard definitions · `bot/keyboards`
- [Filters](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/filters): routing rules · Custom handler filters and predicates · `bot/filters`
- [Analytics clients](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/analytics): event emitters · Vendor adapters for Amplitude, PostHog, and Google Analytics · `bot/analytics`

### Admin panel

Flask-based operational UI for shared bot data

- [Admin app](https://github.com/donbarbos/telegram-bot-template/blob/main/admin/app.py): flask app · Gunicorn-served administrative web application · `admin/app.py`
- [Admin views](https://github.com/donbarbos/telegram-bot-template/blob/main/admin/views/users.py): control views · Operational pages, especially user management and export · `admin/views/users.py`
- [Templates](https://github.com/donbarbos/telegram-bot-template/tree/main/admin/templates): ui templates · AdminLTE-style rendering layer and security pages · `admin/templates`
- [Static assets](https://github.com/donbarbos/telegram-bot-template/tree/main/admin/static): frontend assets · JavaScript, plugins, and styling for the admin UI · `admin/static`

### Data layer

Shared persistence, cache, and migrations

- [Redis cache](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/cache): state cache · Redis-backed cache and serialized bot state · `bot/cache`
- [DB layer](https://github.com/donbarbos/telegram-bot-template/tree/main/bot/database): sqlalchemy persistence · Database access layer for PostgreSQL and SQLAlchemy models · `bot/database`
- [User model](https://github.com/donbarbos/telegram-bot-template/blob/main/bot/database/models/user.py): domain model · Primary persisted entity including referral tracking · `bot/database/models/user.py`
- [Migrations](https://github.com/donbarbos/telegram-bot-template/tree/main/migrations): alembic schema · Database schema evolution and referrer migration · `migrations`

### Ops stack

Deployment, monitoring, and maintenance tooling

- [Prometheus](https://github.com/donbarbos/telegram-bot-template/blob/main/configs/prometheus/prometheus.yml): metrics scrape · Metrics collection for the bot and related services · `configs/prometheus/prometheus.yml`
- [Grafana](https://github.com/donbarbos/telegram-bot-template/tree/main/configs/grafana): dashboards · Dashboards and datasources for operational visibility · `configs/grafana`
- [Ops scripts](https://github.com/donbarbos/telegram-bot-template/tree/main/scripts): maintenance tools · Database lifecycle, backup, restore, and migration helpers · `scripts`
- [Docker stack](https://github.com/donbarbos/telegram-bot-template/blob/main/docker-compose.yml): deployment · Container composition and environment-driven runtime layout · `docker-compose.yml`

## Connections

- Bot main → Middlewares: loads · Initializes middleware pipeline before routing updates
- Middlewares → Handlers: routes to · Passes validated Telegram updates into handlers
- Handlers → Services: delegates · Handlers call service-layer business logic
- Handlers → Keyboards: renders · Handlers use keyboards for replies and menus
- Handlers → Filters: selects by · Filters determine which handler should run
- Services → DB layer: reads/writes · Business logic persists users and related records
- Services → Redis cache: uses · Service logic can rely on Redis-backed state
- Services → Analytics clients: emits events · Service layer sends product analytics events
- DB layer → User model: maps · Database layer exposes the user domain model
- DB layer → Migrations: evolves via · Schema changes are managed through Alembic migrations
- Migrations → User model: shapes · Migrations add and modify the user schema
- Admin app → Admin views: mounts · Flask app registers operational views
- Admin views → Templates: renders · Views produce HTML through shared templates
- Admin views → Static assets: serves · Admin UI depends on static assets
- Admin views → DB layer: queries · Admin panel inspects shared bot data
- Admin views → User model: exports · User administration centers on the user dataset
- Bot main → Prometheus: reports · Bot middleware exposes metrics for scraping
- Bot main → Grafana: observed by · Grafana dashboards visualize collected metrics
- Docker stack → Bot main: runs · Container stack launches the bot service
- Docker stack → Admin app: runs · Container stack launches the admin service
- Docker stack → DB layer: connects to · Shared deployment wiring includes PostgreSQL
- Docker stack → Redis cache: connects to · Deployment wiring includes Redis
- Ops scripts → Migrations: supports · Operational scripts coordinate schema lifecycle
- Ops scripts → DB layer: backs up · Maintenance tooling targets the primary database
