Skip to main content

Dependency Injection implementation for aiogram 3.

Project description

Example of usage

import logging
from os import getenv
from typing import Annotated

from aiogram import Router, Bot, Dispatcher
from aiogram.types import Message, User
from aiogram3_di import setup_di, Depends

router = Router()


def get_user_full_name(event_from_user: User) -> str:
    return event_from_user.full_name


@router.message()
async def start(
        message: Message, full_name: Annotated[str, Depends(get_user_full_name)]
) -> None:
    await message.answer(f"Hi {full_name}")


def main() -> None:
    logging.basicConfig(level=logging.INFO)

    bot = Bot(token=getenv("BOT_TOKEN"))

    dp = Dispatcher()

    dp.include_router(router)

    setup_di(dp)

    dp.run_polling(bot)


if __name__ == "__main__":
    main()

Handler Dependencies.

You can use Depends in the flags parameter of the handler, for example:

flags={"dependencies": [Depends(verify_user)]}

Details

It is inspired by FastAPI.

If you define a normal def, your function will be called in a different thread.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiogram3_di-2.0.0.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

aiogram3_di-2.0.0-py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page