From e9915f7e906607a97775253c3d8aa962758c077e Mon Sep 17 00:00:00 2001 From: Doctor Date: Thu, 25 Apr 2024 11:25:40 +0300 Subject: [PATCH] Add aiogram integration to docs --- docs/code/integrations/aiogram_.py | 44 ++++++++++++++++++++++++++++++ docs/integrations/aiogram.md | 7 +++++ mkdocs.yml | 1 + 3 files changed, 52 insertions(+) create mode 100644 docs/code/integrations/aiogram_.py create mode 100644 docs/integrations/aiogram.md diff --git a/docs/code/integrations/aiogram_.py b/docs/code/integrations/aiogram_.py new file mode 100644 index 0000000..d5bbdef --- /dev/null +++ b/docs/code/integrations/aiogram_.py @@ -0,0 +1,44 @@ +import asyncio +from typing import Annotated + +from aiogram import Bot, Dispatcher, Router +from aiogram.filters import Command +from aiogram.types import Message +from benchmark.container import create_container + +from aioinject import Inject, Object +from aioinject.ext.aiogram import AioInjectMiddleware, inject + + +async def main() -> None: + dispatcher = Dispatcher() + + container = create_container() + container.register(Object(42)) + + router = Router() + + @router.message( + Command(commands=["start"]), + ) + @inject + async def start( + message: Message, + value: Annotated[int, Inject], + ) -> None: + await message.reply(f"Injected value is {value}") + + middleware = AioInjectMiddleware(container) + middleware.add_to_router(router) + + dispatcher.include_router(router) + + async with ( + container, + Bot(token="token-here") as bot, # noqa: S106 + ): + await dispatcher.start_polling(bot) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/docs/integrations/aiogram.md b/docs/integrations/aiogram.md new file mode 100644 index 0000000..5817d25 --- /dev/null +++ b/docs/integrations/aiogram.md @@ -0,0 +1,7 @@ +Aiogram integration is achieved with `AioInjectMiddleware`, which you +could register on individual observers or +on all observers in a router via `add_to_router` method: + +```python +--8<-- "docs/code/integrations/aiogram_.py" +``` diff --git a/mkdocs.yml b/mkdocs.yml index f76eede..2e9c1e4 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -8,6 +8,7 @@ nav: - Context manager dependencies: context-managers.md - Extensions: extensions.md - Integrations: + - Aiogram: integrations/aiogram.md - FastAPI: integrations/fastapi.md - Litestar: integrations/litestar.md - Strawberry GraphQL: integrations/strawberry-graphql.md