-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
82 lines (67 loc) · 2.03 KB
/
main.py
File metadata and controls
82 lines (67 loc) · 2.03 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Main file for the bot"""
import os
import time
import asyncio
import logging
from aiogram import Bot, Dispatcher
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.fsm.storage.redis import RedisStorage
from app import middlewares, handlers
from app.database import create_sessionmaker
from app.utils import set_commands, load_config, schedule, payments
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logging.getLogger('aiogram.event').setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
async def main() -> None:
"""Main function for the bot"""
logger.info("Starting bot...")
config = load_config()
os.environ['TZ'] = config.bot.timezone
time.tzset()
logger.info('Set timesone to "%s"' % config.bot.timezone)
if config.bot.use_redis:
storage = RedisStorage.from_url(
'redis://%s:6379/%i' % (
config.redis.host,
config.redis.db,
),
)
else:
storage = MemoryStorage()
sessionmaker = await create_sessionmaker(config.db)
payment = (
payments.BasePayment() if not config.payments.enabled
else payments.PayOK(
config.payments.api_id,
config.payments.api_key,
config.payments.project_id,
config.payments.project_secret,
)
)
bot = Bot(
token=config.bot.token,
parse_mode="HTML",
)
dp = Dispatcher(storage=storage)
middlewares.setup(dp, sessionmaker)
handlers.setup(dp)
await set_commands(bot, config, sessionmaker)
await schedule.setup(bot, sessionmaker)
bot_info = await bot.me()
try:
await dp.start_polling(
bot,
config=config,
bot_info=bot_info,
payment=payment,
)
finally:
await dp.fsm.storage.close()
if __name__ == "__main__":
try:
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
logger.critical("Bot stopped")