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:
nullmatawasoradesu 2023-08-13 17:59:38 +03:00 committed by GitHub
parent 068875534b
commit f87deea4fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 0 deletions

View 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)
)