engine.factory — Bot + Dispatcher creation
BotBuilder
BotBuilder
Builder for Bot + Dispatcher with explicit middleware management.
Allows injecting custom middleware at arbitrary points, unlike the functional approach with a fixed order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bot_token
|
str
|
Telegram bot token. |
required |
parse_mode
|
str
|
Message parsing mode (default is |
'HTML'
|
fsm_storage
|
BaseStorage | None
|
FSM storage. |
None
|
dispatcher_kwargs
|
dict[str, Any] | None
|
Additional kwargs for |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from codex_bot.engine.factory import BotBuilder
from codex_bot.engine.middlewares import UserValidationMiddleware, ThrottlingMiddleware
builder = BotBuilder(bot_token=settings.bot_token, fsm_storage=RedisStorage(redis))
builder.add_middleware(UserValidationMiddleware())
builder.add_middleware(ThrottlingMiddleware(redis=redis_client))
builder.add_middleware(MyAnalyticsMiddleware())
bot, dp = builder.build()
Source code in src/codex_bot/engine/factory/bot_builder.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
Functions
add_middleware(middleware)
Adds middleware to the connection queue.
Middleware are connected in the order they are added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
middleware
|
Any
|
Middleware instance to connect to |
required |
Returns:
| Type | Description |
|---|---|
BotBuilder
|
|
Example
builder.add_middleware(UserValidationMiddleware())
.add_middleware(ThrottlingMiddleware(redis))
Source code in src/codex_bot/engine/factory/bot_builder.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
build()
Assembles Bot and Dispatcher.
Creates a Bot with the specified parameters and a Dispatcher with FSM storage.
Connects all middleware via dp.update.middleware.
Returns:
| Type | Description |
|---|---|
tuple[Bot, Dispatcher]
|
Tuple |
Example
bot, dp = builder.build()
await dp.start_polling(bot)
Source code in src/codex_bot/engine/factory/bot_builder.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |