-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
76 lines (57 loc) · 1.87 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
67
68
69
70
71
72
73
74
75
76
import os
import asyncio
import logging
import sys
from aiogram import Bot, Dispatcher
from aiogram.types import BotCommand
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from cachetools import TTLCache
from core.handlers.user import register_user
from core.handlers.individual_bot import register_individual_bot
from core.handlers.command_bot import register_command_bot
from core.middlewares.auth import AuthMiddleware
from core.utils.variables import scheduler
from config import load_config
logger = logging.getLogger(__name__)
async def set_commands(bot: Bot):
commands = [
BotCommand(command="/start", description="Открыть меню"),
]
await bot.set_my_commands(commands)
async def main():
if os.path.isfile('bot.log'):
os.remove('bot.log')
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
encoding="UTF-8",
handlers=[
logging.FileHandler("bot.log"),
logging.StreamHandler()
]
)
logger.info("Starting bot")
config = load_config("config.ini")
storage = MemoryStorage()
bot = Bot(token=config.tg_bot.token)
await set_commands(bot)
dp = Dispatcher(bot, storage=storage)
dp["context"] = {
"users": TTLCache(maxsize=sys.maxsize, ttl=60*60*24), # на один день
}
dp.middleware.setup(AuthMiddleware(dp))
scheduler.start()
register_user(dp)
register_individual_bot(dp)
register_command_bot(dp)
try:
await dp.start_polling(allowed_updates=["message", "callback_query"])
finally:
await dp.storage.close()
await dp.storage.wait_closed()
await bot.session.close()
if __name__ == "__main__":
try:
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
logger.info("Bot stopped!")