Added support for message_thread_id in ChatActionSender (#1250)

* Add support for message_thread_id in ChatActionSender

The given changes add support for including the 'message_thread_id' in ChatActionSender function calls, allowing actions to be sent in specific threads rather than the main chat.

* Added changelog
This commit is contained in:
Alex Root Junior 2023-08-06 19:17:58 +03:00 committed by GitHub
parent b311d59fce
commit c9f0b36ad6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 2 deletions

1
CHANGES/1249.feature.rst Normal file
View file

@ -0,0 +1 @@
Added support for message_thread_id in ChatActionSender

View file

@ -33,6 +33,7 @@ class ChatActionSender:
*,
bot: Bot,
chat_id: Union[str, int],
message_thread_id: Optional[int] = None,
action: str = "typing",
interval: float = DEFAULT_INTERVAL,
initial_sleep: float = DEFAULT_INITIAL_SLEEP,
@ -45,6 +46,7 @@ class ChatActionSender:
:param initial_sleep: sleep before first iteration
"""
self.chat_id = chat_id
self.message_thread_id = message_thread_id
self.action = action
self.interval = interval
self.initial_sleep = initial_sleep
@ -82,7 +84,11 @@ class ChatActionSender:
self.bot.id,
counter,
)
await self.bot.send_chat_action(chat_id=self.chat_id, action=self.action)
await self.bot.send_chat_action(
chat_id=self.chat_id,
action=self.action,
message_thread_id=self.message_thread_id,
)
counter += 1
interval = self.interval - (time.monotonic() - start)
@ -341,5 +347,10 @@ class ChatActionMiddleware(BaseMiddleware):
kwargs["action"] = "typing"
else:
kwargs["action"] = chat_action
kwargs["message_thread_id"] = (
event.message_thread_id
if isinstance(event, Message) and event.is_topic_message
else None
)
async with ChatActionSender(bot=bot, chat_id=event.chat.id, **kwargs):
return await handler(event, data)

View file

@ -54,7 +54,11 @@ class TestChatActionSender:
):
await asyncio.sleep(0.1)
assert mocked_send_chat_action.await_count > 1
mocked_send_chat_action.assert_awaited_with(action="typing", chat_id=42)
mocked_send_chat_action.assert_awaited_with(
action="typing",
chat_id=42,
message_thread_id=None,
)
async def test_contextmanager(self, bot: MockedBot):
sender: ChatActionSender = ChatActionSender.typing(bot=bot, chat_id=42)