-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (50 loc) · 1.95 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
59
60
61
62
63
64
65
66
import asyncio
import locale
import logging
import sys
from os import getenv
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
import entities
from context import Context
from entities.base import Base
from middlewares.session_creator import SessionCreatorMiddleware
from middlewares.state_data_provider import StateDataProvider
from routers import create_remind, commands, remind_list, manage_groups, voice_recognition
# Для верного отображения дат
locale.setlocale(locale.LC_TIME, 'ru_RU')
TOKEN = getenv("BOT_TOKEN")
bot = Bot(TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
dp = Dispatcher()
# Создали планировщик
scheduler = AsyncIOScheduler()
# setup data storage
url = 'sqlite:///db.sqlite'
scheduler.add_jobstore('sqlalchemy', url=url)
engine = create_engine(url, echo=False)
db_session: sessionmaker[Session] = sessionmaker(bind=engine)
# Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
async def main() -> None:
# Запуск планировщика тут!!
scheduler.start()
# задаём контекст для бота
context = Context(db_session, scheduler)
dp["context"] = context
dp.update.middleware.register(SessionCreatorMiddleware())
dp.update.middleware.register(StateDataProvider())
# подключаем роутеры
dp.include_router(commands.router)
dp.include_router(manage_groups.router)
dp.include_router(create_remind.router)
dp.include_router(remind_list.router)
dp.include_router(voice_recognition.router)
# Запускаем бота
await dp.start_polling(bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())