chore: run lint and prettifier
This commit is contained in:
+29
-55
@@ -1,44 +1,22 @@
|
||||
import logging
|
||||
import asyncio
|
||||
from typing import Dict, Any, Optional
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from aiogram import Bot, Dispatcher
|
||||
from aiogram.types import (MenuButtonDefault, MenuButtonWebApp, WebAppInfo, BotCommand)
|
||||
from aiogram.enums import ParseMode
|
||||
from aiogram.client.default import DefaultBotProperties
|
||||
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
|
||||
from aiogram.fsm.storage.memory import MemoryStorage
|
||||
from aiohttp import web
|
||||
from bot.services.panel_webhook_service import PanelWebhookService, panel_webhook_route
|
||||
from aiogram.types import BotCommand, MenuButtonDefault, MenuButtonWebApp, WebAppInfo
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from config.settings import Settings
|
||||
|
||||
from db.database_setup import init_db_connection
|
||||
|
||||
from bot.middlewares.i18n import I18nMiddleware, get_i18n_instance, JsonI18n
|
||||
from bot.middlewares.db_session import DBSessionMiddleware
|
||||
from bot.middlewares.ban_check_middleware import BanCheckMiddleware
|
||||
from bot.middlewares.action_logger_middleware import ActionLoggerMiddleware
|
||||
from bot.middlewares.profile_sync import ProfileSyncMiddleware
|
||||
from bot.app.controllers.dispatcher_controller import build_dispatcher
|
||||
from bot.app.factories.build_services import build_core_services
|
||||
from bot.app.web.web_server import build_and_start_web_app
|
||||
|
||||
from bot.routers import build_root_router
|
||||
|
||||
from bot.services.yookassa_service import YooKassaService
|
||||
from bot.services.panel_api_service import PanelApiService
|
||||
from bot.services.subscription_service import SubscriptionService
|
||||
from bot.services.tariff_worker import TariffTrafficWorker
|
||||
from bot.services.referral_service import ReferralService
|
||||
from bot.services.promo_code_service import PromoCodeService
|
||||
from bot.services.stars_service import StarsService
|
||||
from bot.services.crypto_pay_service import CryptoPayService, cryptopay_webhook_route
|
||||
|
||||
from bot.handlers.user import payment as user_payment_webhook_module
|
||||
from bot.handlers.admin.sync_admin import perform_sync
|
||||
from bot.middlewares.i18n import JsonI18n
|
||||
from bot.routers import build_root_router
|
||||
from bot.services.panel_api_service import PanelApiService
|
||||
from bot.services.tariff_worker import TariffTrafficWorker
|
||||
from bot.utils.message_queue import init_queue_manager
|
||||
from config.settings import Settings
|
||||
from db.database_setup import init_db_connection
|
||||
|
||||
|
||||
def redact_token(value: str, token: Optional[str]) -> str:
|
||||
@@ -62,7 +40,6 @@ async def on_startup_configured(dispatcher: Dispatcher):
|
||||
|
||||
logging.info("STARTUP: on_startup_configured executing...")
|
||||
|
||||
|
||||
telegram_webhook_url_to_set = settings.WEBHOOK_BASE_URL
|
||||
if telegram_webhook_url_to_set:
|
||||
full_telegram_webhook_url = (
|
||||
@@ -127,9 +104,7 @@ async def on_startup_configured(dispatcher: Dispatcher):
|
||||
)
|
||||
)
|
||||
await bot.set_chat_menu_button(menu_button=MenuButtonDefault())
|
||||
logging.info(
|
||||
"STARTUP: Mini app domain registered and default menu button restored."
|
||||
)
|
||||
logging.info("STARTUP: Mini app domain registered and default menu button restored.")
|
||||
except Exception:
|
||||
logging.exception("STARTUP: Failed to register mini app domain.")
|
||||
|
||||
@@ -140,8 +115,7 @@ async def on_startup_configured(dispatcher: Dispatcher):
|
||||
if settings.START_COMMAND_DESCRIPTION:
|
||||
bot_commands.insert(
|
||||
0,
|
||||
BotCommand(command="start",
|
||||
description=settings.START_COMMAND_DESCRIPTION),
|
||||
BotCommand(command="start", description=settings.START_COMMAND_DESCRIPTION),
|
||||
)
|
||||
await bot.set_my_commands(bot_commands)
|
||||
logging.info("STARTUP: bot command descriptions set.")
|
||||
@@ -159,20 +133,24 @@ async def on_startup_configured(dispatcher: Dispatcher):
|
||||
# Automatic sync on startup
|
||||
try:
|
||||
logging.info("STARTUP: Running automatic panel sync...")
|
||||
|
||||
|
||||
async with async_session_factory() as session:
|
||||
sync_result = await perform_sync(
|
||||
panel_service=panel_service,
|
||||
session=session,
|
||||
settings=settings,
|
||||
i18n_instance=i18n_instance
|
||||
i18n_instance=i18n_instance,
|
||||
)
|
||||
|
||||
|
||||
if sync_result.get("status") == "completed":
|
||||
logging.info(f"STARTUP: Automatic sync completed successfully. Details: {sync_result.get('details', 'N/A')}")
|
||||
logging.info(
|
||||
f"STARTUP: Automatic sync completed successfully. Details: {sync_result.get('details', 'N/A')}"
|
||||
)
|
||||
else:
|
||||
logging.warning(f"STARTUP: Automatic sync completed with issues. Status: {sync_result.get('status', 'unknown')}")
|
||||
|
||||
logging.warning(
|
||||
f"STARTUP: Automatic sync completed with issues. Status: {sync_result.get('status', 'unknown')}"
|
||||
)
|
||||
|
||||
except Exception:
|
||||
logging.exception("STARTUP: Failed to run automatic sync.")
|
||||
|
||||
@@ -239,9 +217,7 @@ async def on_shutdown_configured(dispatcher: Dispatcher):
|
||||
async def run_bot(settings_param: Settings):
|
||||
local_async_session_factory = init_db_connection(settings_param)
|
||||
if local_async_session_factory is None:
|
||||
logging.critical(
|
||||
"Failed to initialize database connection and session factory. Exiting."
|
||||
)
|
||||
logging.critical("Failed to initialize database connection and session factory. Exiting.")
|
||||
return
|
||||
dp, bot, extra = build_dispatcher(settings_param, local_async_session_factory)
|
||||
i18n_instance = extra["i18n_instance"]
|
||||
@@ -255,9 +231,7 @@ async def run_bot(settings_param: Settings):
|
||||
dp["bot_username"] = actual_bot_username
|
||||
logging.info(f"Bot username resolved: @{actual_bot_username}")
|
||||
else:
|
||||
logging.warning(
|
||||
"Bot username is empty; Telegram Login Widget will be unavailable."
|
||||
)
|
||||
logging.warning("Bot username is empty; Telegram Login Widget will be unavailable.")
|
||||
except Exception as e:
|
||||
logging.error(
|
||||
f"Failed to get bot info (e.g., for YooKassa default URL): {e}. Using fallback: {actual_bot_username}"
|
||||
@@ -287,8 +261,10 @@ async def run_bot(settings_param: Settings):
|
||||
# Wrap startup/shutdown handlers to satisfy aiogram event signature (no args passed)
|
||||
async def _on_startup_wrapper():
|
||||
await on_startup_configured(dp)
|
||||
|
||||
async def _on_shutdown_wrapper():
|
||||
await on_shutdown_configured(dp)
|
||||
|
||||
dp.startup.register(_on_startup_wrapper)
|
||||
dp.shutdown.register(_on_shutdown_wrapper)
|
||||
|
||||
@@ -302,11 +278,11 @@ async def run_bot(settings_param: Settings):
|
||||
await dp.emit_shutdown()
|
||||
raise SystemExit("WEBHOOK_BASE_URL is required. Polling mode is disabled.")
|
||||
|
||||
logging.info(f"--- Bot Run Mode Decision ---")
|
||||
logging.info("--- Bot Run Mode Decision ---")
|
||||
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 ---")
|
||||
logging.info("Decision: Run AIOHTTP server: ENABLED (required for webhooks)")
|
||||
logging.info("--- End Bot Run Mode Decision ---")
|
||||
|
||||
web_app_runner = None
|
||||
main_tasks = []
|
||||
@@ -336,9 +312,7 @@ async def run_bot(settings_param: Settings):
|
||||
try:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
logging.info(
|
||||
f"Task '{task.get_name()}' was cancelled successfully."
|
||||
)
|
||||
logging.info(f"Task '{task.get_name()}' was cancelled successfully.")
|
||||
except Exception as e_task_cancel:
|
||||
logging.error(
|
||||
f"Error during cancellation of task '{task.get_name()}': {e_task_cancel}",
|
||||
|
||||
Reference in New Issue
Block a user