Compare commits

...

3 Commits

Author SHA1 Message Date
Jannes Höke ba6c4fd517 Bump version to v6.0.1 2017-05-21 14:25:40 +02:00
Jannes Höke 1c4c228cf1 add support for User.language_code (#624)
* add support for User.language_code

* Add language filter

Useful is you wanna do something like restrict your shop to a single or a few locales or something like that.
2017-05-21 14:00:53 +02:00
Jacob Bom 2e89e21261 Fix text_markdown and text_html (#623)
* Fix text_markdown and text_html

* Missed a few narrow build checks

* Added tests for emoji-first strings and emojis in url
2017-05-21 14:00:07 +02:00
9 changed files with 136 additions and 18 deletions
+7
View File
@@ -2,6 +2,13 @@
Changes
=======
**2017-05-21**
*Released 6.0.1*
- Add support for ``User.language_code``
- Fix ``Message.text_html`` and ``Message.text_markdown`` for messages with emoji
**2017-05-19**
*Released 6.0.0*
+1 -1
View File
@@ -61,7 +61,7 @@ author = u'Leandro Toledo'
# The short X.Y version.
version = '6.0' # telegram.__version__[:3]
# The full version, including alpha/beta/rc tags.
release = '6.0.0' # telegram.__version__
release = '6.0.1' # telegram.__version__
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
+26
View File
@@ -19,6 +19,11 @@
""" This module contains the Filters for use with the MessageHandler class """
from telegram import Chat
try:
str_type = base_string
except NameError:
str_type = str
class BaseFilter(object):
"""Base class for all Message Filters
@@ -263,3 +268,24 @@ class Filters(object):
return message.chat.type in [Chat.GROUP, Chat.SUPERGROUP]
group = _Group()
class language(BaseFilter):
"""
Filters messages to only allow those which are from users with a certain language code.
Note that according to telegrams documentation, every single user does not have the
language_code attribute.
Args:
lang (str|list): Which language code(s) to allow through. This will be matched using
.startswith meaning that 'en' will match both 'en_US' and 'en_GB'
"""
def __init__(self, lang):
if isinstance(lang, str_type):
self.lang = [lang]
else:
self.lang = lang
def filter(self, message):
return message.from_user.language_code and any(
[message.from_user.language_code.startswith(x) for x in self.lang])
+25 -4
View File
@@ -628,6 +628,9 @@ class Message(TelegramObject):
"""
entities = self.parse_entities()
message_text = self.text
if not sys.maxunicode == 0xffff:
message_text = message_text.encode('utf-16-le')
markdown_text = ''
last_offset = 0
@@ -647,10 +650,18 @@ class Message(TelegramObject):
else:
insert = text
markdown_text += escape_html(message_text[last_offset:entity.offset]) + insert
if sys.maxunicode == 0xffff:
markdown_text += escape_html(message_text[last_offset:entity.offset]) + insert
else:
markdown_text += escape_html(message_text[last_offset * 2:entity.offset * 2]
.decode('utf-16-le')) + insert
last_offset = entity.offset + entity.length
markdown_text += escape_html(message_text[last_offset:])
if sys.maxunicode == 0xffff:
markdown_text += escape_html(message_text[last_offset:])
else:
markdown_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le'))
return markdown_text
@property
@@ -667,6 +678,9 @@ class Message(TelegramObject):
"""
entities = self.parse_entities()
message_text = self.text
if not sys.maxunicode == 0xffff:
message_text = message_text.encode('utf-16-le')
markdown_text = ''
last_offset = 0
@@ -685,9 +699,16 @@ class Message(TelegramObject):
insert = '```' + text + '```'
else:
insert = text
if sys.maxunicode == 0xffff:
markdown_text += escape_markdown(message_text[last_offset:entity.offset]) + insert
else:
markdown_text += escape_markdown(message_text[last_offset * 2:entity.offset * 2]
.decode('utf-16-le')) + insert
markdown_text += escape_markdown(message_text[last_offset:entity.offset]) + insert
last_offset = entity.offset + entity.length
markdown_text += escape_markdown(message_text[last_offset:])
if sys.maxunicode == 0xffff:
markdown_text += escape_markdown(message_text[last_offset:])
else:
markdown_text += escape_markdown(message_text[last_offset * 2:].decode('utf-16-le'))
return markdown_text
+14 -10
View File
@@ -26,21 +26,23 @@ class User(TelegramObject):
"""This object represents a Telegram User.
Attributes:
id (int):
first_name (str):
last_name (str):
username (str):
type (str):
id (int): Unique identifier for this user or bot
first_name (str): User's or bot's first name
last_name (str): User's or bot's last name
username (str): User's or bot's username
language_code (str): IETF language tag of the user's language
type (str): Deprecated
Args:
id (int):
first_name (str):
id (int): Unique identifier for this user or bot
first_name (str): User's or bot's first name
**kwargs: Arbitrary keyword arguments.
Keyword Args:
type (Optional[str]):
last_name (Optional[str]):
username (Optional[str]):
type (Optional[str]): Deprecated
last_name (Optional[str]): User's or bot's last name
username (Optional[str]): User's or bot's username
language_code (Optional[str]): IETF language tag of the user's language
bot (Optional[Bot]): The Bot to use for instance methods
"""
@@ -50,6 +52,7 @@ class User(TelegramObject):
type=None,
last_name=None,
username=None,
language_code=None,
bot=None,
**kwargs):
# Required
@@ -59,6 +62,7 @@ class User(TelegramObject):
self.type = type
self.last_name = last_name
self.username = username
self.language_code = language_code
self.bot = bot
+1 -1
View File
@@ -17,4 +17,4 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
__version__ = '6.0.0'
__version__ = '6.0.1'
+26
View File
@@ -274,6 +274,32 @@ class FiltersTest(BaseTest, unittest.TestCase):
with self.assertRaises(NotImplementedError):
(custom & Filters.text)(self.message)
def test_language_filter_single(self):
self.message.from_user.language_code = 'en_US'
self.assertTrue((Filters.language('en_US'))(self.message))
self.assertTrue((Filters.language('en'))(self.message))
self.assertFalse((Filters.language('en_GB'))(self.message))
self.assertFalse((Filters.language('da'))(self.message))
self.message.from_user.language_code = 'en_GB'
self.assertFalse((Filters.language('en_US'))(self.message))
self.assertTrue((Filters.language('en'))(self.message))
self.assertTrue((Filters.language('en_GB'))(self.message))
self.assertFalse((Filters.language('da'))(self.message))
self.message.from_user.language_code = 'da'
self.assertFalse((Filters.language('en_US'))(self.message))
self.assertFalse((Filters.language('en'))(self.message))
self.assertFalse((Filters.language('en_GB'))(self.message))
self.assertTrue((Filters.language('da'))(self.message))
def test_language_filter_multiple(self):
f = Filters.language(['en_US', 'da'])
self.message.from_user.language_code = 'en_US'
self.assertTrue(f(self.message))
self.message.from_user.language_code = 'en_GB'
self.assertFalse(f(self.message))
self.message.from_user.language_code = 'da'
self.assertTrue(f(self.message))
if __name__ == '__main__':
unittest.main()
+32 -2
View File
@@ -98,16 +98,46 @@ class MessageTest(BaseTest, unittest.TestCase):
{entity: 'http://google.com',
entity_2: 'h'})
def test_text_html(self):
def test_text_html_simple(self):
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>.'
text_html = self.test_message.text_html
self.assertEquals(test_html_string, text_html)
def test_text_markdown(self):
def test_text_markdown_simple(self):
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)
def test_text_html_emoji(self):
text = (b'\\U0001f469\\u200d\\U0001f469\\u200d ABC').decode('unicode-escape')
expected = (b'\\U0001f469\\u200d\\U0001f469\\u200d <b>ABC</b>').decode('unicode-escape')
bold_entity = telegram.MessageEntity(type=telegram.MessageEntity.BOLD, offset=7, length=3)
message = telegram.Message(
message_id=1, from_user=None, date=None, chat=None, text=text, entities=[bold_entity])
self.assertEquals(expected, message.text_html)
def test_text_markdown_emoji(self):
text = (b'\\U0001f469\\u200d\\U0001f469\\u200d ABC').decode('unicode-escape')
expected = (b'\\U0001f469\\u200d\\U0001f469\\u200d *ABC*').decode('unicode-escape')
bold_entity = telegram.MessageEntity(type=telegram.MessageEntity.BOLD, offset=7, length=3)
message = telegram.Message(
message_id=1, from_user=None, date=None, chat=None, text=text, entities=[bold_entity])
self.assertEquals(expected, message.text_markdown)
def test_parse_entities_url_emoji(self):
url = b'http://github.com/?unicode=\\u2713\\U0001f469'.decode('unicode-escape')
text = 'some url'
link_entity = telegram.MessageEntity(type=telegram.MessageEntity.URL, offset=0, length=8, url=url)
message = telegram.Message(
message_id=1,
from_user=None,
date=None,
chat=None,
text=text,
entities=[link_entity])
self.assertDictEqual(message.parse_entities(), {link_entity: text})
self.assertEqual(next(iter(message.parse_entities())).url, url)
@flaky(3, 1)
def test_reply_text(self):
"""Test for Message.reply_text"""
+4
View File
@@ -37,6 +37,7 @@ class UserTest(BaseTest, unittest.TestCase):
self.first_name = "Leandro"
self.last_name = "S."
self.username = "leandrotoledo"
self.language_code = "pt-BR"
self.type = "private"
self.json_dict = {
@@ -44,6 +45,7 @@ class UserTest(BaseTest, unittest.TestCase):
'first_name': self.first_name,
'last_name': self.last_name,
'username': self.username,
'language_code': self.language_code,
'type': self.type
}
@@ -54,6 +56,7 @@ class UserTest(BaseTest, unittest.TestCase):
self.assertEqual(user.first_name, self.first_name)
self.assertEqual(user.last_name, self.last_name)
self.assertEqual(user.username, self.username)
self.assertEqual(user.language_code, self.language_code)
self.assertEqual(user.type, self.type)
self.assertEqual(user.name, '@leandrotoledo')
@@ -98,6 +101,7 @@ class UserTest(BaseTest, unittest.TestCase):
self.assertEqual(user['first_name'], self.first_name)
self.assertEqual(user['last_name'], self.last_name)
self.assertEqual(user['username'], self.username)
self.assertEqual(user['language_code'], self.language_code)
self.assertEqual(user['type'], self.type)
@flaky(3, 1)