pyTelegramBotAPI

Though it is not required, you can use dishka-pyTelegramBotAPI integration. It features:

  • automatic REQUEST scope management using middleware

  • passing dishka.integrations.telebot.TelebotEvent object as a context data to providers for telegram events (update object fields)

  • injection of dependencies into handler function using decorator.

Only sync handlers are supported.

How to use

  1. Import

from dishka.integrations.telebot import (
    FromDishka,
    inject,
    setup_dishka,
    TelebotProvider,
    TelebotEvent,
)
from dishka import make_async_container, Provider, provide, Scope
  1. Create provider. You can use dishka.integrations.telebot.TelebotEvent as a factory parameter to access on REQUEST-scope

class YourProvider(Provider):
    @provide(scope=Scope.REQUEST)
    def create_x(self, event: TelebotEvent) -> X:
         ...
  1. Mark those of your handlers parameters which are to be injected with FromDishka[] and decorate them using @inject

@bot.message()
@inject
def start(
    message: Message,
    gateway: FromDishka[Gateway],
):
  1. (optional) Use TelebotProvider() when creating container if you are going to use dishka.integrations.telebot.TelebotEvent in providers

container = make_async_container(YourProvider(), TelebotProvider())
  1. Setup dishka integration.

setup_dishka(container=container, bot=bot)