mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
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:
parent
b311d59fce
commit
c9f0b36ad6
3 changed files with 18 additions and 2 deletions
1
CHANGES/1249.feature.rst
Normal file
1
CHANGES/1249.feature.rst
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Added support for message_thread_id in ChatActionSender
|
||||||
|
|
@ -33,6 +33,7 @@ class ChatActionSender:
|
||||||
*,
|
*,
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
chat_id: Union[str, int],
|
chat_id: Union[str, int],
|
||||||
|
message_thread_id: Optional[int] = None,
|
||||||
action: str = "typing",
|
action: str = "typing",
|
||||||
interval: float = DEFAULT_INTERVAL,
|
interval: float = DEFAULT_INTERVAL,
|
||||||
initial_sleep: float = DEFAULT_INITIAL_SLEEP,
|
initial_sleep: float = DEFAULT_INITIAL_SLEEP,
|
||||||
|
|
@ -45,6 +46,7 @@ class ChatActionSender:
|
||||||
:param initial_sleep: sleep before first iteration
|
:param initial_sleep: sleep before first iteration
|
||||||
"""
|
"""
|
||||||
self.chat_id = chat_id
|
self.chat_id = chat_id
|
||||||
|
self.message_thread_id = message_thread_id
|
||||||
self.action = action
|
self.action = action
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.initial_sleep = initial_sleep
|
self.initial_sleep = initial_sleep
|
||||||
|
|
@ -82,7 +84,11 @@ class ChatActionSender:
|
||||||
self.bot.id,
|
self.bot.id,
|
||||||
counter,
|
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
|
counter += 1
|
||||||
|
|
||||||
interval = self.interval - (time.monotonic() - start)
|
interval = self.interval - (time.monotonic() - start)
|
||||||
|
|
@ -341,5 +347,10 @@ class ChatActionMiddleware(BaseMiddleware):
|
||||||
kwargs["action"] = "typing"
|
kwargs["action"] = "typing"
|
||||||
else:
|
else:
|
||||||
kwargs["action"] = chat_action
|
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):
|
async with ChatActionSender(bot=bot, chat_id=event.chat.id, **kwargs):
|
||||||
return await handler(event, data)
|
return await handler(event, data)
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,11 @@ class TestChatActionSender:
|
||||||
):
|
):
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
assert mocked_send_chat_action.await_count > 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):
|
async def test_contextmanager(self, bot: MockedBot):
|
||||||
sender: ChatActionSender = ChatActionSender.typing(bot=bot, chat_id=42)
|
sender: ChatActionSender = ChatActionSender.typing(bot=bot, chat_id=42)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue