Skip to content

Commit 135f2c0

Browse files
committed
Added methods to quickly reply to a message with Markdown or HTML
1 parent f72f409 commit 135f2c0

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

telegram/message.py

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from telegram import (Audio, Contact, Document, Chat, Location, PhotoSize, Sticker, TelegramObject,
2424
User, Video, Voice, Venue, MessageEntity, Game, Invoice, SuccessfulPayment,
2525
VideoNote)
26+
from telegram import ParseMode
2627
from telegram.utils.deprecate import warn_deprecate_obj
2728
from telegram.utils.helpers import escape_html, escape_markdown, to_timestamp, from_timestamp
2829

@@ -305,6 +306,40 @@ def reply_text(self, *args, **kwargs):
305306
self._quote(kwargs)
306307
return self.bot.send_message(self.chat_id, *args, **kwargs)
307308

309+
def reply_markdown(self, *args, **kwargs):
310+
"""
311+
Shortcut for ``bot.sendMessage(update.message.chat_id, parse_mode=ParseMode.MARKDOWN, *args, **kwargs)``
312+
Sends a message with markdown formatting.
313+
314+
Keyword Args:
315+
quote (Optional[bool]): If set to ``True``, the message is sent as an actual reply to
316+
this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter
317+
will be ignored. Default: ``True`` in group chats and ``False`` in private chats.
318+
"""
319+
320+
kwargs['parse_mode'] = ParseMode.MARKDOWN
321+
322+
self._quote(kwargs)
323+
324+
return self.bot.sendMessage(self.chat_id, *args, **kwargs)
325+
326+
def reply_html(self, *args, **kwargs):
327+
"""
328+
Shortcut for ``bot.sendMessage(update.message.chat_id, parse_mode=ParseMode.HTML, *args, **kwargs)``
329+
Sends a message with HTML formatting.
330+
331+
Keyword Args:
332+
quote (Optional[bool]): If set to ``True``, the message is sent as an actual reply to
333+
this message. If ``reply_to_message_id`` is passed in ``kwargs``, this parameter
334+
will be ignored. Default: ``True`` in group chats and ``False`` in private chats.
335+
"""
336+
337+
kwargs['parse_mode'] = ParseMode.HTML
338+
339+
self._quote(kwargs)
340+
341+
return self.bot.sendMessage(self.chat_id, *args, **kwargs)
342+
308343
def reply_photo(self, *args, **kwargs):
309344
"""
310345
Shortcut for ``bot.send_photo(update.message.chat_id, *args, **kwargs)``
@@ -510,6 +545,48 @@ def edit_text(self, *args, **kwargs):
510545
return self.bot.edit_message_text(
511546
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)
512547

548+
def edit_markdown(self, *args, **kwargs):
549+
"""
550+
Shortcut for
551+
552+
>>> bot.editMessageText(chat_id=message.chat_id,
553+
... message_id=message.message_id,
554+
... parse_mode=ParseMode.MARKDOWN,
555+
... *args, **kwargs)
556+
557+
Note:
558+
You can only edit messages that the bot sent itself,
559+
therefore this method can only be used on the
560+
return value of the ``bot.send_*`` family of methods.
561+
562+
"""
563+
564+
kwargs['parse_mode'] = ParseMode.MARKDOWN
565+
566+
return self.bot.edit_message_text(
567+
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)
568+
569+
def edit_html(self, *args, **kwargs):
570+
"""
571+
Shortcut for
572+
573+
>>> bot.editMessageText(chat_id=message.chat_id,
574+
... message_id=message.message_id,
575+
... parse_mode=ParseMode.HTML
576+
... *args, **kwargs)
577+
578+
Note:
579+
You can only edit messages that the bot sent itself,
580+
therefore this method can only be used on the
581+
return value of the ``bot.send_*`` family of methods.
582+
583+
"""
584+
585+
kwargs['parse_mode'] = ParseMode.HTML
586+
587+
return self.bot.edit_message_text(
588+
chat_id=self.chat_id, message_id=self.message_id, *args, **kwargs)
589+
513590
def edit_caption(self, *args, **kwargs):
514591
"""
515592
Shortcut for
@@ -610,7 +687,7 @@ def parse_entities(self, types=None):
610687
return {
611688
entity: self.parse_entity(entity)
612689
for entity in self.entities if entity.type in types
613-
}
690+
}
614691

615692
@property
616693
def text_html(self):

tests/test_message.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ def test_reply_text(self):
147147
self.assertTrue(self.is_json(message.to_json()))
148148
self.assertEqual(message.text, 'Testing class method')
149149

150+
@flaky(3, 1)
151+
def test_reply_markdown(self):
152+
"""Test for Message.reply_text_markdown"""
153+
message = self._bot.sendMessage(self._chat_id, '.')
154+
text = 'Testing class method *with* _markdown_ `enabled`.'
155+
message = message.reply_markdown(text)
156+
157+
self.assertTrue(self.is_json(message.to_json()))
158+
self.assertEqual(message.text_markdown, text)
159+
160+
@flaky(3, 1)
161+
def test_reply_html(self):
162+
"""Test for Message.reply_text_html"""
163+
message = self._bot.sendMessage(self._chat_id, '.')
164+
text = 'Testing class method <b>with</b> <i>html</i> <code>enabled</code>.'
165+
message = message.reply_html(text)
166+
167+
self.assertTrue(self.is_json(message.to_json()))
168+
self.assertEqual(message.text_html, text)
169+
150170
@flaky(3, 1)
151171
def test_forward(self):
152172
"""Test for Message.forward"""
@@ -165,6 +185,26 @@ def test_edit_text(self):
165185
self.assertTrue(self.is_json(message.to_json()))
166186
self.assertEqual(message.text, 'Testing class method')
167187

188+
@flaky(3, 1)
189+
def test_edit_markdown(self):
190+
"""Test for Message.edit_text"""
191+
message = self._bot.sendMessage(self._chat_id, '.')
192+
text = 'Testing class method *with* _markdown_ `enabled`.'
193+
message = message.edit_markdown(text)
194+
195+
self.assertTrue(self.is_json(message.to_json()))
196+
self.assertEqual(message.text_markdown, text)
197+
198+
@flaky(3, 1)
199+
def test_edit_html(self):
200+
"""Test for Message.edit_text"""
201+
message = self._bot.sendMessage(self._chat_id, '.')
202+
text = 'Testing class method <b>with</b> <i>html</i> <code>enabled</code>.'
203+
message = message.edit_html(text)
204+
205+
self.assertTrue(self.is_json(message.to_json()))
206+
self.assertEqual(message.text_html, text)
207+
168208
@flaky(3, 1)
169209
def test_delete1(self):
170210
"""Test for Message.delete"""

0 commit comments

Comments
 (0)