-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
55 lines (42 loc) · 1.47 KB
/
bot.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
import asyncio
import logging
from aiogram import Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.contrib.fsm_storage.redis import RedisStorage
from agbot.config import load_config
from agbot.filters.role import RoleFilter, AdminFilter
from agbot.handlers.default import register_default
from agbot.handlers.admin import register_admin
from agbot.middlewares.database import DatabaseSessionMiddleware
from agbot.middlewares.role import RoleMiddleware
from agbot.services.db_conn import session, engine, base
from agbot.services.orm import BaseModel
logger = logging.getLogger(__name__)
async def main():
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
)
logger.error("Starting bot")
config = load_config()
if config.tg_bot.use_redis:
storage = RedisStorage()
else:
storage = MemoryStorage()
bot = Bot(token=config.tg_bot.token)
dp = Dispatcher(bot, storage=storage)
base.metadata.create_all(engine)
BaseModel.set_session(session)
dp.middleware.setup(DatabaseSessionMiddleware(session))
dp.middleware.setup(RoleMiddleware(config.tg_bot.admin_id))
dp.filters_factory.bind(RoleFilter)
dp.filters_factory.bind(AdminFilter)
register_admin(dp)
register_default(dp)
# start
try:
await dp.start_polling()
finally:
await bot.close()
if __name__ == '__main__':
asyncio.run(main())