From c43e2a0a3a7af773af3f4342c99b44531e1b603a Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 29 Apr 2020 10:29:14 +0200 Subject: [PATCH] Return produced service message with pin_message Fixes #1394. --- telethon/client/messageparse.py | 9 ++++++++- telethon/client/messages.py | 17 +++++++++++++++-- telethon/events/album.py | 2 +- telethon/tl/custom/message.py | 2 +- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/telethon/client/messageparse.py b/telethon/client/messageparse.py index 7bff5c06..1815f305 100644 --- a/telethon/client/messageparse.py +++ b/telethon/client/messageparse.py @@ -131,7 +131,14 @@ class MessageParseMethods: elif isinstance(update, ( types.UpdateNewChannelMessage, types.UpdateNewMessage)): update.message._finish_init(self, entities, input_chat) - id_to_message[update.message.id] = update.message + + # Pinning a message with `updatePinnedMessage` seems to + # always produce a service message we can't map so return + # it directly. + if hasattr(request, 'random_id'): + id_to_message[update.message.id] = update.message + else: + return update.message elif (isinstance(update, types.UpdateEditMessage) and helpers._entity_type(request.peer) != helpers._EntityType.CHANNEL): diff --git a/telethon/client/messages.py b/telethon/client/messages.py index b0d76f07..8237c380 100644 --- a/telethon/client/messages.py +++ b/telethon/client/messages.py @@ -1246,11 +1246,24 @@ class MessageMethods: """ message = utils.get_message_id(message) or 0 entity = await self.get_input_entity(entity) - await self(functions.messages.UpdatePinnedMessageRequest( + request = functions.messages.UpdatePinnedMessageRequest( peer=entity, id=message, silent=not notify - )) + ) + result = await self(request) + + # Unpinning does not produce a service message, and technically + # users can pass negative IDs which seem to behave as unpinning too. + if message <= 0: + return + + # Pinning in User chats (just with yourself really) does not produce a service message + if helpers._entity_type(entity) == helpers._EntityType.USER: + return + + # Pinning a message that doesn't exist would RPC-error earlier + return self._get_response_message(request, result, entity) # endregion diff --git a/telethon/events/album.py b/telethon/events/album.py index 2ba1e61b..f32dfc83 100644 --- a/telethon/events/album.py +++ b/telethon/events/album.py @@ -259,7 +259,7 @@ class Album(EventBuilder): `telethon.client.messages.MessageMethods.pin_message` with both ``entity`` and ``message`` already set. """ - await self.messages[0].pin(notify=notify) + return await self.messages[0].pin(notify=notify) def __len__(self): """ diff --git a/telethon/tl/custom/message.py b/telethon/tl/custom/message.py index 7460a487..4ece30d8 100644 --- a/telethon/tl/custom/message.py +++ b/telethon/tl/custom/message.py @@ -898,7 +898,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC): # maybe just make it illegal to call messages from raw API? # That or figure out a way to always set it directly. if self._client: - await self._client.pin_message( + return await self._client.pin_message( await self.get_input_chat(), self.id, notify=notify) # endregion Public Methods