Skip to content

Commit

Permalink
Add aiogram integration to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirVondukr committed Apr 25, 2024
1 parent 42c17a7 commit e9915f7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/code/integrations/aiogram_.py
Original file line number Diff line number Diff line change
@@ -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())
7 changes: 7 additions & 0 deletions docs/integrations/aiogram.md
Original file line number Diff line number Diff line change
@@ -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"
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e9915f7

Please sign in to comment.