-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.py
76 lines (57 loc) · 2.32 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import asyncio
import logging
from aiogram.dispatcher.webhook.aiohttp_server import (
SimpleRequestHandler,
setup_application,
)
from aiohttp import web
from aiogram import Bot, Dispatcher
from aiogram.types.input_file import FSInputFile
from aiogram.client.session.aiohttp import AiohttpSession
import settings
from handlers import introduction, buy_service, create_bill, profile, user_agreement
from expiration_notifier.manager import ExpirationManager
from expiration_notifier.notifier import TgBotNotifier
async def notify(bot: Bot):
try:
notifiers = [TgBotNotifier(bot)]
await ExpirationManager(notifiers).run()
except Exception as exc:
logging.error("Ошибка ExpirationManager", exc_info=exc)
async def on_startup(dispatcher: Dispatcher, bot: Bot):
await bot.set_webhook(
f"{settings.BASE_URL}{settings.BOT_PATH}",
certificate=FSInputFile(settings.CERTIFICATE_PATH),
ip_address=settings.PUBLIC_IP,
drop_pending_updates=True,
)
webhook = await bot.get_webhook_info()
print("======== SET WEBHOOK ========")
print(webhook)
# Запускаем проверку подключений и отправку уведомлений
asyncio.get_event_loop().create_task(notify(bot))
async def on_shutdown(dispatcher: Dispatcher, bot: Bot):
webhook = await bot.delete_webhook()
print("======== DELETE WEBHOOK ======== ->", webhook)
def add_routes(dispatcher: Dispatcher):
dispatcher.include_router(introduction.router)
dispatcher.include_router(buy_service.router)
dispatcher.include_router(profile.router)
dispatcher.include_router(create_bill.router)
dispatcher.include_router(user_agreement.router)
def main():
session = AiohttpSession()
bot_settings = {"session": session, "parse_mode": "HTML"}
bot = Bot(token=settings.TOKEN, **bot_settings)
dp = Dispatcher()
dp.startup.register(on_startup)
dp.shutdown.register(on_shutdown)
# Добавляем роуты
add_routes(dp)
app = web.Application()
SimpleRequestHandler(dispatcher=dp, bot=bot).register(app, path=settings.BOT_PATH)
setup_application(app, dp, bot=bot)
web.run_app(app, host=settings.WEB_SERVER_HOST, port=settings.WEB_SERVER_PORT)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()