From f70766212536f9f95e532ce196673c5b2d1c0116 Mon Sep 17 00:00:00 2001 From: machka-pasla Date: Mon, 25 Aug 2025 13:24:30 +0300 Subject: [PATCH] Refactor bot initialization and routing logic to enforce webhook mode requirement - Updated the bot's startup logic to require a configured WEBHOOK_BASE_URL, exiting if not set, and logging appropriate error messages. - Simplified the decision-making process for running the AIOHTTP server, ensuring it only runs in webhook mode. - Enhanced the router configuration to filter updates for private chats, improving message handling security. --- bot/main_bot.py | 58 +++++++++++++++---------------------------------- bot/routers.py | 6 ++++- 2 files changed, 22 insertions(+), 42 deletions(-) diff --git a/bot/main_bot.py b/bot/main_bot.py index 8b7955a..86981b8 100644 --- a/bot/main_bot.py +++ b/bot/main_bot.py @@ -107,10 +107,10 @@ async def on_startup_configured(dispatcher: Dispatcher): "STARTUP: Skipped setting Telegram webhook due to security or configuration error." ) else: - logging.info( - "STARTUP: WEBHOOK_BASE_URL not set in environment. Running in polling mode and clearing any existing webhook." + logging.error( + "STARTUP: WEBHOOK_BASE_URL not set in environment. Webhook mode is required. Exiting." ) - await bot.delete_webhook(drop_pending_updates=True) + raise SystemExit("WEBHOOK_BASE_URL is required. Polling mode is disabled.") if settings.SUBSCRIPTION_MINI_APP_URL: try: @@ -271,54 +271,30 @@ async def run_bot(settings_param: Settings): await register_all_routers(dp, settings_param) tg_webhook_base = settings_param.WEBHOOK_BASE_URL - yk_webhook_base = settings_param.WEBHOOK_BASE_URL - should_run_aiohttp_server = bool(tg_webhook_base) or ( - bool(yk_webhook_base) and bool(settings_param.yookassa_webhook_path) - ) - - telegram_uses_webhook_mode = bool(tg_webhook_base) - run_telegram_polling = not telegram_uses_webhook_mode + # Webhook mode is now required - exit if not configured + if not tg_webhook_base: + logging.error("WEBHOOK_BASE_URL is required. Polling mode is disabled. Exiting.") + await dp.emit_shutdown() + raise SystemExit("WEBHOOK_BASE_URL is required. Polling mode is disabled.") logging.info(f"--- Bot Run Mode Decision ---") - logging.info( - f"Configured WEBHOOK_BASE_URL: '{tg_webhook_base}' -> Telegram Webhook Mode: {telegram_uses_webhook_mode}" - ) - logging.info( - f"YooKassa webhook path: '{settings_param.yookassa_webhook_path}'" - ) - logging.info(f"Decision: Run AIOHTTP server: {should_run_aiohttp_server}") - logging.info(f"Decision: Run Telegram Polling: {run_telegram_polling}") + logging.info(f"Configured WEBHOOK_BASE_URL: '{tg_webhook_base}' -> Webhook Mode: ENABLED") + logging.info(f"YooKassa webhook path: '{settings_param.yookassa_webhook_path}'") + logging.info(f"Decision: Run AIOHTTP server: ENABLED (required for webhooks)") logging.info(f"--- End Bot Run Mode Decision ---") web_app_runner = None main_tasks = [] - if should_run_aiohttp_server: - async def web_server_task(): - await build_and_start_web_app(dp, bot, settings_param, local_async_session_factory) + # Only run AIOHTTP server for webhook mode + async def web_server_task(): + await build_and_start_web_app(dp, bot, settings_param, local_async_session_factory) - main_tasks.append(asyncio.create_task(web_server_task(), name="AIOHTTPServerTask")) + main_tasks.append(asyncio.create_task(web_server_task(), name="AIOHTTPServerTask")) - if run_telegram_polling: - logging.info("Starting bot in Telegram Polling mode...") - main_tasks.append( - asyncio.create_task( - dp.start_polling(bot, allowed_updates=dp.resolve_used_update_types()), - name="TelegramPollingTask", - ) - ) - - if not main_tasks: - logging.error( - "Bot is not configured for any run mode (neither Webhook nor Polling). Exiting." - ) - await dp.emit_shutdown() - return - - logging.info( - f"Starting bot with main tasks: {[task.get_name() for task in main_tasks]}" - ) + logging.info("Starting bot in Webhook mode with AIOHTTP server...") + logging.info(f"Starting bot with main tasks: {[task.get_name() for task in main_tasks]}") try: await asyncio.gather(*main_tasks) diff --git a/bot/routers.py b/bot/routers.py index c79b963..fdcb716 100644 --- a/bot/routers.py +++ b/bot/routers.py @@ -1,4 +1,4 @@ -from aiogram import Router +from aiogram import Router, F from bot.handlers.user import user_router_aggregate from bot.handlers import inline_mode @@ -10,6 +10,10 @@ from config.settings import Settings def build_root_router(settings: Settings) -> Router: root = Router(name="root") + # Allow all updates only in private chats (messages, callback queries, etc.) + root.message.filter(F.chat.type == "private") + root.callback_query.filter(F.message.chat.type == "private") + # Public routers root.include_router(user_router_aggregate) root.include_router(inline_mode.router)