Skip to content

Commit

Permalink
Added support for message_thread_id in ChatActionSender (#1250)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JrooTJunior committed Aug 6, 2023
1 parent b311d59 commit c9f0b36
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/1249.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for message_thread_id in ChatActionSender
13 changes: 12 additions & 1 deletion aiogram/utils/chat_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(
*,
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,
Expand All @@ -45,6 +46,7 @@ def __init__(
: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
Expand Down Expand Up @@ -82,7 +84,11 @@ async def _worker(self) -> None:
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)
Expand Down Expand Up @@ -341,5 +347,10 @@ async def __call__(
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)
6 changes: 5 additions & 1 deletion tests/test_utils/test_chat_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ async def test_worker(self, bot: Bot):
):
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)
Expand Down

0 comments on commit c9f0b36

Please sign in to comment.