import asyncio import logging from aiogram.exceptions import TelegramNetworkError from aiohttp import ClientConnectionError from aiogram import F from aiogram.types import Message from aiogram.dispatcher.event.bases import SkipHandler from bot.bot import dp, bot, setup_proxy from bot.routers import moderation, layout, weather, ai, voice, yadisk, dialogue from bot.utils.database import init_db, save_chat_user from bot.utils.ai_client import start_model_health_check from bot.utils.logging_config import setup_logging from bot.utils.voice import preload_model from bot.setup_commands import setup_bot_commands from config import PROXY_ENABLED setup_logging() logger = logging.getLogger(__name__) # Router order matters: moderation first so it can delete messages before others process them. dp.include_router(moderation.router) dp.include_router(layout.router) dp.include_router(weather.router) dp.include_router(ai.router) dp.include_router(voice.router) dp.include_router(yadisk.router) dp.include_router(dialogue.router) @dp.message(F.text) async def save_user_info(message: Message): try: await save_chat_user( message.from_user.id, message.chat.id, message.from_user.username, message.from_user.full_name or "", ) except Exception as exc: logger.warning("Failed to save chat user: %s", exc) raise SkipHandler async def main(): await init_db() logging.info("База данных инициализирована") await setup_bot_commands() health_check_task = asyncio.create_task(start_model_health_check()) try: if PROXY_ENABLED: await setup_proxy() else: me = await bot.me() logging.info("Бот запущен без прокси: @%s", me.username) except TelegramNetworkError: if PROXY_ENABLED: logging.error("Не могу соединиться с прокси сервером. Попробуй другой прокси.") else: logging.error("Не могу соединиться с Telegram. Проверь интернет-соединение.") health_check_task.cancel() return except ClientConnectionError: if PROXY_ENABLED: logging.error("Не могу соединиться с прокси сервером. Попробуй другой прокси.") else: logging.error("Не могу соединиться с Telegram. Проверь интернет-соединение.") health_check_task.cancel() return except Exception as exc: logging.error("Ошибка при запуске бота: %s", exc) health_check_task.cancel() return try: await preload_model() logging.info("Модель Whisper готова к работе") except Exception as exc: logging.error("Не удалось загрузить модель Whisper: %s", exc) health_check_task.cancel() return try: await dp.start_polling(bot) finally: health_check_task.cancel() try: await health_check_task except asyncio.CancelledError: pass if __name__ == "__main__": asyncio.run(main())