mirror of
https://github.com/Balshgit/mosgortrans.git
synced 2025-09-11 13:00:40 +03:00
add async queue
This commit is contained in:
parent
50bdc01b9d
commit
d9b4556795
14
app/main.py
14
app/main.py
@ -2,6 +2,7 @@ import asyncio
|
|||||||
import sys
|
import sys
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from aiogram import Bot, Dispatcher
|
from aiogram import Bot, Dispatcher
|
||||||
from aiogram.types import Update
|
from aiogram.types import Update
|
||||||
@ -22,11 +23,14 @@ from app.settings import (
|
|||||||
WEBHOOK_URL,
|
WEBHOOK_URL,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
queue: Any = asyncio.Queue()
|
||||||
|
|
||||||
|
|
||||||
async def bot_startup() -> None:
|
async def bot_startup() -> None:
|
||||||
await bot.set_webhook(WEBHOOK_URL)
|
await bot.set_webhook(WEBHOOK_URL)
|
||||||
logger.info(f'Webhook set to {WEBHOOK_URL}'.replace(API_TOKEN, '{BOT_API_TOKEN}'))
|
logger.info(f'Webhook set to {WEBHOOK_URL}'.replace(API_TOKEN, '{BOT_API_TOKEN}'))
|
||||||
asyncio_schedule()
|
asyncio_schedule()
|
||||||
|
await worker()
|
||||||
|
|
||||||
|
|
||||||
async def bot_shutdown() -> None:
|
async def bot_shutdown() -> None:
|
||||||
@ -83,13 +87,17 @@ async def webhook(request: web.Request) -> web.Response:
|
|||||||
"""
|
"""
|
||||||
data = await request.json()
|
data = await request.json()
|
||||||
tg_update = Update(**data)
|
tg_update = Update(**data)
|
||||||
|
queue.put_nowait(tg_update)
|
||||||
|
return web.Response(status=HTTPStatus.ACCEPTED)
|
||||||
|
|
||||||
|
|
||||||
|
async def worker() -> None:
|
||||||
Dispatcher.set_current(dispatcher)
|
Dispatcher.set_current(dispatcher)
|
||||||
Bot.set_current(dispatcher.bot)
|
Bot.set_current(dispatcher.bot)
|
||||||
|
|
||||||
await dispatcher.process_update(tg_update)
|
while True:
|
||||||
|
update = await queue.get()
|
||||||
return web.Response(status=HTTPStatus.OK)
|
await dispatcher.process_update(update)
|
||||||
|
|
||||||
|
|
||||||
async def create_app() -> web.Application:
|
async def create_app() -> web.Application:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user