From c9f0b36ad6c2e5b2a971020fc6eccb5b1ee8c9c1 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sun, 6 Aug 2023 19:17:58 +0300 Subject: [PATCH] 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 --- CHANGES/1249.feature.rst | 1 + aiogram/utils/chat_action.py | 13 ++++++++++++- tests/test_utils/test_chat_action.py | 6 +++++- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 CHANGES/1249.feature.rst diff --git a/CHANGES/1249.feature.rst b/CHANGES/1249.feature.rst new file mode 100644 index 00000000..147b407b --- /dev/null +++ b/CHANGES/1249.feature.rst @@ -0,0 +1 @@ +Added support for message_thread_id in ChatActionSender diff --git a/aiogram/utils/chat_action.py b/aiogram/utils/chat_action.py index d4778443..6d7b7400 100644 --- a/aiogram/utils/chat_action.py +++ b/aiogram/utils/chat_action.py @@ -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) diff --git a/tests/test_utils/test_chat_action.py b/tests/test_utils/test_chat_action.py index 84cb8abb..d6041c37 100644 --- a/tests/test_utils/test_chat_action.py +++ b/tests/test_utils/test_chat_action.py @@ -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)