Dispatcher and Router¶
Bases: Router
Top-level router that starts update polling.
Dispatcher extends Router and is intended to be the root object that receives updates from MaxApi and dispatches them to handlers.
Initialize the dispatcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage
|
BaseStorage | None
|
BaseStorage instance to process. |
None
|
fsm_strategy
|
FSMStrategy
|
FSMStrategy instance to process. |
USER_IN_CHAT
|
events_isolation
|
BaseEventIsolation | None
|
BaseEventIsolation instance to process. |
None
|
disable_fsm
|
bool
|
The disable fsm value. |
False
|
name
|
str | None
|
The name value. |
None
|
kwargs
|
Any
|
Keyword arguments forwarded to the wrapped callable. |
{}
|
Source code in src/pyromax/dispatcher/Dispatcher.py
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 | |
update
instance-attribute
¶
update = UpdateMaxEventObserver(
router=self,
event_name="UPDATE",
type_of_update=MaxObject,
)
fsm
instance-attribute
¶
fsm = FSMContextMiddleware(
storage=storage or MemoryStorage(),
strategy=fsm_strategy,
events_isolation=events_isolation
or DisabledEventIsolation(),
)
start_polling
async
¶
start_polling(max_api: MaxApi) -> None
Start reading updates and dispatch them to handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_api
|
MaxApi
|
Initialized MaxApi instance. |
required |
Source code in src/pyromax/dispatcher/Dispatcher.py
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 149 150 151 152 | |
Bases: Subject
Container for handlers and nested routers.
Routers group event handlers into reusable modules and allow hierarchical composition of bot logic.
Attributes: events: a dict with all event observers(listeners)
Initialize the router.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
The name value. |
None
|
Source code in src/pyromax/dispatcher/Router.py
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 | |
message_removed
instance-attribute
¶
message_removed = RemovedMessageEventObserver(
self, "REMOVED", type_of_update=Message
)
edited_message
instance-attribute
¶
edited_message = MessageEventObserver(
self, "EDITED", type_of_update=Message
)
reply_to_message
instance-attribute
¶
reply_to_message = ReplyToMessageEventObserver(
self, "REPLY", type_of_update=Message
)
forward_message
instance-attribute
¶
forward_message = MessageForwardEventObserver(
self, "FORWARD", type_of_update=Message
)
message_reaction
instance-attribute
¶
message_reaction = StandardMaxEventObserver(
self, "MESSAGE_REACTION", type_of_update=EmojiReaction
)
message_added_reaction
instance-attribute
¶
message_added_reaction = EmojiReactionAddObserver(
self,
"MESSAGE_ADDED_REACTION",
type_of_update=EmojiReaction,
)
message_deleted_reaction
instance-attribute
¶
message_deleted_reaction = EmojiReactionRemoveObserver(
self,
"MESSAGE_DELETED_REACTION",
type_of_update=EmojiReaction,
)
error
instance-attribute
¶
error = StandardMaxEventObserver(
self, "ERROR", type_of_update=ErrorEvent
)
events
instance-attribute
¶
events: dict[str, StandardMaxEventObserver[Any]] = {
"EDITED": edited_message,
"REPLY": reply_to_message,
"FORWARD": forward_message,
"REMOVED": message_removed,
"USER": message,
"MESSAGE_ADDED_REACTION": message_added_reaction,
"MESSAGE_DELETED_REACTION": message_deleted_reaction,
"MESSAGE_REACTION": message_reaction,
"ERROR": error,
}
chain_head
property
¶
chain_head: Generator[Router, None, None]
Chain head.
:yields: Items produced by the iterator. :ytype: Generator['Router', None, None]
chain_tail
property
¶
chain_tail: Generator[Router, None, None]
Chain tail.
:yields: Items produced by the iterator. :ytype: Generator['Router', None, None]
parent_router
property
writable
¶
parent_router: Optional[Router]
Parent router.
Returns:
| Type | Description |
|---|---|
Optional['Router']
|
The resulting Optional['Router'] value. |
include_routers ¶
include_routers(*routers: Router) -> None
Attach multiple child routers at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
routers
|
Router
|
Routers to attach. |
()
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested action cannot be completed. |
Source code in src/pyromax/dispatcher/Router.py
154 155 156 157 158 159 160 161 162 163 164 165 166 | |
include_router ¶
include_router(router: Router) -> Router
Attach another router as a child router.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
router
|
Router
|
Router to attach. |
required |
Returns:
| Type | Description |
|---|---|
'Router'
|
The attached router. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested action cannot be completed. |
Source code in src/pyromax/dispatcher/Router.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
notify
async
¶
notify(
update: MaxObject,
data: dict[Any, Any] | None = None,
event_types: list[str] | None = None,
) -> Any
Propagate an update through handlers and child routers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
update
|
MaxObject
|
Incoming update object. |
required |
data
|
dict[Any, Any] | None
|
Context data available to handlers. |
None
|
event_types
|
list[str] | None
|
keys of Router.events |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Any if the update was handled, otherwise UNHANDLED. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data cannot be None. |
Source code in src/pyromax/dispatcher/Router.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |