diff --git a/config.py b/config.py index bd7479a..cef379c 100644 --- a/config.py +++ b/config.py @@ -52,6 +52,9 @@ class Settings(BaseSettings): DATA_DIR: str = "/data" TG_PERSISTENCE_DIR: str = "" + # Telegram webhook — leave empty to use polling mode + WEBHOOK_BASE_URL: str = "" + # BetterBot — site directories SITE_DIR: str = "/site" MEMORAIZ_DIR: str = "/memoraiz" diff --git a/main.py b/main.py index 3f5ac98..d8ef5a3 100644 --- a/main.py +++ b/main.py @@ -584,9 +584,13 @@ async def startup() -> None: # invoke it explicitly to register bot commands. if tg_app.post_init: await tg_app.post_init(tg_app) - base_url = f"https://code.bytesizeprotip.com/telegram/webhook/{webhook_secret()}" - await tg_app.bot.set_webhook(base_url, drop_pending_updates=True) - logger.info("Telegram webhook registered (dropped pending updates) at %s", base_url) + if settings.WEBHOOK_BASE_URL: + webhook_url = f"{settings.WEBHOOK_BASE_URL}/telegram/webhook/{webhook_secret()}" + await tg_app.bot.set_webhook(webhook_url, drop_pending_updates=True) + logger.info("Telegram webhook registered (dropped pending updates) at %s", webhook_url) + else: + await tg_app.updater.start_polling(drop_pending_updates=True) + logger.info("Telegram polling started (no WEBHOOK_BASE_URL configured)") # Start event polling background task (T027) import asyncio @@ -597,6 +601,8 @@ async def startup() -> None: @app.on_event("shutdown") async def shutdown() -> None: if tg_app: + if tg_app.updater and tg_app.updater.running: + await tg_app.updater.stop() await tg_app.stop() await tg_app.shutdown() await copilot.stop()