Add a friendly method to react to messages (#3681)

This commit is contained in:
Shrimadhav U K 2022-02-16 15:56:09 +05:30 committed by GitHub
parent af0ea638b4
commit c45b919109
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -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 # Pinning a message that doesn't exist would RPC-error earlier
return self._get_response_message(request, result, entity) 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)

View File

@ -1251,6 +1251,19 @@ class Message(ChatGetter, SenderGetter):
return await self._client.unpin_message( return await self._client.unpin_message(
await self.get_input_chat(), self.id) 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 # endregion Public Methods
# region Private Methods # region Private Methods