DispelEvilBot/bot/__main__.py

47 lines
1.4 KiB
Python

"""Точка входа бота: python -m bot"""
from __future__ import annotations
import asyncio
import logging
from bot.bot_factory import create_bot, create_dispatcher
from bot.services.heartbeat import run_heartbeat
from bot.services.notifications import run_notifications
from common import __version__
from common.config import get_settings
from common.db.pool import close_pool, init_pool
from common.logging_config import setup_logging
logger = logging.getLogger(__name__)
async def main() -> None:
setup_logging(logging.INFO)
settings = get_settings()
logger.info("Запуск DispelEvilBot v%s (APP_ENV=%s)", __version__, settings.app_env)
await init_pool()
bot = create_bot()
dp = create_dispatcher()
notifications_task = asyncio.create_task(run_notifications(bot))
heartbeat_task = asyncio.create_task(run_heartbeat())
try:
await bot.delete_webhook(drop_pending_updates=True)
await dp.start_polling(bot, allowed_updates=dp.resolve_used_update_types())
finally:
notifications_task.cancel()
heartbeat_task.cancel()
await asyncio.gather(notifications_task, heartbeat_task, return_exceptions=True)
await close_pool()
await bot.session.close()
if __name__ == "__main__":
try:
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
logger.info("Бот остановлен")