mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-09 09:22:03 +00:00
Add ctx module. Aliases for keys in execution context.
This commit is contained in:
parent
46d6f0641c
commit
68a4a7a4aa
1 changed files with 39 additions and 0 deletions
39
aiogram/dispatcher/ctx.py
Normal file
39
aiogram/dispatcher/ctx.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
from . import Bot
|
||||||
|
from .. import types
|
||||||
|
from ..dispatcher import Dispatcher, FSMContext, MODE, UPDATE_OBJECT
|
||||||
|
from ..utils import context
|
||||||
|
|
||||||
|
|
||||||
|
def _get(key, default=None, no_error=False):
|
||||||
|
result = context.get_value(key, default)
|
||||||
|
if not no_error and result is None:
|
||||||
|
raise RuntimeError(f"Context is not configured for '{key}'")
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def get_bot() -> Bot:
|
||||||
|
return _get('bot')
|
||||||
|
|
||||||
|
|
||||||
|
def get_dispatcher() -> Dispatcher:
|
||||||
|
return _get('dispatcher')
|
||||||
|
|
||||||
|
|
||||||
|
def get_update() -> types.Update:
|
||||||
|
return _get(UPDATE_OBJECT)
|
||||||
|
|
||||||
|
|
||||||
|
def get_mode() -> str:
|
||||||
|
return _get(MODE, 'unknown')
|
||||||
|
|
||||||
|
|
||||||
|
def get_chat() -> int:
|
||||||
|
return _get('chat', no_error=True)
|
||||||
|
|
||||||
|
|
||||||
|
def get_user() -> int:
|
||||||
|
return _get('user', no_error=True)
|
||||||
|
|
||||||
|
|
||||||
|
def get_state() -> FSMContext:
|
||||||
|
return get_dispatcher().current_state()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue