βοΈ Engine
The engine module provides the core infrastructure for building and running an aiogram bot with codex-bot.
π§ The Why
Parametric Assembly
In a large bot, the main router and dispatcher can become cluttered with imports. The engine module solves this by using Parametric Assembly (via RouterBuilder and BotBuilder). This allows you to explicitly control the order of middleware and features without hardcoding them in a single file.
Feature Discovery
The FeatureDiscoveryService automates the registration of features (routers, orchestrators, menu configs, and garbage states). This follows the Django INSTALLED_APPS pattern, making it easy to add or remove features by simply updating a list in your settings.
π The Flow
- Discovery:
FeatureDiscoveryServicescans theinstalled_featureslist and imports theirhandlers.pyandfeature_setting.py. - Assembly:
RouterBuildercollects allRouterobjects from the discovered features and includes them in the main router. - Configuration:
BotBuildercreates theBotandDispatcherinstances, injecting the discovered features and middlewares in the specified order. - Startup: The bot starts polling, with all features and infrastructure (i18n, throttling, container) ready to handle requests.
πΊοΈ Module Map
| Component | Description |
|---|---|
| π Router Builder | build_main_router and collect_feature_routers. |
| π Discovery | FeatureDiscoveryService for auto-discovery and registration. |
| π Factory | BotBuilder for creating Bot and Dispatcher. |
| π Middlewares | UserValidation, Throttling, Container, and I18n middlewares. |
| π HTTP Client | BaseApiClient with connection pooling. |
| π I18n Compiler | compile_locales for Fluent (.ftl) files. |
Last Updated: 2025-02-07