Skip to content
Merged
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
42 changes: 42 additions & 0 deletions 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 @@ -426,6 +427,47 @@ 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.send_message(update.message.chat_id, parse_mode=ParseMode.MARKDOWN, *args,
**kwargs)

Sends a message with markdown formatting.

Keyword Args:
quote (:obj:`bool`, optional): 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.MARKDOWN

self._quote(kwargs)

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

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

bot.send_message(update.message.chat_id, parse_mode=ParseMode.HTML, *args, **kwargs)

Sends a message with HTML formatting.

Keyword Args:
quote (:obj:`bool`, optional): 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.send_message(self.chat_id, *args, **kwargs)

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

Expand Down
50 changes: 50 additions & 0 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import pytest

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

def test_reply_markdown(self, monkeypatch, message):
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and '
'```pre```. http://google.com')

def test(*args, **kwargs):
cid = args[1] == message.chat_id
markdown_text = args[2] == test_md_string
markdown_enabled = kwargs['parse_mode'] == ParseMode.MARKDOWN
if kwargs.get('reply_to_message_id'):
reply = kwargs['reply_to_message_id'] == message.message_id
else:
reply = True
return all([cid, markdown_text, reply, markdown_enabled])

text_markdown = self.test_message.text_markdown
assert text_markdown == test_md_string

monkeypatch.setattr('telegram.Bot.send_message', test)
assert message.reply_markdown(self.test_message.text_markdown)
assert message.reply_markdown(self.test_message.text_markdown, quote=True)
assert message.reply_markdown(self.test_message.text_markdown,
reply_to_message_id=message.message_id,
quote=True)

def test_reply_html(self, monkeypatch, message):
test_html_string = ('Test for &lt;<b>bold</b>, <i>ita_lic</i>, <code>code</code>, '
'<a href="http://github.com/">links</a> and <pre>pre</pre>. '
'http://google.com')

def test(*args, **kwargs):
cid = args[1] == message.chat_id
html_text = args[2] == test_html_string
html_enabled = kwargs['parse_mode'] == ParseMode.HTML
if kwargs.get('reply_to_message_id'):
reply = kwargs['reply_to_message_id'] == message.message_id
else:
reply = True
return all([cid, html_text, reply, html_enabled])

text_html = self.test_message.text_html
assert text_html == test_html_string

monkeypatch.setattr('telegram.Bot.send_message', test)
assert message.reply_html(self.test_message.text_html)
assert message.reply_html(self.test_message.text_html, quote=True)
assert message.reply_html(self.test_message.text_html,
reply_to_message_id=message.message_id,
quote=True)

def test_reply_photo(self, monkeypatch, message):
def test(*args, **kwargs):
id = args[1] == message.chat_id
Expand Down