separate bot init and webhook setting
All checks were successful
Build Docs / changes (push) Successful in 5s
Build Docs / build-docs (push) Has been skipped
Build Docs / deploy-docs (push) Has been skipped

This commit is contained in:
Alexander Kalinovsky
2025-04-14 20:12:14 +07:00
parent 0e6225e82f
commit b995abfc1e

View File

@@ -41,6 +41,9 @@ async def default_lifespan(app: "QBotApp"):
if app.lifespan_bot_init: if app.lifespan_bot_init:
await app.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 app.config.TELEGRAM_BOT_USERNAME = (await app.bot.get_me()).username
logger.info("qbot app started") logger.info("qbot app started")
@@ -77,6 +80,7 @@ class QBotApp(Generic[UserType, ConfigType], FastAPI):
] = None, ] = None,
lifespan: Lifespan[AppType] | None = None, lifespan: Lifespan[AppType] | None = None,
lifespan_bot_init: bool = True, lifespan_bot_init: bool = True,
lifespan_set_webhook: bool = True,
allowed_updates: list[str] | None = None, allowed_updates: list[str] | None = None,
**kwargs, **kwargs,
): ):
@@ -128,6 +132,7 @@ class QBotApp(Generic[UserType, ConfigType], FastAPI):
self.bot_commands = dict[str, BotCommand]() self.bot_commands = dict[str, BotCommand]()
self.lifespan_bot_init = lifespan_bot_init self.lifespan_bot_init = lifespan_bot_init
self.lifespan_set_webhook = lifespan_set_webhook
super().__init__(lifespan=default_lifespan, **kwargs) super().__init__(lifespan=default_lifespan, **kwargs)
@@ -169,6 +174,8 @@ class QBotApp(Generic[UserType, ConfigType], FastAPI):
language_code=None if locale == "default" else locale, language_code=None if locale == "default" else locale,
) )
async def set_webhook(self):
await self.bot.set_webhook( await self.bot.set_webhook(
url=f"{self.config.TELEGRAM_WEBHOOK_URL}/telegram/webhook", url=f"{self.config.TELEGRAM_WEBHOOK_URL}/telegram/webhook",
drop_pending_updates=True, drop_pending_updates=True,
@@ -176,6 +183,7 @@ class QBotApp(Generic[UserType, ConfigType], FastAPI):
secret_token=self.config.TELEGRAM_WEBHOOK_AUTH_KEY, secret_token=self.config.TELEGRAM_WEBHOOK_AUTH_KEY,
) )
async def show_form( async def show_form(
self, self,
app_state: State, app_state: State,