Correctly handle response into webhook (silent call)

This commit is contained in:
Alex Root Junior 2021-10-25 01:32:53 +03:00
parent a40d1703a2
commit 208134430f
3 changed files with 17 additions and 9 deletions

View file

@ -202,10 +202,11 @@ class Dispatcher(Router):
return await self.propagate_event(update_type=update_type, event=event, **kwargs)
@classmethod
async def _silent_call_request(cls, bot: Bot, result: TelegramMethod[Any]) -> None:
async def silent_call_request(cls, bot: Bot, result: TelegramMethod[Any]) -> None:
"""
Simulate answer into WebHook
:param bot:
:param result:
:return:
"""
@ -233,7 +234,7 @@ class Dispatcher(Router):
try:
response = await self.feed_update(bot, update, **kwargs)
if call_answer and isinstance(response, TelegramMethod):
await self._silent_call_request(bot=bot, result=response)
await self.silent_call_request(bot=bot, result=response)
return response is not UNHANDLED
except Exception as e:
@ -324,7 +325,7 @@ class Dispatcher(Router):
except Exception as e:
raise e
if isinstance(result, TelegramMethod):
asyncio.ensure_future(self._silent_call_request(bot=bot, result=result))
asyncio.ensure_future(self.silent_call_request(bot=bot, result=result))
try:
try:

View file

@ -118,11 +118,18 @@ class BaseRequestHandler(ABC):
"""
pass
async def _background_feed_update(self, bot: Bot, update: Dict[str, Any]) -> None:
result = await self.dispatcher.feed_raw_update(
bot=bot,
update=update,
)
if isinstance(result, TelegramMethod):
await self.dispatcher.silent_call_request(bot=bot, result=result)
async def _handle_request_background(self, bot: Bot, request: web.Request) -> web.Response:
asyncio.create_task(
self.dispatcher.feed_raw_update(
bot=bot,
update=await request.json(loads=bot.session.json_loads),
self._background_feed_update(
bot=bot, update=await request.json(loads=bot.session.json_loads)
)
)
return web.Response(status=200)

View file

@ -167,7 +167,7 @@ class TestDispatcher:
async def test_silent_call_request(self, bot: MockedBot, caplog):
dispatcher = Dispatcher()
bot.add_result_for(SendMessage, ok=False, error_code=400, description="Kaboom")
await dispatcher._silent_call_request(bot, SendMessage(chat_id=42, text="test"))
await dispatcher.silent_call_request(bot, SendMessage(chat_id=42, text="test"))
log_records = [rec.message for rec in caplog.records]
assert len(log_records) == 1
assert "Failed to make answer" in log_records[0]
@ -576,7 +576,7 @@ class TestDispatcher:
dispatcher.update.handlers.reverse()
with patch(
"aiogram.dispatcher.dispatcher.Dispatcher._silent_call_request",
"aiogram.dispatcher.dispatcher.Dispatcher.silent_call_request",
new_callable=CoroutineMock,
) as mocked_silent_call_request:
result = await dispatcher._process_update(bot=bot, update=Update(update_id=42))
@ -704,7 +704,7 @@ class TestDispatcher:
dispatcher.message.register(simple_message_handler)
with patch(
"aiogram.dispatcher.dispatcher.Dispatcher._silent_call_request",
"aiogram.dispatcher.dispatcher.Dispatcher.silent_call_request",
new_callable=CoroutineMock,
) as mocked_silent_call_request:
response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=0.1)