add queue to bot

This commit is contained in:
Dmitry Afanasyev 2022-08-21 23:47:18 +03:00
parent 61d6826f24
commit 8d86eb9407
2 changed files with 5 additions and 28 deletions

View File

@ -75,7 +75,6 @@ async def office_home(
@dispatcher.message_handler(commands=['chatid'])
async def chat_id(message: types.Message) -> types.Message:
logger.info(message.chat.id)
return await bot.send_message(message.chat.id, message.chat.id)

View File

@ -25,7 +25,8 @@ from app.settings import (
queue = asyncio.Queue() # type: ignore
async def bot_startup() -> None:
async def on_startup(dp: Dispatcher) -> None:
logger.info("Start bot with webhook")
await bot.set_webhook(WEBHOOK_URL)
loop = asyncio.get_running_loop()
loop.create_task(get_updates_from_queue())
@ -33,16 +34,12 @@ async def bot_startup() -> None:
asyncio_schedule()
async def bot_shutdown() -> None:
async def on_shutdown(dp: Dispatcher) -> None:
logger.warning('Shutting down..')
# Remove webhook (not acceptable in some cases)
await bot.delete_webhook()
# Close DB connection (if used)
await dispatcher.storage.close()
await dispatcher.storage.wait_closed()
session = await bot.get_session()
if session and not session.closed:
await session.close()
@ -51,30 +48,11 @@ async def bot_shutdown() -> None:
logger.warning('Bye!')
async def aiogram_startup(dp: Dispatcher) -> None:
await bot_startup()
async def aiogram_shutdown(dp: Dispatcher) -> None:
await bot_shutdown()
async def on_startup_gunicorn(app: web.Application) -> None:
logger.info("Start bot with webhook")
await bot_startup()
async def on_shutdown_gunicorn(app: web.Application) -> None:
await bot_shutdown()
def bot_polling() -> None:
logger.info("Start bot in polling mode")
start_polling(
dispatcher=dispatcher,
skip_updates=True,
on_startup=aiogram_startup,
on_shutdown=aiogram_shutdown,
)
@ -103,8 +81,8 @@ async def get_updates_from_queue() -> None:
async def create_app() -> web.Application:
application = web.Application()
application.router.add_post(f'{WEBHOOK_PATH}/{API_TOKEN}', put_updates_on_queue)
application.on_startup.append(on_startup_gunicorn)
application.on_shutdown.append(on_shutdown_gunicorn)
application.on_startup.append(on_startup)
application.on_shutdown.append(on_shutdown)
return application