How donbarbos/telegram-bot-template works
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: database DB layer · users.py Admin views · docker-compose.yml Docker stack · handlers Handlers
Components 19
Bot service
Telegram bot runtime and its internal request path
- Bot main · Telegram bot process entry point
bot/__main__.py - Middlewares · Auth, logging, throttling, i18n, database, Prometheus, and channel checks
bot/middlewares - Handlers · Conversation and command handlers for user-facing bot flows
bot/handlers - Services · User management and analytics orchestration
bot/services - Keyboards · Reply and inline keyboard definitions
bot/keyboards - Filters · Custom handler filters and predicates
bot/filters - Analytics clients · Vendor adapters for Amplitude, PostHog, and Google Analytics
bot/analytics
Admin panel
Flask-based operational UI for shared bot data
- Admin app · Gunicorn-served administrative web application
admin/app.py - Admin views · Operational pages, especially user management and export
admin/views/users.py - Templates · AdminLTE-style rendering layer and security pages
admin/templates - Static assets · JavaScript, plugins, and styling for the admin UI
admin/static
Data layer
Shared persistence, cache, and migrations
- Redis cache · Redis-backed cache and serialized bot state
bot/cache - DB layer · Database access layer for PostgreSQL and SQLAlchemy models
bot/database - User model · Primary persisted entity including referral tracking
bot/database/models/user.py - Migrations · Database schema evolution and referrer migration
migrations
Ops stack
Deployment, monitoring, and maintenance tooling
- Prometheus · Metrics collection for the bot and related services
configs/prometheus/prometheus.yml - Grafana · Dashboards and datasources for operational visibility
configs/grafana - Ops scripts · Database lifecycle, backup, restore, and migration helpers
scripts - Docker stack · Container composition and environment-driven runtime layout
docker-compose.yml
Connections 24
- 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