-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
56 lines (47 loc) · 1.72 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
56
import asyncio
import logging
from aiogram import Bot, Dispatcher
from aiogram.exceptions import TelegramNetworkError, TelegramRetryAfter
from aiogram.fsm.storage.redis import RedisStorage
from config import settings
from data.postgresql_connect import create_db, session_factory
from handlers.commands_handler import command_router
from handlers.currency_handlers import currency_router
from handlers.empty_handler import empty_handler_router
from handlers.gigachat_hendlers import gigachat_router
from handlers.weather_handlers import weather_router
from middlewares.db_middlewares import DataBaseMiddleware
from utils.commands import register_commands
from utils.logger_settings import setup_logging
logger = logging.getLogger(__name__)
async def bot_connect():
storage = RedisStorage.from_url(settings.REDIS_URL)
bot = Bot(token=settings.TELEGRAM_TOKEN, parse_mode="HTML")
dp = Dispatcher(storage=storage)
dp.include_routers(
command_router,
weather_router,
currency_router,
gigachat_router,
empty_handler_router, # этот роутер всегда должен быть последним
)
dp.update.middleware(DataBaseMiddleware(session_pool=session_factory))
await create_db()
try:
await bot.delete_webhook(drop_pending_updates=True)
await dp.start_polling(bot)
await register_commands(bot)
except TelegramNetworkError as error:
logger.error(error)
finally:
await bot.close()
def main():
setup_logging()
try:
asyncio.run(bot_connect())
except TelegramRetryAfter as error:
logger.error(error)
except KeyboardInterrupt as error:
logger.error(error)
if __name__ == '__main__':
main()