pyTelegramBotAPI¶
Though it is not required, you can use dishka-pyTelegramBotAPI integration. It features:
automatic REQUEST scope management using middleware
passing
dishka.integrations.telebot.TelebotEventobject 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¶
Import
from dishka.integrations.telebot import (
FromDishka,
inject,
setup_dishka,
TelebotProvider,
TelebotEvent,
)
from dishka import make_async_container, Provider, provide, Scope
Create provider. You can use
dishka.integrations.telebot.TelebotEventas a factory parameter to access on REQUEST-scope
class YourProvider(Provider):
@provide(scope=Scope.REQUEST)
def create_x(self, event: TelebotEvent) -> X:
...
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],
):
(optional) Use
TelebotProvider()when creating container if you are going to usedishka.integrations.telebot.TelebotEventin providers
container = make_async_container(YourProvider(), TelebotProvider())
Setup
dishkaintegration.
setup_dishka(container=container, bot=bot)