mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-14 19:48:57 +01:00
Merge branch 'master' into payment
This commit is contained in:
commit
834c1ab3c5
7 changed files with 67 additions and 26 deletions
|
@ -244,7 +244,13 @@ class Bot(TelegramObject):
|
|||
@log
|
||||
@message
|
||||
def delete_message(self, chat_id, message_id):
|
||||
"""Use this method to delete messages which were sent not later than 48 hours ago.
|
||||
"""Use this method to delete a message. A message can only be deleted if it was sent less
|
||||
than 48 hours ago. Any such recently sent outgoing message may be deleted. Additionally,
|
||||
if the bot is an administrator in a group chat, it can delete any message. If the bot is
|
||||
an administrator in a supergroup, it can delete messages from any other user and service
|
||||
messages about people joining or leaving the group (other types of service messages may
|
||||
only be removed by the group creator). In channels, bots can only remove their own
|
||||
messages.
|
||||
|
||||
Args:
|
||||
chat_id (int|str): Unique identifier for the target chat or
|
||||
|
@ -252,10 +258,6 @@ class Bot(TelegramObject):
|
|||
@channelusername).
|
||||
message_id (int): Unique message identifier.
|
||||
|
||||
Note:
|
||||
This method is not documented, so it's not guaranteed to work. Also, its behaviour can
|
||||
be changed at any time.
|
||||
|
||||
Returns:
|
||||
bool: On success, `True` is returned.
|
||||
|
||||
|
|
|
@ -30,7 +30,8 @@ class CommandHandler(Handler):
|
|||
name and/or some additional text.
|
||||
|
||||
Args:
|
||||
command (str): The name of the command this handler should listen for.
|
||||
command (str|list): The name of the command or list of command this handler should
|
||||
listen for.
|
||||
callback (function): A function that takes ``bot, update`` as
|
||||
positional arguments. It will be called when the ``check_update``
|
||||
has determined that an update should be processed by this handler.
|
||||
|
@ -79,7 +80,15 @@ class CommandHandler(Handler):
|
|||
pass_job_queue=pass_job_queue,
|
||||
pass_user_data=pass_user_data,
|
||||
pass_chat_data=pass_chat_data)
|
||||
self.command = command
|
||||
try:
|
||||
_str = basestring # Python 2
|
||||
except NameError:
|
||||
_str = str # Python 3
|
||||
|
||||
if isinstance(command, _str):
|
||||
self.command = [command]
|
||||
else:
|
||||
self.command = command
|
||||
self.filters = filters
|
||||
self.allow_edited = allow_edited
|
||||
self.pass_args = pass_args
|
||||
|
@ -108,7 +117,7 @@ class CommandHandler(Handler):
|
|||
else:
|
||||
res = self.filters(message)
|
||||
|
||||
return res and (message.text.startswith('/') and command[0] == self.command
|
||||
return res and (message.text.startswith('/') and command[0] in self.command
|
||||
and command[1].lower() == message.bot.username.lower())
|
||||
else:
|
||||
return False
|
||||
|
|
|
@ -24,6 +24,7 @@ from time import mktime
|
|||
|
||||
from telegram import (Audio, Contact, Document, Chat, Location, PhotoSize, Sticker, TelegramObject,
|
||||
User, Video, Voice, Venue, MessageEntity, Game, Invoice, SuccessfulPayment)
|
||||
from telegram.utils.helpers import escape_html, escape_markdown
|
||||
|
||||
|
||||
class Message(TelegramObject):
|
||||
|
@ -554,10 +555,6 @@ class Message(TelegramObject):
|
|||
... message_id=message.message_id,
|
||||
... *args, **kwargs)
|
||||
|
||||
Note:
|
||||
This method is not documented, so it's not guaranteed to work. Also, its behaviour can
|
||||
be changed at any time.
|
||||
|
||||
Returns:
|
||||
bool: On success, `True` is returned.
|
||||
|
||||
|
@ -639,6 +636,7 @@ class Message(TelegramObject):
|
|||
last_offset = 0
|
||||
|
||||
for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)):
|
||||
text = escape_html(text)
|
||||
|
||||
if entity.type == MessageEntity.TEXT_LINK:
|
||||
insert = '<a href="{}">{}</a>'.format(entity.url, text)
|
||||
|
@ -653,7 +651,7 @@ class Message(TelegramObject):
|
|||
else:
|
||||
insert = text
|
||||
|
||||
markdown_text += message_text[last_offset:entity.offset] + insert
|
||||
markdown_text += escape_html(message_text[last_offset:entity.offset]) + insert
|
||||
last_offset = entity.offset + entity.length
|
||||
|
||||
markdown_text += message_text[last_offset:]
|
||||
|
@ -677,6 +675,7 @@ class Message(TelegramObject):
|
|||
last_offset = 0
|
||||
|
||||
for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)):
|
||||
text = escape_markdown(text)
|
||||
|
||||
if entity.type == MessageEntity.TEXT_LINK:
|
||||
insert = '[{}]({})'.format(text, entity.url)
|
||||
|
@ -691,7 +690,7 @@ class Message(TelegramObject):
|
|||
else:
|
||||
insert = text
|
||||
|
||||
markdown_text += message_text[last_offset:entity.offset] + insert
|
||||
markdown_text += escape_markdown(message_text[last_offset:entity.offset]) + insert
|
||||
last_offset = entity.offset + entity.length
|
||||
|
||||
markdown_text += message_text[last_offset:]
|
||||
|
|
|
@ -20,6 +20,11 @@
|
|||
|
||||
import re
|
||||
|
||||
try:
|
||||
from html import escape as escape_html # noqa: F401
|
||||
except ImportError:
|
||||
from cgi import escape as escape_html # noqa: F401
|
||||
|
||||
|
||||
def escape_markdown(text):
|
||||
"""Helper function to escape telegram markup symbols"""
|
||||
|
|
|
@ -96,8 +96,6 @@ class BotTest(BaseTest, unittest.TestCase):
|
|||
@timeout(10)
|
||||
def test_deleteMessage_old_message(self):
|
||||
with self.assertRaisesRegexp(telegram.TelegramError, "can't be deleted"):
|
||||
# NOTE: This behaviour can be changed in future because `deleteMessage` method is not
|
||||
# documented in Bot API reference now.
|
||||
# Considering that the first message is old enough
|
||||
self._bot.delete_message(chat_id=self._chat_id, message_id=1)
|
||||
|
||||
|
|
|
@ -37,33 +37,33 @@ class MessageTest(BaseTest, unittest.TestCase):
|
|||
self.test_entities = [
|
||||
{
|
||||
'length': 4,
|
||||
'offset': 9,
|
||||
'offset': 10,
|
||||
'type': 'bold'
|
||||
},
|
||||
{
|
||||
'length': 6,
|
||||
'offset': 15,
|
||||
'length': 7,
|
||||
'offset': 16,
|
||||
'type': 'italic'
|
||||
},
|
||||
{
|
||||
'length': 4,
|
||||
'offset': 23,
|
||||
'offset': 25,
|
||||
'type': 'code'
|
||||
},
|
||||
{
|
||||
'length': 5,
|
||||
'offset': 29,
|
||||
'offset': 31,
|
||||
'type': 'text_link',
|
||||
'url': 'http://github.com/'
|
||||
},
|
||||
{
|
||||
'length': 3,
|
||||
'offset': 39,
|
||||
'offset': 41,
|
||||
'type': 'pre'
|
||||
},
|
||||
]
|
||||
|
||||
self.test_text = 'Test for bold, italic, code, links and pre.'
|
||||
self.test_text = 'Test for <bold, ita_lic, code, links and pre.'
|
||||
self.test_message = telegram.Message(
|
||||
message_id=1,
|
||||
from_user=None,
|
||||
|
@ -99,12 +99,12 @@ class MessageTest(BaseTest, unittest.TestCase):
|
|||
entity_2: 'h'})
|
||||
|
||||
def test_text_html(self):
|
||||
test_html_string = 'Test for <b>bold</b>, <i>italic</i>, <code>code</code>, ' '<a href="http://github.com/">links</a> and <pre>pre</pre>.'
|
||||
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>.'
|
||||
text_html = self.test_message.text_html
|
||||
self.assertEquals(test_html_string, text_html)
|
||||
|
||||
def test_text_markdown(self):
|
||||
test_md_string = 'Test for *bold*, _italic_, `code`, [links](http://github.com/) and ```pre```.'
|
||||
test_md_string = 'Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and ```pre```.'
|
||||
text_markdown = self.test_message.text_markdown
|
||||
self.assertEquals(test_md_string, text_markdown)
|
||||
|
||||
|
@ -152,7 +152,6 @@ class MessageTest(BaseTest, unittest.TestCase):
|
|||
reply_to_message_id=1)
|
||||
|
||||
with self.assertRaisesRegexp(telegram.TelegramError, "can't be deleted"):
|
||||
# NOTE: This behaviour can be changed in future. See `tests/test_bot.py` for more info
|
||||
message.reply_to_message.delete()
|
||||
|
||||
def test_equality(self):
|
||||
|
|
|
@ -300,6 +300,35 @@ class UpdaterTest(BaseTest, unittest.TestCase):
|
|||
sleep(.1)
|
||||
self.assertTrue(None is self.received_message)
|
||||
|
||||
def test_CommandHandler_commandList(self):
|
||||
self._setup_updater('', messages=0)
|
||||
handler = CommandHandler(['foo', 'bar', 'spameggs'], self.telegramHandlerTest)
|
||||
self.updater.dispatcher.add_handler(handler)
|
||||
bot = self.updater.bot
|
||||
user = User(0, 'TestUser')
|
||||
queue = self.updater.start_polling(0.01)
|
||||
|
||||
message = Message(0, user, 0, None, text='/foo', bot=bot)
|
||||
queue.put(Update(0, message=message))
|
||||
sleep(.1)
|
||||
self.assertEqual(self.received_message, '/foo')
|
||||
|
||||
message.text = '/bar'
|
||||
queue.put(Update(1, message=message))
|
||||
sleep(.1)
|
||||
self.assertEqual(self.received_message, '/bar')
|
||||
|
||||
message.text = '/spameggs'
|
||||
queue.put(Update(2, message=message))
|
||||
sleep(.1)
|
||||
self.assertEqual(self.received_message, '/spameggs')
|
||||
|
||||
self.reset()
|
||||
message.text = '/not_in_list'
|
||||
queue.put(Update(3, message=message))
|
||||
sleep(.1)
|
||||
self.assertTrue(self.received_message is None)
|
||||
|
||||
def test_addRemoveStringRegexHandler(self):
|
||||
self._setup_updater('', messages=0)
|
||||
d = self.updater.dispatcher
|
||||
|
|
Loading…
Add table
Reference in a new issue