engine.factory — Bot + Dispatcher creation
BotBuilder
BotBuilder
Builder for Bot + Dispatcher with automatic core middleware management.
Ensures that essential framework middlewares are registered in the correct sequence.
Automatically injects the created Bot instance into the provided Container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bot_token
|
str
|
Telegram bot token obtained from @BotFather. |
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
|
Example
builder = BotBuilder(bot_token="...")
builder.setup_core(container=my_container)
builder.add_project_middleware(MyAnalyticsMiddleware())
bot, dp = builder.build()
Source code in src/codex_bot/engine/factory/bot_builder.py
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
Functions
__init__(bot_token, parse_mode='HTML', fsm_storage=None, dispatcher_kwargs=None)
Initializes the builder with basic settings.
Source code in src/codex_bot/engine/factory/bot_builder.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
add_middleware(middleware)
Alias for add_project_middleware. Maintained for backward compatibility.
Source code in src/codex_bot/engine/factory/bot_builder.py
108 109 110 | |
add_project_middleware(middleware)
Adds a project-specific middleware to the execution chain.
Project middlewares are automatically registered AFTER the core framework ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
middleware
|
BaseMiddleware
|
Instance of an aiogram BaseMiddleware. |
required |
Returns:
| Type | Description |
|---|---|
BotBuilder
|
The builder instance for method chaining. |
Source code in src/codex_bot/engine/factory/bot_builder.py
94 95 96 97 98 99 100 101 102 103 104 105 106 | |
build()
Assembles and configures Bot and Dispatcher.
- Creates the Bot instance.
- Injects the Bot into the Container (if linked via setup_core).
- Initializes the Dispatcher and registers all middlewares.
Returns:
| Type | Description |
|---|---|
tuple[Bot, Dispatcher]
|
A tuple of (Bot, Dispatcher). |
Source code in src/codex_bot/engine/factory/bot_builder.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
setup_core(container)
Registers mandatory framework middlewares and links the container.
Registration order: 1. Container Injection: Makes the DI container available in context. 2. Director Initialization: Creates the request coordinator. 3. User Validation (RBAC): Handles user identification and admin checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
container
|
ContainerProtocol
|
The project's DI container implementing ContainerProtocol. |
required |
Returns:
| Type | Description |
|---|---|
BotBuilder
|
The builder instance for method chaining. |
Source code in src/codex_bot/engine/factory/bot_builder.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |