mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 06:25:12 +01:00
bot.py: Add shortcut methods reply_{markdown,html} (#827)
This commit is contained in:
parent
c19e464324
commit
f0dfdfb203
2 changed files with 92 additions and 0 deletions
|
@ -23,6 +23,7 @@ import sys
|
|||
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
|
||||
|
||||
|
@ -426,6 +427,47 @@ class Message(TelegramObject):
|
|||
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::
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ from datetime import datetime
|
|||
|
||||
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)
|
||||
|
@ -241,6 +242,55 @@ class TestMessage(object):
|
|||
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 <<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
|
||||
|
|
Loading…
Reference in a new issue