mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Added a section on Dependency Injection technology in documentation (#1253)
* Added a section on Dependency Injection technology in documentation * Added changelog * Edited comments & titles
This commit is contained in:
parent
068875534b
commit
f87deea4fb
3 changed files with 90 additions and 0 deletions
33
examples/context_addition_from_filter.py
Normal file
33
examples/context_addition_from_filter.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from typing import Any, Dict, Optional, Union
|
||||
|
||||
from aiogram import Router
|
||||
from aiogram.filters import Filter
|
||||
from aiogram.types import Message, User
|
||||
|
||||
|
||||
router = Router(name=__name__)
|
||||
|
||||
|
||||
class HelloFilter(Filter):
|
||||
def __init__(self, name: Optional[str] = None) -> None:
|
||||
self.name = name
|
||||
|
||||
async def __call__(
|
||||
self,
|
||||
message: Message,
|
||||
event_from_user: User
|
||||
# Filters also can accept keyword parameters like in handlers
|
||||
) -> Union[bool, Dict[str, Any]]:
|
||||
if message.text.casefold() == "hello":
|
||||
# Returning a dictionary that will update the context data
|
||||
return {"name": event_from_user.mention_html(name=self.name)}
|
||||
return False
|
||||
|
||||
|
||||
@router.message(HelloFilter())
|
||||
async def my_handler(
|
||||
message: Message, name: str # Now we can accept "name" as named parameter
|
||||
) -> Any:
|
||||
return message.answer(
|
||||
"Hello, {name}!".format(name=name)
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue