-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (43 loc) · 1.79 KB
/
Copy pathmain.py
File metadata and controls
59 lines (43 loc) · 1.79 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
import asyncio
import logging
import os
from aiogram import Bot, Dispatcher
from aiogram.exceptions import TelegramBadRequest
from core.config import settings
from core.db.database import SessionLocal, init_models
from core.handlers import basic, core
from core.message_texts import start_bot_message, stop_bot_message
from core.middlewares.db_middleware import DbSessionMiddleware
from core.utils.commands import set_commands
async def on_startup(bot: Bot):
await set_commands(bot=bot)
try:
await bot.send_message(chat_id=settings.admin_id, text=start_bot_message)
except TelegramBadRequest:
logging.warning("Could not notify admin on startup (chat not found). Send /start to the bot first.")
async def on_shutdown(bot: Bot):
try:
await bot.send_message(chat_id=settings.admin_id, text=stop_bot_message)
except TelegramBadRequest:
logging.warning("Could not notify admin on shutdown (chat not found).")
async def main():
log_format = "%(asctime)s - [%(levelname)s] - %(name)s - (%(filename)s).%(funcName)s(%(lineno)d) - %(message)s"
logging.basicConfig(level=logging.INFO, format=log_format)
os.makedirs("logs", exist_ok=True)
file_handler = logging.FileHandler("logs/anime_logs", mode="a")
file_handler.setFormatter(logging.Formatter(log_format))
logging.getLogger().addHandler(file_handler)
await init_models()
bot = Bot(token=settings.bot_token)
dp = Dispatcher()
dp.update.middleware(DbSessionMiddleware(session_pool=SessionLocal))
dp.startup.register(on_startup)
dp.shutdown.register(on_shutdown)
dp.include_router(basic.router)
dp.include_router(core.router)
try:
await dp.start_polling(bot)
finally:
await bot.session.close()
if __name__ == "__main__":
asyncio.run(main())