New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added methods to quickly reply to a message with Markdown or HTML #827
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
63a702a
Added methods to quickly reply to a message with Markdown or HTML
JosXa 06734c9
Added methods to quickly reply to a message with Markdown or HTML
JosXa fb2323f
fix docstrings
Eldinnie b12fc3e
Merge remote-tracking branch 'origin/reply-text-markdown-2' into repl…
Eldinnie 5726863
Added notice to contribution guidelines
JosXa 5bd7115
Merge remote-tracking branch 'remotes/origin/master' into reply-text-…
JosXa 1b245b0
Added tests for reply_markdown and reply_html
JosXa 59b5502
Merge branch 'reply-text-markdown-2' of https://github.com/python-tel…
JosXa 69d9419
Removed mistaken urllib3 and files from another commit of mine
JosXa fb43963
Reset old issue template
JosXa 320a730
Removed wrong files
JosXa 239a9b0
Merge remote-tracking branch 'remotes/origin/master' into extract-urls
JosXa c86e175
Requested docstring changes
JosXa 8821292
Make flake8 happy
JosXa File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,10 @@ | |
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 | ||
|
||
|
||
_UNDEFINED = object() | ||
|
||
|
||
|
@@ -408,6 +408,46 @@ 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) | ||
|
||
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.sendMessage(update.message.chat_id, parse_mode=ParseMode.HTML, *args, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
|
||
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:: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -33,34 +34,52 @@ def message(bot): | |
|
||
@pytest.fixture(scope='function', | ||
params=[ | ||
{'forward_from': User(99, 'forward_user', False), | ||
'forward_date': datetime.now()}, | ||
{'forward_from_chat': Chat(-23, 'channel'), | ||
'forward_from_message_id': 101, | ||
'forward_date': datetime.now()}, | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please reset the dict to the way it was. |
||
'forward_from': User(99, 'forward_user', False), | ||
'forward_date': datetime.now() | ||
}, | ||
{ | ||
'forward_from_chat': Chat(-23, 'channel'), | ||
'forward_from_message_id': 101, | ||
'forward_date': datetime.now() | ||
}, | ||
{'reply_to_message': Message(50, None, None, None)}, | ||
{'edit_date': datetime.now()}, | ||
{'test': 'a text message', | ||
'enitites': [MessageEntity('bold', 10, 4), | ||
MessageEntity('italic', 16, 7)]}, | ||
{'audio': Audio('audio_id', 12), | ||
'caption': 'audio_file'}, | ||
{'document': Document('document_id'), | ||
'caption': 'document_file'}, | ||
{'game': Game('my_game', 'just my game', | ||
[PhotoSize('game_photo_id', 30, 30), ])}, | ||
{'photo': [PhotoSize('photo_id', 50, 50)], | ||
'caption': 'photo_file'}, | ||
{ | ||
'test': 'a text message', | ||
'enitites': [MessageEntity('bold', 10, 4), | ||
MessageEntity('italic', 16, 7)] | ||
}, | ||
{ | ||
'audio': Audio('audio_id', 12), | ||
'caption': 'audio_file' | ||
}, | ||
{ | ||
'document': Document('document_id'), | ||
'caption': 'document_file' | ||
}, | ||
{ | ||
'game': Game('my_game', 'just my game', | ||
[PhotoSize('game_photo_id', 30, 30), ]) | ||
}, | ||
{ | ||
'photo': [PhotoSize('photo_id', 50, 50)], | ||
'caption': 'photo_file' | ||
}, | ||
{'sticker': Sticker('sticker_id', 50, 50)}, | ||
{'video': Video('video_id', 12, 12, 12), | ||
'caption': 'video_file'}, | ||
{ | ||
'video': Video('video_id', 12, 12, 12), | ||
'caption': 'video_file' | ||
}, | ||
{'voice': Voice('voice_id', 5)}, | ||
{'video_note': VideoNote('video_note_id', 20, 12)}, | ||
{'new_chat_members': [User(55, 'new_user', False)]}, | ||
{'contact': Contact('phone_numner', 'contact_name')}, | ||
{'location': Location(-23.691288, 46.788279)}, | ||
{'venue': Venue(Location(-23.691288, 46.788279), | ||
'some place', 'right here')}, | ||
{ | ||
'venue': Venue(Location(-23.691288, 46.788279), | ||
'some place', 'right here') | ||
}, | ||
{'left_chat_member': User(33, 'kicked', False)}, | ||
{'new_chat_title': 'new title'}, | ||
{'new_chat_photo': [PhotoSize('photo_id', 50, 50)]}, | ||
|
@@ -72,9 +91,11 @@ def message(bot): | |
{'migrate_from_chat_id': -54321}, | ||
{'pinned_message': Message(7, None, None, None)}, | ||
{'invoice': Invoice('my invoice', 'invoice', 'start', 'EUR', 243)}, | ||
{'successful_payment': SuccessfulPayment('EUR', 243, 'payload', | ||
'charge_id', 'provider_id', | ||
order_info={})}, | ||
{ | ||
'successful_payment': SuccessfulPayment('EUR', 243, 'payload', | ||
'charge_id', 'provider_id', | ||
order_info={}) | ||
}, | ||
{'forward_signature': 'some_forward_sign'}, | ||
{'author_signature': 'some_author_sign'} | ||
], | ||
|
@@ -201,7 +222,6 @@ def test_effective_attachment(self, message_params): | |
item = None | ||
assert message_params.effective_attachment == item | ||
|
||
|
||
def test_reply_text(self, monkeypatch, message): | ||
def test(*args, **kwargs): | ||
id = args[1] == message.chat_id | ||
|
@@ -217,6 +237,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 <<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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the snake_case names in docs. (send_message)