From f3b75d95c424cd2d3bc165cd6fdc3c52146a78c7 Mon Sep 17 00:00:00 2001 From: Evgen Date: Fri, 12 May 2017 20:40:57 +0500 Subject: [PATCH] Implemented undocumented 'deleteMessage' method (#602) * Implemented `Bot.delete_message()` and `Message.delete()` methods * Added tests for `Bot.delete_message()` and `Messsage.delete()` * Added myself to `AUTHORS.rst` * Using `assertRaisesRegexp` instead of `assertRaisesRegex` in tests Now tests can run on Python 2.7 without raising 'AttributeError' * Fix docstring Added a warning to the docstring describing that this method is undocumented and not guaranteed to work --- AUTHORS.rst | 1 + telegram/bot.py | 29 +++++++++++++++++++++++++++++ telegram/message.py | 19 +++++++++++++++++++ tests/test_bot.py | 19 +++++++++++++++++++ tests/test_message.py | 20 ++++++++++++++++++++ 5 files changed, 88 insertions(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index b77549b76..e950e031c 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -24,6 +24,7 @@ The following wonderful people contributed directly or indirectly to this projec - `Eli Gao `_ - `ErgoZ Riftbit Vaper `_ - `Eugene Lisitsky `_ +- `evgfilim1 `_ - `franciscod `_ - `Hugo Damer `_ - `Jacob Bom `_ diff --git a/telegram/bot.py b/telegram/bot.py index 846bfe721..43a3e4450 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -241,6 +241,34 @@ class Bot(TelegramObject): return url, data + @log + @message + def delete_message(self, chat_id, message_id): + """Use this method to delete messages which were sent not later than 48 hours ago. + + Args: + chat_id (str): Unique identifier for the target chat or + username of the target channel (in the format + @channelusername). + message_id (int): Unique message identifier. + + Note: + This method is not documented, so it's not guaranteed to work. Also, its behaviour can + be changed at any time. + + Returns: + bool: On success, `True` is returned. + + Raises: + :class:`telegram.TelegramError` + + """ + url = '{0}/deleteMessage'.format(self.base_url) + + data = {'chat_id': chat_id, 'message_id': message_id} + + return url, data + @log @message def forward_message(self, @@ -1739,6 +1767,7 @@ class Bot(TelegramObject): # camelCase aliases getMe = get_me sendMessage = send_message + deleteMessage = delete_message forwardMessage = forward_message sendPhoto = send_photo sendAudio = send_audio diff --git a/telegram/message.py b/telegram/message.py index 39c564e1f..c69292e36 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -540,6 +540,25 @@ class Message(TelegramObject): return self.bot.edit_message_reply_markup( chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs) + def delete(self, *args, **kwargs): + """ + Shortcut for + + >>> bot.delete_message(chat_id=message.chat_id, + ... message_id=message.message_id, + ... *args, **kwargs) + + Note: + This method is not documented, so it's not guaranteed to work. Also, its behaviour can + be changed at any time. + + Returns: + bool: On success, `True` is returned. + + """ + return self.bot.delete_message( + chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs) + def parse_entity(self, entity): """ Returns the text from a given :class:`telegram.MessageEntity`. diff --git a/tests/test_bot.py b/tests/test_bot.py index d92348c55..dd414261a 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -82,6 +82,25 @@ class BotTest(BaseTest, unittest.TestCase): self.assertTrue(self.is_json(message.to_json())) self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей') + @flaky(3, 1) + @timeout(10) + def test_deleteMessage(self): + message = self._bot.send_message( + chat_id=self._chat_id, text='This message will be deleted') + + self.assertTrue( + self._bot.delete_message( + chat_id=self._chat_id, message_id=message.message_id)) + + @flaky(3, 1) + @timeout(10) + def test_deleteMessage_old_message(self): + with self.assertRaisesRegexp(telegram.TelegramError, "can't be deleted"): + # NOTE: This behaviour can be changed in future because `deleteMessage` method is not + # documented in Bot API reference now. + # Considering that the first message is old enough + self._bot.delete_message(chat_id=self._chat_id, message_id=1) + @flaky(3, 1) @timeout(10) def testGetUpdates(self): diff --git a/tests/test_message.py b/tests/test_message.py index 7f5c14676..8b8b4870c 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -136,6 +136,26 @@ class MessageTest(BaseTest, unittest.TestCase): self.assertTrue(self.is_json(message.to_json())) self.assertEqual(message.text, 'Testing class method') + @flaky(3, 1) + def test_delete1(self): + """Test for Message.delete""" + message = self._bot.send_message( + chat_id=self._chat_id, text='This message will be deleted') + + self.assertTrue(message.delete()) + + @flaky(3, 1) + def test_delete2(self): + """Another test for Message.delete""" + message = self._bot.send_message( + chat_id=self._chat_id, + text='This ^ message will not be deleted', + reply_to_message_id=1) + + with self.assertRaisesRegexp(telegram.TelegramError, "can't be deleted"): + # NOTE: This behaviour can be changed in future. See `tests/test_bot.py` for more info + message.reply_to_message.delete() + if __name__ == '__main__': unittest.main()