How pythonhubstudio/aiogram-3-course-telegram-bot works
• This repository is a course-style progression of standalone Telegram bot examples, organized by lesson folder. Each lesson_* directory is effectively its own mini-application, usually with its own app.py as the entry point and its own supporting modules.
• The core architectural boundary in almost every lesson is:
Telegram update ingress → aiogram router/filters → handler logic → optional keyboards/database/stateful services → Telegram response.
The code is built around aiogram 3’s event-driven bot model.
• app.py in each lesson is the composition root. It wires the bot, dispatcher, routers, middleware, and startup configuration. This is where the app’s subsystems are assembled rather than where business logic lives.
• The handlers/ package is the main application layer. It is split by interaction context:
- user_private.py for direct chats with users
- user_group.py for group chat behavior
- admin_private.py for admin-only/private flows
- later lessons add menu_processing.py for callback-driven menu navigation
This separation is important for diagramming because chat context is a primary routing dimension.
• The filters/ package contains message-routing constraints, especially chat_types.py. These filters keep private-chat logic, group-chat logic, and admin/private behavior separated before handlers execute. In other words, filters are the first gate in the request flow.
• The kbds/ package is the presentation/output layer for Telegram UI elements:
- reply.py for reply keyboards
- inline.py for inline keyboards in later lessons
This is where user interaction controls are generated, and it becomes especially important once callback-based flows appear.
• The common/ package holds shared domain constants and text resources:
- bot_cmds_list.py centralizes bot command definitions
- restricted_words.py appears when moderation/filtering of user content is introduced
- texts_for_db.py in later lessons provides content seeds or text data used by persistence-related examples
This package is the repository’s shared configuration/data layer, not application logic.
• lesson_6 introduces FSM/stateful conversation handling. That changes the architecture from simple stateless handlers to multi-step flows that preserve user progress across updates. In diagram terms, handler execution is no longer isolated per message; it can depend on stored conversation state.
• lesson_7 adds a real persistence subsystem under database/:
- engine.py for DB connection/engine setup
- models.py for data models
- orm_query.py for query abstractions
This is paired with middlewares/db.py, which indicates DB access is injected into handler processing through middleware rather than manually opened everywhere.
• From lesson_7 onward, the data flow includes:
Telegram update → middleware → DB session/context → ORM/query layer → handlers → response.
That middleware boundary is architecturally important because it centralizes resource lifecycle and handler access to persistence.
• lesson_8 expands the interaction model with content-rich and menu-driven UI:
- banners/ suggests externally stored or reusable media/content
- utils/paginator.py introduces pagination support for multi-item navigation
- handlers/menu_processing.py supports multi-level inline menu traversal
This shifts the bot from command-response style to structured navigational UX.
• The repo is not a single cohesive product backend; it is a set of examples showing architectural evolution:
1. basic bot wiring
2. handler/filter separation
3. keyboards and command organization
4. FSM conversations
5. DB-backed flows with middleware
6. inline multi-level menus and pagination
• For an architecture diagram, the most important cross-cutting components are:
aiogram dispatcher/routers, chat-type filters, role-based handlers, keyboards, FSM state, DB middleware, and the database layer.
External dependency boundaries are primarily Telegram Bot API and, in later lessons, a relational database accessed through ORM-style code.
Main entry points: lesson_4/app.py App · lesson_8_multi_level_inline_menu/app.py Menu app · lesson_7/app.py DB app · chat_types.py Chat filters
Components 23
Lessons 1-3
Foundational aiogram bot wiring and early handler composition.
Lessons 4-5
Chat-type routing and keyboard-driven interaction split by context.
- App · Wires filters and context-specific handlers.
lesson_4/app.py - Chat filters · Separates private and group update paths early.
lesson_4/filters/chat_types.py - Private handler · Private chat update handling.
lesson_4/handlers/user_private.py - Group handler · Group chat update handling.
lesson_4/handlers/user_group.py - Admin handler · Admin-only private chat handling.
lesson_4/handlers/admin_private.py - App · Adds keyboard composition to the routed handler flow.
lesson_5/app.py - Reply kbds · Reply keyboard construction for user interaction.
lesson_5/kbds/reply.py - Bot cmds · Central bot command definitions.
lesson_5/common/bot_cmds_list.py
Lesson 6 FSM
Stateful conversation handling layered onto the handler model.
- FSM app · Stateful conversation example with multi-step flows.
lesson_6-FSM/app.py - Restricted words · Content constraint list used by moderation or validation logic.
lesson_6-FSM/common/restricted_words.py
Lesson 7 DB
Database-backed bot flow with injected DB context.
- DB app · Integrates persistence into the bot pipeline.
lesson_7/app.py - DB middleware · Injects database context into update handling.
lesson_7/middlewares/db.py - Engine · Database connection and engine initialization.
lesson_7/database/engine.py - Models · Persistent domain model definitions.
lesson_7/database/models.py - ORM query · Database query abstractions used by handlers.
lesson_7/database/orm_query.py
Lesson 8 Menus
Inline navigation, pagination, and menu traversal examples.
- Menu app · Advanced inline-menu example with navigation flows.
lesson_8_multi_level_inline_menu/app.py - Menu handler · Processes callback-driven menu traversal.
lesson_8_multi_level_inline_menu/handlers/menu_processing.py - Inline kbds · Inline keyboard generation for menu navigation.
lesson_8_multi_level_inline_menu/kbds/inline.py - Paginator · Supports multi-item pagination in inline flows.
lesson_8_multi_level_inline_menu/utils/paginator.py - DB texts · Seed text/content used by persistence-oriented examples.
lesson_8_multi_level_inline_menu/common/texts_for_db.py
Connections 29
- App → App: evolves to · Repository progression from one lesson to the next.
- App → App: evolves to · Adds clearer handler separation.
- App → App: evolves to · Introduces routing filters and more explicit chat contexts.
- App → Chat filters: registers · Composition root wires chat-type filters.
- App → Private handler: routes to · Private chat updates are dispatched here.
- App → Group handler: routes to · Group chat updates are dispatched here.
- App → Admin handler: routes to · Admin/private updates are dispatched here.
- Chat filters → Private handler: gates · Private-chat filtering precedes handler execution.
- Chat filters → Group handler: gates · Group-chat filtering precedes handler execution.
- Chat filters → Admin handler: gates · Admin/private filtering precedes handler execution.
- App → App: evolves to · Keyboard-driven interaction is added next.
- App → Reply kbds: builds UI · Reply keyboards are assembled as presentation objects.
- App → Bot cmds: uses · Command list is centralized in shared data.
- App → FSM app: evolves to · Conversation state is introduced.
- FSM app → Restricted words: uses · Stateful flows can apply content restrictions.
- FSM app → DB app: evolves to · Persistence is added through middleware and ORM layers.
- DB app → DB middleware: injects · Database context is provided through middleware.
- DB middleware → Engine: opens · Middleware relies on the DB engine and session lifecycle.
- DB app → Models: persists via · Handlers operate over model definitions.
- DB app → ORM query: queries via · Handler logic delegates reads and writes to query abstractions.
- ORM query → Models: maps · Query layer operates on ORM models.
- DB app → Menu app: evolves to · Example set advances to richer menu navigation.
- Menu app → Menu handler: routes to · Callback/menu traversal is handled separately.
- Menu app → Inline kbds: builds UI · Inline keyboards drive navigation.
- Menu app → Paginator: uses · Pagination supports multi-item menu pages.
- Menu app → DB texts: uses · Seed content backs database-oriented examples.
- Menu handler → Inline kbds: updates · Menu handler swaps inline keyboards across steps.
- Menu handler → Paginator: depends on · Traversal and page selection rely on pagination.
- Menu app → ORM query: reuses · Advanced lesson continues to rely on the ORM/query layer.