-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
58 lines (43 loc) · 1.46 KB
/
main.py
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
from aiogram import Dispatcher
from aiogram import executor
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram_dialog import DialogRegistry
from loguru import logger
from tortoise import Tortoise
from app import commands
from app import config
from app.bot import bot
from app.bot import handlers
from app.bot import middlewares
from app.bot.dialogs import register_dialogs
from app.database import TORTOISE_ORM
from app.misc import set_commands
from app.misc.sentry import init_sentry
from app.tasks import scheduler
# Storage and dispatcher instances
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)
registry = DialogRegistry(dp)
async def startup(dispatcher: Dispatcher):
logger.info("Registering dialogs")
await register_dialogs(registry)
# Setup handlers
logger.info("Configuring handlers...")
handlers.setup(dispatcher)
logger.info("Configuring Database")
await Tortoise.init(TORTOISE_ORM)
await Tortoise.generate_schemas()
# Set command hints
await set_commands(dispatcher, commands)
middlewares.setup(dispatcher)
logger.info("Start scheduler")
scheduler.start()
init_sentry()
logger.info("Start polling")
async def shutdown(dispatcher: Dispatcher):
await Tortoise.close_connections()
if __name__ == "__main__":
# Start long-polling mode
executor.start_polling(
dp, on_startup=startup, on_shutdown=shutdown, skip_updates=config.EXECUTOR_SKIP_UPDATES
)