Some refactor. Some docstrings.

This commit is contained in:
Alexey Sokolov
2022-02-14 16:52:15 +03:00
parent e8b2730aac
commit 3cd506682f
3 changed files with 86 additions and 28 deletions

View File

@@ -1,34 +1,44 @@
import logging
from aiogram import Bot, Dispatcher
from aiogram.dispatcher.webhook import configure_app
from aiohttp import web
from aiohttp.web import Application
from config import config
from telegram import telegram_bot, telegram_dispatcher
from config import Config
from .routes import routes, TELEGRAM_WEBHOOK_ROUTE
TELEGRAM_WEBHOOK_URL = f'{config.base_url}{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
async def on_startup(_):
await telegram_bot.set_webhook(TELEGRAM_WEBHOOK_URL)
async def on_shutdown(_):
logging.warning('Shutting down')
await telegram_bot.delete_webhook()
await telegram_dispatcher.storage.close()
await telegram_dispatcher.storage.wait_closed()
session = await telegram_bot.get_session()
await session.close()
logging.warning('Bye!..')
app = web.Application()
app.add_routes(routes)
app.on_startup.append(on_startup)
app.on_shutdown.append(on_shutdown)
configure_app(telegram_dispatcher, app, TELEGRAM_WEBHOOK_ROUTE)
__all__ = ('app',)
__all__ = ('create_app',)