Support await on any client.action

This commit is contained in:
Lonami Exo 2022-01-16 12:40:09 +01:00
parent a3513d5232
commit dc0f978b59
2 changed files with 9 additions and 5 deletions

View File

@ -750,3 +750,5 @@ keep support for explicit size instance?
renamed send_read_acknowledge. add send_read_acknowledge as alias for mark_read?
force sms removed as it was broken anyway and not very reliable
you can now await client.action for a one-off any action not just cancel

View File

@ -27,6 +27,9 @@ class _ChatAction:
self._task = None
self._running = False
def __await__(self):
return self._once().__await__()
async def __aenter__(self):
self._chat = await self._client.get_input_entity(self._chat)
@ -51,6 +54,10 @@ class _ChatAction:
self._task = None
async def _once(self):
self._chat = await self._client.get_input_entity(self._chat)
await self._client(_tl.fn.messages.SetTyping(self._chat, self._action))
async def _update(self):
try:
while self._running:
@ -456,11 +463,6 @@ def action(
auto_cancel: bool = True) -> 'typing.Union[_ChatAction, typing.Coroutine]':
action = _ChatAction._parse(action)
if isinstance(action, _tl.SendMessageCancelAction):
# ``SetTyping.resolve`` will get input peer of ``entity``.
return self(_tl.fn.messages.SetTyping(
entity, _tl.SendMessageCancelAction()))
return _ChatAction(
self, entity, action, delay=delay, auto_cancel=auto_cancel)