mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-27 08:50:38 +01:00
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
This commit is contained in:
parent
36b98b0ab4
commit
f3b75d95c4
5 changed files with 88 additions and 0 deletions
|
@ -24,6 +24,7 @@ The following wonderful people contributed directly or indirectly to this projec
|
||||||
- `Eli Gao <https://github.com/eligao>`_
|
- `Eli Gao <https://github.com/eligao>`_
|
||||||
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
|
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
|
||||||
- `Eugene Lisitsky <https://github.com/lisitsky>`_
|
- `Eugene Lisitsky <https://github.com/lisitsky>`_
|
||||||
|
- `evgfilim1 <https://github.com/evgfilim1>`_
|
||||||
- `franciscod <https://github.com/franciscod>`_
|
- `franciscod <https://github.com/franciscod>`_
|
||||||
- `Hugo Damer <https://github.com/HakimusGIT>`_
|
- `Hugo Damer <https://github.com/HakimusGIT>`_
|
||||||
- `Jacob Bom <https://github.com/bomjacob>`_
|
- `Jacob Bom <https://github.com/bomjacob>`_
|
||||||
|
|
|
@ -241,6 +241,34 @@ class Bot(TelegramObject):
|
||||||
|
|
||||||
return url, data
|
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
|
@log
|
||||||
@message
|
@message
|
||||||
def forward_message(self,
|
def forward_message(self,
|
||||||
|
@ -1739,6 +1767,7 @@ class Bot(TelegramObject):
|
||||||
# camelCase aliases
|
# camelCase aliases
|
||||||
getMe = get_me
|
getMe = get_me
|
||||||
sendMessage = send_message
|
sendMessage = send_message
|
||||||
|
deleteMessage = delete_message
|
||||||
forwardMessage = forward_message
|
forwardMessage = forward_message
|
||||||
sendPhoto = send_photo
|
sendPhoto = send_photo
|
||||||
sendAudio = send_audio
|
sendAudio = send_audio
|
||||||
|
|
|
@ -540,6 +540,25 @@ class Message(TelegramObject):
|
||||||
return self.bot.edit_message_reply_markup(
|
return self.bot.edit_message_reply_markup(
|
||||||
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)
|
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):
|
def parse_entity(self, entity):
|
||||||
"""
|
"""
|
||||||
Returns the text from a given :class:`telegram.MessageEntity`.
|
Returns the text from a given :class:`telegram.MessageEntity`.
|
||||||
|
|
|
@ -82,6 +82,25 @@ class BotTest(BaseTest, unittest.TestCase):
|
||||||
self.assertTrue(self.is_json(message.to_json()))
|
self.assertTrue(self.is_json(message.to_json()))
|
||||||
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
|
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)
|
@flaky(3, 1)
|
||||||
@timeout(10)
|
@timeout(10)
|
||||||
def testGetUpdates(self):
|
def testGetUpdates(self):
|
||||||
|
|
|
@ -136,6 +136,26 @@ class MessageTest(BaseTest, unittest.TestCase):
|
||||||
self.assertTrue(self.is_json(message.to_json()))
|
self.assertTrue(self.is_json(message.to_json()))
|
||||||
self.assertEqual(message.text, 'Testing class method')
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Add table
Reference in a new issue