From b995abfc1e6ce66a19a2b46416a853cc5d5e06ad Mon Sep 17 00:00:00 2001 From: Alexander Kalinovsky Date: Mon, 14 Apr 2025 20:12:14 +0700 Subject: [PATCH] separate bot init and webhook setting --- src/quickbot/main.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/quickbot/main.py b/src/quickbot/main.py index f9a92cd..b60ec1b 100644 --- a/src/quickbot/main.py +++ b/src/quickbot/main.py @@ -41,6 +41,9 @@ async def default_lifespan(app: "QBotApp"): if app.lifespan_bot_init: await app.bot_init() + if app.lifespan_set_webhook: + await app.set_webhook() + app.config.TELEGRAM_BOT_USERNAME = (await app.bot.get_me()).username logger.info("qbot app started") @@ -77,6 +80,7 @@ class QBotApp(Generic[UserType, ConfigType], FastAPI): ] = None, lifespan: Lifespan[AppType] | None = None, lifespan_bot_init: bool = True, + lifespan_set_webhook: bool = True, allowed_updates: list[str] | None = None, **kwargs, ): @@ -128,6 +132,7 @@ class QBotApp(Generic[UserType, ConfigType], FastAPI): self.bot_commands = dict[str, BotCommand]() self.lifespan_bot_init = lifespan_bot_init + self.lifespan_set_webhook = lifespan_set_webhook super().__init__(lifespan=default_lifespan, **kwargs) @@ -169,6 +174,8 @@ class QBotApp(Generic[UserType, ConfigType], FastAPI): language_code=None if locale == "default" else locale, ) + + async def set_webhook(self): await self.bot.set_webhook( url=f"{self.config.TELEGRAM_WEBHOOK_URL}/telegram/webhook", drop_pending_updates=True, @@ -176,6 +183,7 @@ class QBotApp(Generic[UserType, ConfigType], FastAPI): secret_token=self.config.TELEGRAM_WEBHOOK_AUTH_KEY, ) + async def show_form( self, app_state: State,