-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (50 loc) · 1.94 KB
/
Copy pathmain.py
File metadata and controls
63 lines (50 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.enums import ParseMode
from aiogram.client.default import DefaultBotProperties
from app import config, utils, db, handlers, scheduler
bot = Bot(
token=config.settings.BOT_TOKEN,
default=DefaultBotProperties(parse_mode=ParseMode.HTML, disable_notification=True, link_preview_is_disabled=True)
)
log = config.setup_logging(logging.getLogger(__name__))
async def on_update_error(event: types.ErrorEvent) -> bool:
"""Catch any unhandled error so a single bad update can't kill polling."""
exc = event.exception
log.error(f"❌ Необработанная ошибка обновления: {exc}", exc_info=exc)
await utils.notify_developers(
f"❌ Update error: {type(exc).__name__}: {exc}",
dedupe_key=f"update:{type(exc).__name__}",
)
return True
async def main():
config.validate_runtime()
dp = Dispatcher()
dp.include_router(handlers.router)
dp.errors.register(on_update_error)
await db.init_db()
await bot.set_my_commands(
[
types.BotCommand(command="start", description="Open Quoto menu"),
types.BotCommand(command="privacy", description="User agreement & privacy"),
]
)
# Инициализация планировщика
sched = scheduler.setup_scheduler(bot)
sched.start()
try:
log.info("🟢 Бот запущен!")
await dp.start_polling(bot)
finally:
sched.shutdown(wait=False)
await bot.session.close()
log.info("🔴 Бот остановлен!")
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
log.info("🔴 Скрипт остановлен")
except Exception as e:
log.critical(f"Критическая ошибка: {e}")
asyncio.run(utils.notify_developers(f"❌ Критическая ошибка в main.py: {e}"))