Files
ltlnotifyer/web/__init__.py
2022-02-14 16:52:15 +03:00

45 lines
1.2 KiB
Python

import logging
from aiogram import Bot, Dispatcher
from aiogram.dispatcher.webhook import configure_app
from aiohttp.web import Application
from config import Config
from .routes import routes, TELEGRAM_WEBHOOK_ROUTE
def create_app(bot: Bot,
dispatcher: Dispatcher,
config: Config) -> Application:
"""
Create aiohttp application.
:param bot: telegram bot instance.
:param dispatcher: telegram dispatcher instance.
:param config: config object.
:return: aiohttp Appliction.
"""
telegram_webhook_url = f'{config.base_url}{TELEGRAM_WEBHOOK_ROUTE}'
async def on_startup(_):
await bot.set_webhook(telegram_webhook_url)
async def on_shutdown(_):
logging.warning('Shutting down')
await bot.delete_webhook()
await dispatcher.storage.close()
await dispatcher.storage.wait_closed()
session = await bot.get_session()
await session.close()
logging.warning('Bye!..')
app = Application()
app.add_routes(routes)
app.on_startup.append(on_startup)
app.on_shutdown.append(on_shutdown)
configure_app(dispatcher, app, TELEGRAM_WEBHOOK_ROUTE)
return app
__all__ = ('create_app',)