Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion telegram/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from telegram import (Audio, Contact, Document, Chat, Location, PhotoSize, Sticker, TelegramObject,
User, Video, Voice, Venue, MessageEntity, Game, Invoice, SuccessfulPayment,
VideoNote)
from telegram import ParseMode
from telegram.utils.deprecate import warn_deprecate_obj
from telegram.utils.helpers import escape_html, escape_markdown, to_timestamp, from_timestamp

Expand Down Expand Up @@ -305,6 +306,40 @@ def reply_text(self, *args, **kwargs):
self._quote(kwargs)
return self.bot.send_message(self.chat_id, *args, **kwargs)

def reply_markdown(self, *args, **kwargs):
"""
Shortcut for ``bot.sendMessage(update.message.chat_id, parse_mode=ParseMode.MARKDOWN, *args, **kwargs)``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change to:

Shortcut for::

<two time indent>bot.sendMessage(update.message.chat_id, parse_mode=ParseMode.MARKDOWN, *args, **kwargs)

Sends a message with markdown formatting.

Keyword Args:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Args: (without keyword)

quote (Optional[bool]): If set to ``True``, the message is sent as an actual reply to
this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent continuing line with two indents.

will be ignored. Default: ``True`` in group chats and ``False`` in private chats.
"""

kwargs['parse_mode'] = ParseMode.MARKDOWN

self._quote(kwargs)

return self.bot.sendMessage(self.chat_id, *args, **kwargs)

def reply_html(self, *args, **kwargs):
"""
Shortcut for ``bot.sendMessage(update.message.chat_id, parse_mode=ParseMode.HTML, *args, **kwargs)``
Sends a message with HTML formatting.

Keyword Args:
quote (Optional[bool]): If set to ``True``, the message is sent as an actual reply to
this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter
will be ignored. Default: ``True`` in group chats and ``False`` in private chats.
"""

kwargs['parse_mode'] = ParseMode.HTML

self._quote(kwargs)

return self.bot.sendMessage(self.chat_id, *args, **kwargs)

def reply_photo(self, *args, **kwargs):
"""
Shortcut for ``bot.send_photo(update.message.chat_id, *args, **kwargs)``
Expand Down Expand Up @@ -510,6 +545,48 @@ def edit_text(self, *args, **kwargs):
return self.bot.edit_message_text(
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)

def edit_markdown(self, *args, **kwargs):
"""
Shortcut for

>>> bot.editMessageText(chat_id=message.chat_id,
... message_id=message.message_id,
... parse_mode=ParseMode.MARKDOWN,
... *args, **kwargs)

Note:
You can only edit messages that the bot sent itself,
therefore this method can only be used on the
return value of the ``bot.send_*`` family of methods.

"""

kwargs['parse_mode'] = ParseMode.MARKDOWN

return self.bot.edit_message_text(
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)

def edit_html(self, *args, **kwargs):
"""
Shortcut for

>>> bot.editMessageText(chat_id=message.chat_id,
... message_id=message.message_id,
... parse_mode=ParseMode.HTML
... *args, **kwargs)

Note:
You can only edit messages that the bot sent itself,
therefore this method can only be used on the
return value of the ``bot.send_*`` family of methods.

"""

kwargs['parse_mode'] = ParseMode.HTML

return self.bot.edit_message_text(
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)

def edit_caption(self, *args, **kwargs):
"""
Shortcut for
Expand Down Expand Up @@ -610,7 +687,7 @@ def parse_entities(self, types=None):
return {
entity: self.parse_entity(entity)
for entity in self.entities if entity.type in types
}
}

@property
def text_html(self):
Expand Down
40 changes: 40 additions & 0 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,26 @@ def test_reply_text(self):
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.text, 'Testing class method')

@flaky(3, 1)
def test_reply_markdown(self):
"""Test for Message.reply_text_markdown"""
message = self._bot.sendMessage(self._chat_id, '.')
text = 'Testing class method *with* _markdown_ `enabled`.'
message = message.reply_markdown(text)

self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.text_markdown, text)

@flaky(3, 1)
def test_reply_html(self):
"""Test for Message.reply_text_html"""
message = self._bot.sendMessage(self._chat_id, '.')
text = 'Testing class method <b>with</b> <i>html</i> <code>enabled</code>.'
message = message.reply_html(text)

self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.text_html, text)

@flaky(3, 1)
def test_forward(self):
"""Test for Message.forward"""
Expand All @@ -165,6 +185,26 @@ def test_edit_text(self):
self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.text, 'Testing class method')

@flaky(3, 1)
def test_edit_markdown(self):
"""Test for Message.edit_text"""
message = self._bot.sendMessage(self._chat_id, '.')
text = 'Testing class method *with* _markdown_ `enabled`.'
message = message.edit_markdown(text)

self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.text_markdown, text)

@flaky(3, 1)
def test_edit_html(self):
"""Test for Message.edit_text"""
message = self._bot.sendMessage(self._chat_id, '.')
text = 'Testing class method <b>with</b> <i>html</i> <code>enabled</code>.'
message = message.edit_html(text)

self.assertTrue(self.is_json(message.to_json()))
self.assertEqual(message.text_html, text)

@flaky(3, 1)
def test_delete1(self):
"""Test for Message.delete"""
Expand Down