From 2cffc16dd3d52cade01d5ff4ac292e1379393fde Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Fri, 6 Nov 2020 14:27:01 +0400 Subject: [PATCH 1/6] Update pinned message methods - Also added unpin() shortcut in message.py and unpin all msgs shortcut in chat.py Signed-off-by: Harshil --- telegram/bot.py | 65 +++++++++++++++++++++++++++++++++++++------ telegram/chat.py | 21 +++++++++++--- telegram/message.py | 16 +++++++++++ tests/test_bot.py | 15 +++++++--- tests/test_chat.py | 7 +++++ tests/test_message.py | 9 ++++++ 6 files changed, 116 insertions(+), 17 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index daad48e5722..996940f8a11 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -3628,10 +3628,10 @@ def pin_chat_message( api_kwargs: JSONDict = None, ) -> bool: """ - Use this method to pin a message in a group, a supergroup, or a channel. - The bot must be an administrator in the chat for this to work and must have the - ‘can_pin_messages’ admin right in the supergroup or ‘can_edit_messages’ admin right - in the channel. + Use this method to add a message to the list of pinned messages in a chat. If the + chat is not a private chat, the bot must be an administrator in the chat for this to work + and must have the ``can_pin_messages`` admin right in a supergroup or ``can_edit_messages`` + admin right in a channel. Args: chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username @@ -3664,17 +3664,23 @@ def pin_chat_message( @log def unpin_chat_message( - self, chat_id: Union[str, int], timeout: float = None, api_kwargs: JSONDict = None + self, + chat_id: Union[str, int], + message_id: Union[str, int] = None, + timeout: float = None, + api_kwargs: JSONDict = None, ) -> bool: """ - Use this method to unpin a message in a group, a supergroup, or a channel. - The bot must be an administrator in the chat for this to work and must have the - ``can_pin_messages`` admin right in the supergroup or ``can_edit_messages`` admin right - in the channel. + Use this method to remove a message from the list of pinned messages in a chat. If the + chat is not a private chat, the bot must be an administrator in the chat for this to work + and must have the ``can_pin_messages`` admin right in a supergroup or ``can_edit_messages`` + admin right in a channel. Args: chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username of the target channel (in the format @channelusername). + message_id (:obj:`int`, optional): Identifier of a message to unpin. If not specified, + the most recent pinned message (by sending date) will be unpinned. timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as the read timeout from the server (instead of the one specified during creation of the connection pool). @@ -3690,10 +3696,49 @@ def unpin_chat_message( """ data: JSONDict = {'chat_id': chat_id} + if message_id is not None: + data['message_id'] = message_id + result = self._post('unpinChatMessage', data, timeout=timeout, api_kwargs=api_kwargs) return result # type: ignore[return-value] + @log + def unpin_all_chat_messages( + self, + chat_id: Union[str, int], + timeout: float = None, + api_kwargs: JSONDict = None, + ) -> bool: + """ + Use this method to remove a message from the list of pinned messages in a chat. If the + chat is not a private chat, the bot must be an administrator in the chat for this + to work and must have the ``can_pin_messages`` admin right in a supergroup or + ``can_edit_messages`` admin right in a channel. + + Args: + chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username + of the target channel (in the format @channelusername). + timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as + the read timeout from the server (instead of the one specified during creation of + the connection pool). + api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the + Telegram API. + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + Raises: + :class:`telegram.TelegramError` + + """ + + data: JSONDict = {'chat_id': chat_id} + + result = self._post('unpinAllChatMessages', data, timeout=timeout, api_kwargs=api_kwargs) + + return result # type: ignore[return-value] + @log def get_sticker_set( self, name: str, timeout: float = None, api_kwargs: JSONDict = None @@ -4483,6 +4528,8 @@ def to_dict(self) -> JSONDict: """Alias for :attr:`pin_chat_message`""" unpinChatMessage = unpin_chat_message """Alias for :attr:`unpin_chat_message`""" + unpinAllChatMessages = unpin_all_chat_messages + """Alias for :attr:`unpin_all_chat_messages`""" getStickerSet = get_sticker_set """Alias for :attr:`get_sticker_set`""" uploadStickerFile = upload_sticker_file diff --git a/telegram/chat.py b/telegram/chat.py index d1c4c7e481e..479ed0bed3c 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -46,8 +46,8 @@ class Chat(TelegramObject): photo (:class:`telegram.ChatPhoto`): Optional. Chat photo. description (:obj:`str`): Optional. Description, for groups, supergroups and channel chats. invite_link (:obj:`str`): Optional. Chat invite link, for supergroups and channel chats. - pinned_message (:class:`telegram.Message`): Optional. Pinned message, for supergroups. - Returned only in :meth:`telegram.Bot.get_chat`. + pinned_message (:class:`telegram.Message`): Optional. The most recent pinned message + (by sending date). Returned only in :meth:`telegram.Bot.get_chat`. permissions (:class:`telegram.ChatPermissions`): Optional. Default chat member permissions, for groups and supergroups. Returned only in :meth:`telegram.Bot.get_chat`. slow_mode_delay (:obj:`int`): Optional. For supergroups, the minimum allowed delay between @@ -77,8 +77,8 @@ class Chat(TelegramObject): chats. Each administrator in a chat generates their own invite links, so the bot must first generate the link using ``export_chat_invite_link()``. Returned only in :meth:`telegram.Bot.get_chat`. - pinned_message (:class:`telegram.Message`, optional): Pinned message, for groups, - supergroups and channels. Returned only in :meth:`telegram.Bot.get_chat`. + pinned_message (:class:`telegram.Message`, optional): The most recent pinned message + (by sending date). Returned only in :meth:`telegram.Bot.get_chat`. permissions (:class:`telegram.ChatPermissions`): Optional. Default chat member permissions, for groups and supergroups. Returned only in :meth:`telegram.Bot.get_chat`. slow_mode_delay (:obj:`int`, optional): For supergroups, the minimum allowed delay between @@ -277,6 +277,19 @@ def set_administrator_custom_title(self, *args: Any, **kwargs: Any) -> bool: """ return self.bot.set_chat_administrator_custom_title(self.id, *args, **kwargs) + def unpin_all_chat_messages(self, *args: Any, **kwargs: Any) -> bool: + """Shortcut for:: + + bot.unpin_all_chat_messages(chat_id=message.chat_id, + *args, + **kwargs) + + Returns: + :obj:`True`: On success. + + """ + return self.bot.unpin_all_chat_messages(chat_id=self.id, *args, **kwargs) + def send_message(self, *args: Any, **kwargs: Any) -> 'Message': """Shortcut for:: diff --git a/telegram/message.py b/telegram/message.py index 7f2d2ab9eee..d1aa824036f 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -1143,6 +1143,22 @@ def pin(self, *args: Any, **kwargs: Any) -> bool: chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs ) + def unpin(self, *args: Any, **kwargs: Any) -> bool: + """Shortcut for:: + + bot.unpin_chat_message(chat_id=message.chat_id, + message_id=message.message_id, + *args, + **kwargs) + + Returns: + :obj:`True`: On success. + + """ + return self.bot.unpin_chat_message( + chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs + ) + def parse_entity(self, entity: MessageEntity) -> str: """Returns the text from a given :class:`telegram.MessageEntity`. diff --git a/tests/test_bot.py b/tests/test_bot.py index f12e51671fe..a62db807fe9 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -1263,15 +1263,22 @@ def test_set_chat_description(self, bot, channel_id): @flaky(3, 1) @pytest.mark.timeout(10) def test_pin_and_unpin_message(self, bot, super_group_id): - message = bot.send_message(super_group_id, text="test_pin_message") + message1 = bot.send_message(super_group_id, text="test_pin_message_1") + message2 = bot.send_message(super_group_id, text="test_pin_message_2") + message3 = bot.send_message(super_group_id, text="test_pin_message_3") + assert bot.pin_chat_message( - chat_id=super_group_id, message_id=message.message_id, disable_notification=True + chat_id=super_group_id, message_id=message1.message_id, disable_notification=True ) + message2.pin() + message3.pin() chat = bot.get_chat(super_group_id) - assert chat.pinned_message == message + assert chat.pinned_message == message3 + + assert bot.unpinChatMessage(super_group_id, message2.message_id) - assert bot.unpinChatMessage(super_group_id) + assert bot.unpin_all_chat_messages(super_group_id) # get_sticker_set, upload_sticker_file, create_new_sticker_set, add_sticker_to_set, # set_sticker_position_in_set and delete_sticker_from_set are tested in the diff --git a/tests/test_chat.py b/tests/test_chat.py index 337c209c8bc..7edd74cbd6b 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -183,6 +183,13 @@ def test(*args, **kwargs): monkeypatch.setattr('telegram.Bot.set_chat_administrator_custom_title', test) assert chat.set_administrator_custom_title(42, 'custom_title') + def test_unpin_all_chat_messages(self, monkeypatch, chat): + def test(*args, **kwargs): + return kwargs['chat_id'] == chat.id + + monkeypatch.setattr(chat.bot, 'unpin_all_chat_messages', test) + assert chat.unpin_all_chat_messages() + def test_instance_method_send_message(self, monkeypatch, chat): def test(*args, **kwargs): return args[0] == chat.id and args[1] == 'test' diff --git a/tests/test_message.py b/tests/test_message.py index ab25f4da5f5..3d4b8736a27 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -1052,6 +1052,15 @@ def test(*args, **kwargs): monkeypatch.setattr(message.bot, 'pin_chat_message', test) assert message.pin() + def test_unpin(self, monkeypatch, message): + def test(*args, **kwargs): + chat_id = kwargs['chat_id'] == message.chat_id + message_id = kwargs['message_id'] == message.message_id + return chat_id and message_id + + monkeypatch.setattr(message.bot, 'unpin_chat_message', test) + assert message.unpin() + def test_default_quote(self, message): message.bot.defaults = Defaults() kwargs = {} From 9de19eb9c644a7148dc588b04a150ed653cfe75c Mon Sep 17 00:00:00 2001 From: Harshil Date: Sat, 7 Nov 2020 00:36:33 +0400 Subject: [PATCH 2/6] Address review + Minor fixes and changes - Add pin_message() & unpin_message() shortcuts in `CallbackQuery`. - Add pin_message(), unpin_message(), & unpin_all_messages() shortcuts in `User`. - Rename unpin_all_chat_messages() to unpin_all_messages() in `Chat` - Minor doc fixes. Signed-off-by: Harshil --- telegram/bot.py | 24 +++++++++++------------ telegram/callbackquery.py | 26 +++++++++++++++++++++++++ telegram/chat.py | 6 +++--- telegram/message.py | 4 ++-- telegram/user.py | 39 +++++++++++++++++++++++++++++++++++++ tests/test_bot.py | 10 ++++++++-- tests/test_callbackquery.py | 20 +++++++++++++++++++ tests/test_chat.py | 8 ++++---- tests/test_message.py | 8 ++++---- tests/test_user.py | 21 ++++++++++++++++++++ 10 files changed, 139 insertions(+), 27 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index 996940f8a11..ab9cfab30fd 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -3638,8 +3638,8 @@ def pin_chat_message( of the target channel (in the format @channelusername). message_id (:obj:`int`): Identifier of a message to pin. disable_notification (:obj:`bool`, optional): Pass :obj:`True`, if it is not necessary - to send a notification to all group members about the new pinned message. - Notifications are always disabled in channels. + to send a notification to all chat members about the new pinned message. + Notifications are always disabled in channels and private chats. timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as the read timeout from the server (instead of the one specified during creation of the connection pool). @@ -3658,17 +3658,17 @@ def pin_chat_message( if disable_notification is not None: data['disable_notification'] = disable_notification - result = self._post('pinChatMessage', data, timeout=timeout, api_kwargs=api_kwargs) - - return result # type: ignore[return-value] + return self._post( + 'pinChatMessage', data, timeout=timeout, api_kwargs=api_kwargs + ) # type: ignore[return-value] @log def unpin_chat_message( self, chat_id: Union[str, int], - message_id: Union[str, int] = None, timeout: float = None, api_kwargs: JSONDict = None, + message_id: Union[str, int] = None, ) -> bool: """ Use this method to remove a message from the list of pinned messages in a chat. If the @@ -3699,9 +3699,9 @@ def unpin_chat_message( if message_id is not None: data['message_id'] = message_id - result = self._post('unpinChatMessage', data, timeout=timeout, api_kwargs=api_kwargs) - - return result # type: ignore[return-value] + return self._post( + 'unpinChatMessage', data, timeout=timeout, api_kwargs=api_kwargs + ) # type: ignore[return-value] @log def unpin_all_chat_messages( @@ -3735,9 +3735,9 @@ def unpin_all_chat_messages( data: JSONDict = {'chat_id': chat_id} - result = self._post('unpinAllChatMessages', data, timeout=timeout, api_kwargs=api_kwargs) - - return result # type: ignore[return-value] + return self._post( + 'unpinAllChatMessages', data, timeout=timeout, api_kwargs=api_kwargs + ) # type: ignore[return-value] @log def get_sticker_set( diff --git a/telegram/callbackquery.py b/telegram/callbackquery.py index f47b1f5f4f2..59f005a960d 100644 --- a/telegram/callbackquery.py +++ b/telegram/callbackquery.py @@ -325,3 +325,29 @@ def delete_message(self, *args: Any, **kwargs: Any) -> Union[Message, bool]: """ return self.message.delete(*args, **kwargs) + + def pin_message(self, *args: Any, **kwargs: Any) -> bool: + """Shortcut for:: + + bot.pin_chat_message(chat_id=update.callback_query.message.chat_id, + *args, + **kwargs) + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + """ + return self.bot.pin_chat_message(self.message.chat_id, *args, **kwargs) + + def unpin_message(self, *args: Any, **kwargs: Any) -> bool: + """Shortcut for:: + + bot.unpin_chat_message(chat_id=update.callback_query.message.chat_id, + *args, + **kwargs) + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + """ + return self.bot.unpin_chat_message(self.message.chat_id, *args, **kwargs) diff --git a/telegram/chat.py b/telegram/chat.py index 479ed0bed3c..be081b054fe 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -277,15 +277,15 @@ def set_administrator_custom_title(self, *args: Any, **kwargs: Any) -> bool: """ return self.bot.set_chat_administrator_custom_title(self.id, *args, **kwargs) - def unpin_all_chat_messages(self, *args: Any, **kwargs: Any) -> bool: + def unpin_all_messages(self, *args: Any, **kwargs: Any) -> bool: """Shortcut for:: - bot.unpin_all_chat_messages(chat_id=message.chat_id, + bot.unpin_all_chat_messages(chat_id=update.effective_chat.id, *args, **kwargs) Returns: - :obj:`True`: On success. + :obj:`bool`: On success, :obj:`True` is returned. """ return self.bot.unpin_all_chat_messages(chat_id=self.id, *args, **kwargs) diff --git a/telegram/message.py b/telegram/message.py index d1aa824036f..0e5cfda3e2e 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -1136,7 +1136,7 @@ def pin(self, *args: Any, **kwargs: Any) -> bool: **kwargs) Returns: - :obj:`True`: On success. + :obj:`bool`: On success, :obj:`True` is returned. """ return self.bot.pin_chat_message( @@ -1152,7 +1152,7 @@ def unpin(self, *args: Any, **kwargs: Any) -> bool: **kwargs) Returns: - :obj:`True`: On success. + :obj:`bool`: On success, :obj:`True` is returned. """ return self.bot.unpin_chat_message( diff --git a/telegram/user.py b/telegram/user.py index 8f9a93cda2f..cf24e528142 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -189,6 +189,45 @@ def mention_html(self, name: str = None) -> str: return util_mention_html(self.id, name) return util_mention_html(self.id, self.full_name) + def pin_message(self, *args: Any, **kwargs: Any) -> bool: + """Shortcut for:: + + bot.pin_chat_message(chat_id=update.effective_user.id, + *args, + **kwargs) + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + """ + return self.bot.pin_chat_message(self.id, *args, **kwargs) + + def unpin_message(self, *args: Any, **kwargs: Any) -> bool: + """Shortcut for:: + + bot.unpin_chat_message(chat_id=update.effective_user.id, + *args, + **kwargs) + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + """ + return self.bot.unpin_chat_message(self.id, *args, **kwargs) + + def unpin_all_messages(self, *args: Any, **kwargs: Any) -> bool: + """Shortcut for:: + + bot.unpin_all_chat_messages(chat_id=update.effective_user.id, + *args, + **kwargs) + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + """ + return self.bot.unpin_all_chat_messages(self.id, *args, **kwargs) + def send_message(self, *args: Any, **kwargs: Any) -> 'Message': """Shortcut for:: diff --git a/tests/test_bot.py b/tests/test_bot.py index a62db807fe9..97f4ae66729 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -1270,13 +1270,19 @@ def test_pin_and_unpin_message(self, bot, super_group_id): assert bot.pin_chat_message( chat_id=super_group_id, message_id=message1.message_id, disable_notification=True ) - message2.pin() - message3.pin() + + bot.pin_chat_message( + chat_id=super_group_id, message_id=message2.message_id, disable_notification=True + ) + bot.pin_chat_message( + chat_id=super_group_id, message_id=message3.message_id, disable_notification=True + ) chat = bot.get_chat(super_group_id) assert chat.pinned_message == message3 assert bot.unpinChatMessage(super_group_id, message2.message_id) + assert bot.unpin_chat_message(super_group_id) assert bot.unpin_all_chat_messages(super_group_id) diff --git a/tests/test_callbackquery.py b/tests/test_callbackquery.py index 463718b1e0e..539b21b1dbc 100644 --- a/tests/test_callbackquery.py +++ b/tests/test_callbackquery.py @@ -223,6 +223,26 @@ def make_assertion(*args, **kwargs): monkeypatch.setattr(callback_query.bot, 'delete_message', make_assertion) assert callback_query.delete_message() + def test_pin_message(self, monkeypatch, callback_query): + if callback_query.inline_message_id: + pytest.skip("Can't pin inline messages") + + def make_assertion(*args, **kwargs): + return args[0] == callback_query.message.chat_id + + monkeypatch.setattr(callback_query.bot, 'pin_chat_message', make_assertion) + assert callback_query.pin_message() + + def test_unpin_message(self, monkeypatch, callback_query): + if callback_query.inline_message_id: + pytest.skip("Can't unpin inline messages") + + def make_assertion(*args, **kwargs): + return args[0] == callback_query.message.chat_id + + monkeypatch.setattr(callback_query.bot, 'unpin_chat_message', make_assertion) + assert callback_query.unpin_message() + def test_equality(self): a = CallbackQuery(self.id_, self.from_user, 'chat') b = CallbackQuery(self.id_, self.from_user, 'chat') diff --git a/tests/test_chat.py b/tests/test_chat.py index 7edd74cbd6b..330e8839e56 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -183,12 +183,12 @@ def test(*args, **kwargs): monkeypatch.setattr('telegram.Bot.set_chat_administrator_custom_title', test) assert chat.set_administrator_custom_title(42, 'custom_title') - def test_unpin_all_chat_messages(self, monkeypatch, chat): - def test(*args, **kwargs): + def test_unpin_all_messages(self, monkeypatch, chat): + def make_assertion(*args, **kwargs): return kwargs['chat_id'] == chat.id - monkeypatch.setattr(chat.bot, 'unpin_all_chat_messages', test) - assert chat.unpin_all_chat_messages() + monkeypatch.setattr(chat.bot, 'unpin_all_chat_messages', make_assertion) + assert chat.unpin_all_messages() def test_instance_method_send_message(self, monkeypatch, chat): def test(*args, **kwargs): diff --git a/tests/test_message.py b/tests/test_message.py index 3d4b8736a27..c5b30e01590 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -1044,21 +1044,21 @@ def test(*args, **kwargs): assert message.stop_poll() def test_pin(self, monkeypatch, message): - def test(*args, **kwargs): + def make_assertion(*args, **kwargs): chat_id = kwargs['chat_id'] == message.chat_id message_id = kwargs['message_id'] == message.message_id return chat_id and message_id - monkeypatch.setattr(message.bot, 'pin_chat_message', test) + monkeypatch.setattr(message.bot, 'pin_chat_message', make_assertion) assert message.pin() def test_unpin(self, monkeypatch, message): - def test(*args, **kwargs): + def make_assertion(*args, **kwargs): chat_id = kwargs['chat_id'] == message.chat_id message_id = kwargs['message_id'] == message.message_id return chat_id and message_id - monkeypatch.setattr(message.bot, 'unpin_chat_message', test) + monkeypatch.setattr(message.bot, 'unpin_chat_message', make_assertion) assert message.unpin() def test_default_quote(self, message): diff --git a/tests/test_user.py b/tests/test_user.py index 15fc159df6a..1207ad1dd05 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -144,6 +144,27 @@ def test(*args, **kwargs): monkeypatch.setattr(user.bot, 'get_user_profile_photos', test) assert user.get_profile_photos() + def test_pin_message(self, monkeypatch, user): + def make_assertion(*args, **kwargs): + return args[0] == user.id + + monkeypatch.setattr(user.bot, 'pin_chat_message', make_assertion) + assert user.pin_message() + + def test_unpin_message(self, monkeypatch, user): + def make_assertion(*args, **kwargs): + return args[0] == user.id + + monkeypatch.setattr(user.bot, 'unpin_chat_message', make_assertion) + assert user.unpin_message() + + def test_unpin_all_messages(self, monkeypatch, user): + def make_assertion(*args, **kwargs): + return args[0] == user.id + + monkeypatch.setattr(user.bot, 'unpin_all_chat_messages', make_assertion) + assert user.unpin_all_messages() + def test_instance_method_send_message(self, monkeypatch, user): def test(*args, **kwargs): return args[0] == user.id and args[1] == 'test' From 2166cf5d57f762b2cfadce99f6e1b8e0756f0d18 Mon Sep 17 00:00:00 2001 From: Harshil Date: Sat, 7 Nov 2020 00:51:01 +0400 Subject: [PATCH 3/6] fix failing pre-commit(?) Signed-off-by: Harshil --- telegram/bot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index ab9cfab30fd..12f8fa7cc34 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -3658,9 +3658,9 @@ def pin_chat_message( if disable_notification is not None: data['disable_notification'] = disable_notification - return self._post( + return self._post( # type: ignore[return-value] 'pinChatMessage', data, timeout=timeout, api_kwargs=api_kwargs - ) # type: ignore[return-value] + ) @log def unpin_chat_message( @@ -3699,9 +3699,9 @@ def unpin_chat_message( if message_id is not None: data['message_id'] = message_id - return self._post( + return self._post( # type: ignore[return-value] 'unpinChatMessage', data, timeout=timeout, api_kwargs=api_kwargs - ) # type: ignore[return-value] + ) @log def unpin_all_chat_messages( @@ -3735,9 +3735,9 @@ def unpin_all_chat_messages( data: JSONDict = {'chat_id': chat_id} - return self._post( + return self._post( # type: ignore[return-value] 'unpinAllChatMessages', data, timeout=timeout, api_kwargs=api_kwargs - ) # type: ignore[return-value] + ) @log def get_sticker_set( From 67a310fb7cb0585e58c32c34dcf50eee93ba501b Mon Sep 17 00:00:00 2001 From: Harshil Date: Sat, 7 Nov 2020 16:53:25 +0400 Subject: [PATCH 4/6] Address review, fix tests --- telegram/callbackquery.py | 4 ++-- telegram/chat.py | 26 ++++++++++++++++++++++++++ tests/test_bot.py | 2 +- tests/test_callbackquery.py | 12 ++++++++++-- tests/test_chat.py | 25 ++++++++++++++++++++++++- 5 files changed, 63 insertions(+), 6 deletions(-) diff --git a/telegram/callbackquery.py b/telegram/callbackquery.py index 59f005a960d..cd4a6ff36d1 100644 --- a/telegram/callbackquery.py +++ b/telegram/callbackquery.py @@ -337,7 +337,7 @@ def pin_message(self, *args: Any, **kwargs: Any) -> bool: :obj:`bool`: On success, :obj:`True` is returned. """ - return self.bot.pin_chat_message(self.message.chat_id, *args, **kwargs) + return self.message.pin(*args, **kwargs) def unpin_message(self, *args: Any, **kwargs: Any) -> bool: """Shortcut for:: @@ -350,4 +350,4 @@ def unpin_message(self, *args: Any, **kwargs: Any) -> bool: :obj:`bool`: On success, :obj:`True` is returned. """ - return self.bot.unpin_chat_message(self.message.chat_id, *args, **kwargs) + return self.message.unpin(*args, **kwargs) diff --git a/telegram/chat.py b/telegram/chat.py index be081b054fe..bc8e975eb31 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -277,6 +277,32 @@ def set_administrator_custom_title(self, *args: Any, **kwargs: Any) -> bool: """ return self.bot.set_chat_administrator_custom_title(self.id, *args, **kwargs) + def pin_message(self, *args: Any, **kwargs: Any) -> bool: + """Shortcut for:: + + bot.pin_chat_message(chat_id=update.effective_chat.id, + *args, + **kwargs) + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + """ + return self.bot.pin_chat_message(self.id, *args, **kwargs) + + def unpin_message(self, *args: Any, **kwargs: Any) -> bool: + """Shortcut for:: + + bot.unpin_chat_message(chat_id=update.effective_chat.id, + *args, + **kwargs) + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + """ + return self.bot.unpin_chat_message(self.id, *args, **kwargs) + def unpin_all_messages(self, *args: Any, **kwargs: Any) -> bool: """Shortcut for:: diff --git a/tests/test_bot.py b/tests/test_bot.py index 97f4ae66729..acecd4657fd 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -1281,7 +1281,7 @@ def test_pin_and_unpin_message(self, bot, super_group_id): chat = bot.get_chat(super_group_id) assert chat.pinned_message == message3 - assert bot.unpinChatMessage(super_group_id, message2.message_id) + assert bot.unpin_chat_message(super_group_id, message_id=message2.message_id) assert bot.unpin_chat_message(super_group_id) assert bot.unpin_all_chat_messages(super_group_id) diff --git a/tests/test_callbackquery.py b/tests/test_callbackquery.py index 539b21b1dbc..0c2d0c9ebf9 100644 --- a/tests/test_callbackquery.py +++ b/tests/test_callbackquery.py @@ -228,7 +228,11 @@ def test_pin_message(self, monkeypatch, callback_query): pytest.skip("Can't pin inline messages") def make_assertion(*args, **kwargs): - return args[0] == callback_query.message.chat_id + _id = callback_query.message.chat_id + try: + return kwargs['chat_id'] == _id + except KeyError: + return args[0] == _id monkeypatch.setattr(callback_query.bot, 'pin_chat_message', make_assertion) assert callback_query.pin_message() @@ -238,7 +242,11 @@ def test_unpin_message(self, monkeypatch, callback_query): pytest.skip("Can't unpin inline messages") def make_assertion(*args, **kwargs): - return args[0] == callback_query.message.chat_id + _id = callback_query.message.chat_id + try: + return kwargs['chat_id'] == _id + except KeyError: + return args[0] == _id monkeypatch.setattr(callback_query.bot, 'unpin_chat_message', make_assertion) assert callback_query.unpin_message() diff --git a/tests/test_chat.py b/tests/test_chat.py index 330e8839e56..160690b0779 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -183,9 +183,32 @@ def test(*args, **kwargs): monkeypatch.setattr('telegram.Bot.set_chat_administrator_custom_title', test) assert chat.set_administrator_custom_title(42, 'custom_title') + def test_pin_message(self, monkeypatch, chat): + def make_assertion(*args, **kwargs): + try: + return kwargs['chat_id'] == chat.id + except KeyError: + return args[0] == chat.id + + monkeypatch.setattr(chat.bot, 'pin_chat_message', make_assertion) + assert chat.pin_message() + + def test_unpin_message(self, monkeypatch, chat): + def make_assertion(*args, **kwargs): + try: + return kwargs['chat_id'] == chat.id + except KeyError: + return args[0] == chat.id + + monkeypatch.setattr(chat.bot, 'unpin_chat_message', make_assertion) + assert chat.unpin_message() + def test_unpin_all_messages(self, monkeypatch, chat): def make_assertion(*args, **kwargs): - return kwargs['chat_id'] == chat.id + try: + return kwargs['chat_id'] == chat.id + except KeyError: + return args[0] == chat.id monkeypatch.setattr(chat.bot, 'unpin_all_chat_messages', make_assertion) assert chat.unpin_all_messages() From 37d43735009f9f39c9de524816d444a5c90c29dc Mon Sep 17 00:00:00 2001 From: Harshil Date: Sat, 7 Nov 2020 23:47:38 +0400 Subject: [PATCH 5/6] update docstring (review 3) --- telegram/callbackquery.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram/callbackquery.py b/telegram/callbackquery.py index cd4a6ff36d1..930f74edfdb 100644 --- a/telegram/callbackquery.py +++ b/telegram/callbackquery.py @@ -329,7 +329,7 @@ def delete_message(self, *args: Any, **kwargs: Any) -> Union[Message, bool]: def pin_message(self, *args: Any, **kwargs: Any) -> bool: """Shortcut for:: - bot.pin_chat_message(chat_id=update.callback_query.message.chat_id, + bot.pin_chat_message(chat_id=message.chat_id, *args, **kwargs) @@ -342,7 +342,7 @@ def pin_message(self, *args: Any, **kwargs: Any) -> bool: def unpin_message(self, *args: Any, **kwargs: Any) -> bool: """Shortcut for:: - bot.unpin_chat_message(chat_id=update.callback_query.message.chat_id, + bot.unpin_chat_message(chat_id=message.chat_id, *args, **kwargs) From c937c06bc1302ca36c3c0ea6488ed7de339c26c0 Mon Sep 17 00:00:00 2001 From: Harshil Date: Tue, 10 Nov 2020 21:20:16 +0400 Subject: [PATCH 6/6] update docstring (review 4) --- telegram/bot.py | 2 +- telegram/callbackquery.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/telegram/bot.py b/telegram/bot.py index 12f8fa7cc34..6ba072824f4 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -3711,7 +3711,7 @@ def unpin_all_chat_messages( api_kwargs: JSONDict = None, ) -> bool: """ - Use this method to remove a message from the list of pinned messages in a chat. If the + Use this method to clear the list of pinned messages in a chat. If the chat is not a private chat, the bot must be an administrator in the chat for this to work and must have the ``can_pin_messages`` admin right in a supergroup or ``can_edit_messages`` admin right in a channel. diff --git a/telegram/callbackquery.py b/telegram/callbackquery.py index 930f74edfdb..98c3503d79b 100644 --- a/telegram/callbackquery.py +++ b/telegram/callbackquery.py @@ -330,6 +330,7 @@ def pin_message(self, *args: Any, **kwargs: Any) -> bool: """Shortcut for:: bot.pin_chat_message(chat_id=message.chat_id, + message_id=message.message_id, *args, **kwargs) @@ -343,6 +344,7 @@ def unpin_message(self, *args: Any, **kwargs: Any) -> bool: """Shortcut for:: bot.unpin_chat_message(chat_id=message.chat_id, + message_id=message.message_id, *args, **kwargs)