Skip to content

Commit f0dfdfb

Browse files
JosXatsnoam
authored andcommitted
bot.py: Add shortcut methods reply_{markdown,html} (python-telegram-bot#827)
1 parent c19e464 commit f0dfdfb

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

telegram/message.py

Lines changed: 42 additions & 0 deletions
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

@@ -426,6 +427,47 @@ def reply_text(self, *args, **kwargs):
426427
self._quote(kwargs)
427428
return self.bot.send_message(self.chat_id, *args, **kwargs)
428429

430+
def reply_markdown(self, *args, **kwargs):
431+
"""Shortcut for::
432+
433+
bot.send_message(update.message.chat_id, parse_mode=ParseMode.MARKDOWN, *args,
434+
**kwargs)
435+
436+
Sends a message with markdown formatting.
437+
438+
Keyword Args:
439+
quote (:obj:`bool`, optional): If set to ``True``, the message is sent as an actual
440+
reply to this message. If ``reply_to_message_id`` is passed in ``kwargs``, this
441+
parameter will be ignored. Default: ``True`` in group chats and ``False`` in
442+
private chats.
443+
"""
444+
445+
kwargs['parse_mode'] = ParseMode.MARKDOWN
446+
447+
self._quote(kwargs)
448+
449+
return self.bot.send_message(self.chat_id, *args, **kwargs)
450+
451+
def reply_html(self, *args, **kwargs):
452+
"""Shortcut for::
453+
454+
bot.send_message(update.message.chat_id, parse_mode=ParseMode.HTML, *args, **kwargs)
455+
456+
Sends a message with HTML formatting.
457+
458+
Keyword Args:
459+
quote (:obj:`bool`, optional): If set to ``True``, the message is sent as an actual
460+
reply to this message. If ``reply_to_message_id`` is passed in ``kwargs``, this
461+
parameter will be ignored. Default: ``True`` in group chats and ``False`` in
462+
private chats.
463+
"""
464+
465+
kwargs['parse_mode'] = ParseMode.HTML
466+
467+
self._quote(kwargs)
468+
469+
return self.bot.send_message(self.chat_id, *args, **kwargs)
470+
429471
def reply_photo(self, *args, **kwargs):
430472
"""Shortcut for::
431473

tests/test_message.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import pytest
2222

23+
from telegram import ParseMode
2324
from telegram import (Update, Message, User, MessageEntity, Chat, Audio, Document,
2425
Game, PhotoSize, Sticker, Video, Voice, VideoNote, Contact, Location, Venue,
2526
Invoice, SuccessfulPayment)
@@ -241,6 +242,55 @@ def test(*args, **kwargs):
241242
assert message.reply_text('test', quote=True)
242243
assert message.reply_text('test', reply_to_message_id=message.message_id, quote=True)
243244

245+
def test_reply_markdown(self, monkeypatch, message):
246+
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and '
247+
'```pre```. http://google.com')
248+
249+
def test(*args, **kwargs):
250+
cid = args[1] == message.chat_id
251+
markdown_text = args[2] == test_md_string
252+
markdown_enabled = kwargs['parse_mode'] == ParseMode.MARKDOWN
253+
if kwargs.get('reply_to_message_id'):
254+
reply = kwargs['reply_to_message_id'] == message.message_id
255+
else:
256+
reply = True
257+
return all([cid, markdown_text, reply, markdown_enabled])
258+
259+
text_markdown = self.test_message.text_markdown
260+
assert text_markdown == test_md_string
261+
262+
monkeypatch.setattr('telegram.Bot.send_message', test)
263+
assert message.reply_markdown(self.test_message.text_markdown)
264+
assert message.reply_markdown(self.test_message.text_markdown, quote=True)
265+
assert message.reply_markdown(self.test_message.text_markdown,
266+
reply_to_message_id=message.message_id,
267+
quote=True)
268+
269+
def test_reply_html(self, monkeypatch, message):
270+
test_html_string = ('Test for &lt;<b>bold</b>, <i>ita_lic</i>, <code>code</code>, '
271+
'<a href="http://github.com/">links</a> and <pre>pre</pre>. '
272+
'http://google.com')
273+
274+
def test(*args, **kwargs):
275+
cid = args[1] == message.chat_id
276+
html_text = args[2] == test_html_string
277+
html_enabled = kwargs['parse_mode'] == ParseMode.HTML
278+
if kwargs.get('reply_to_message_id'):
279+
reply = kwargs['reply_to_message_id'] == message.message_id
280+
else:
281+
reply = True
282+
return all([cid, html_text, reply, html_enabled])
283+
284+
text_html = self.test_message.text_html
285+
assert text_html == test_html_string
286+
287+
monkeypatch.setattr('telegram.Bot.send_message', test)
288+
assert message.reply_html(self.test_message.text_html)
289+
assert message.reply_html(self.test_message.text_html, quote=True)
290+
assert message.reply_html(self.test_message.text_html,
291+
reply_to_message_id=message.message_id,
292+
quote=True)
293+
244294
def test_reply_photo(self, monkeypatch, message):
245295
def test(*args, **kwargs):
246296
id = args[1] == message.chat_id

0 commit comments

Comments
 (0)