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 DIMiddleware, Depends

router = Router()


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


@router.message()
async def _(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)
    dp.message.middleware(DIMiddleware())  # register Dependency Injection middleware
    dp.run_polling(bot)


if __name__ == '__main__':
    main()

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-1.0.2.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file aiogram3_di-1.0.2.tar.gz.

File metadata

  • Download URL: aiogram3_di-1.0.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for aiogram3_di-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f641de0d0c0f50aa902f7393e4eddb610edd55d4791f833f547af5993dcc6a3e
MD5 ba04f949b98ebeefd897f6179956929a
BLAKE2b-256 f8e83b3d1c708dc5ca38d0ec5126d3105cdfb5bb46d54f75a85722fd24e14181

See more details on using hashes here.

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