From c45b91910906736dd6f21b6a3bdd0dfa80a905cd Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Wed, 16 Feb 2022 15:56:09 +0530 Subject: [PATCH] Add a friendly method to react to messages (#3681) --- telethon/_client/messages.py | 41 +++++++++++++++++++++++++++++++ telethon/types/_custom/message.py | 13 ++++++++++ 2 files changed, 54 insertions(+) diff --git a/telethon/_client/messages.py b/telethon/_client/messages.py index 1949ee89..2e89ffd8 100644 --- a/telethon/_client/messages.py +++ b/telethon/_client/messages.py @@ -747,3 +747,44 @@ async def _pin(self, entity, message, *, unpin, notify=False, pm_oneside=False): # Pinning a message that doesn't exist would RPC-error earlier return self._get_response_message(request, result, entity) + +async def send_reaction( + self: 'TelegramClient', + entity: 'hints.EntityLike', + message: 'hints.MessageIDLike', + reaction: typing.Optional[str] = None, + big: bool = False +): + message = utils.get_message_id(message) or 0 + if not reaction: + get_default_request = _tl.fn.help.GetAppConfig() + app_config = await self(get_default_request) + reaction = ( + next( + ( + y for y in app_config.value + if "reactions_default" in y.key + ) + ) + ).value.value + request = _tl.fn.messages.SendReaction( + big=big, + peer=entity, + msg_id=message, + reaction=reaction + ) + result = await self(request) + for update in result.updates: + if isinstance(update, _tl.UpdateMessageReactions): + return update.reactions + if isinstance(update, _tl.UpdateEditMessage): + return update.message.reactions + +async def set_quick_reaction( + self: 'TelegramClient', + reaction: str +): + request = _tl.fn.messages.SetDefaultReaction( + reaction=reaction + ) + return await self(request) diff --git a/telethon/types/_custom/message.py b/telethon/types/_custom/message.py index 730d2b49..84c2e409 100644 --- a/telethon/types/_custom/message.py +++ b/telethon/types/_custom/message.py @@ -1251,6 +1251,19 @@ class Message(ChatGetter, SenderGetter): return await self._client.unpin_message( await self.get_input_chat(), self.id) + async def react(self, reaction=None): + """ + Reacts on the given message. Shorthand for + `telethon.client.messages.MessageMethods.send_reaction` + with both ``entity`` and ``message`` already set. + """ + if self._client: + return await self._client.send_reaction( + await self.get_input_chat(), + self.id, + reaction + ) + # endregion Public Methods # region Private Methods