add text mention for message parse (#1206)

* add text mention for message parse

* add author

* Update .gitignore
This commit is contained in:
Ehsan 2018-09-25 21:37:55 +03:30 committed by Jasmin Bom
parent 439790375e
commit 3829666a53
4 changed files with 46 additions and 28 deletions

View file

@ -23,6 +23,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `d-qoi <https://github.com/d-qoi>`_
- `daimajia <https://github.com/daimajia>`_
- `Daniel Reed <https://github.com/nmlorg>`_
- `Ehsan Online <https://github.com/ehsanonline>`_
- `Eli Gao <https://github.com/eligao>`_
- `Emilio Molinari <https://github.com/xates>`_
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_

View file

@ -943,7 +943,9 @@ class Message(TelegramObject):
if entity.type == MessageEntity.TEXT_LINK:
insert = '<a href="{}">{}</a>'.format(entity.url, text)
elif (entity.type == MessageEntity.URL) and urled:
elif entity.type == MessageEntity.TEXT_MENTION and entity.user:
insert = '<a href="tg://user?id={}">{}</a>'.format(entity.user.id, text)
elif entity.type == MessageEntity.URL and urled:
insert = '<a href="{0}">{0}</a>'.format(text)
elif entity.type == MessageEntity.BOLD:
insert = '<b>' + text + '</b>'
@ -1040,7 +1042,9 @@ class Message(TelegramObject):
if entity.type == MessageEntity.TEXT_LINK:
insert = '[{}]({})'.format(text, entity.url)
elif (entity.type == MessageEntity.URL) and urled:
elif entity.type == MessageEntity.TEXT_MENTION and entity.user:
insert = '[{}](tg://user?id={})'.format(text, entity.user.id)
elif entity.type == MessageEntity.URL and urled:
insert = '[{0}]({0})'.format(text)
elif entity.type == MessageEntity.BOLD:
insert = '*' + text + '*'

View file

@ -114,9 +114,11 @@ class TestMessage(object):
{'length': 7, 'offset': 16, 'type': 'italic'},
{'length': 4, 'offset': 25, 'type': 'code'},
{'length': 5, 'offset': 31, 'type': 'text_link', 'url': 'http://github.com/'},
{'length': 3, 'offset': 41, 'type': 'pre'},
{'length': 17, 'offset': 46, 'type': 'url'}]
test_text = 'Test for <bold, ita_lic, code, links and pre. http://google.com'
{'length': 12, 'offset': 38, 'type': 'text_mention',
'user': User(123456789, 'mentioned user', False)},
{'length': 3, 'offset': 55, 'type': 'pre'},
{'length': 17, 'offset': 60, 'type': 'url'}]
test_text = 'Test for <bold, ita_lic, code, links, text-mention and pre. http://google.com'
test_message = Message(message_id=1,
from_user=None,
date=None,
@ -173,8 +175,9 @@ class TestMessage(object):
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>. '
'http://google.com')
'<a href="http://github.com/">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and '
'<pre>pre</pre>. http://google.com')
text_html = self.test_message.text_html
assert text_html == test_html_string
@ -185,14 +188,16 @@ class TestMessage(object):
def test_text_html_urled(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>. '
'<a href="http://google.com">http://google.com</a>')
'<a href="http://github.com/">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and '
'<pre>pre</pre>. <a href="http://google.com">http://google.com</a>')
text_html = self.test_message.text_html_urled
assert text_html == test_html_string
def test_text_markdown_simple(self):
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and '
'```pre```. http://google.com')
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/), '
'[text-mention](tg://user?id=123456789) and ```pre```. '
'http://google.com')
text_markdown = self.test_message.text_markdown
assert text_markdown == test_md_string
@ -202,8 +207,9 @@ class TestMessage(object):
assert message.text_markdown is None
def test_text_markdown_urled(self):
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and '
'```pre```. [http://google.com](http://google.com)')
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/), '
'[text-mention](tg://user?id=123456789) and ```pre```. '
'[http://google.com](http://google.com)')
text_markdown = self.test_message.text_markdown_urled
assert text_markdown == test_md_string
@ -225,8 +231,9 @@ class TestMessage(object):
def test_caption_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>. '
'http://google.com')
'<a href="http://github.com/">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and '
'<pre>pre</pre>. http://google.com')
caption_html = self.test_message.caption_html
assert caption_html == test_html_string
@ -237,14 +244,16 @@ class TestMessage(object):
def test_caption_html_urled(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>. '
'<a href="http://google.com">http://google.com</a>')
'<a href="http://github.com/">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and '
'<pre>pre</pre>. <a href="http://google.com">http://google.com</a>')
caption_html = self.test_message.caption_html_urled
assert caption_html == test_html_string
def test_caption_markdown_simple(self):
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and '
'```pre```. http://google.com')
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/), '
'[text-mention](tg://user?id=123456789) and ```pre```. '
'http://google.com')
caption_markdown = self.test_message.caption_markdown
assert caption_markdown == test_md_string
@ -254,8 +263,9 @@ class TestMessage(object):
assert message.caption_markdown is None
def test_caption_markdown_urled(self):
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and '
'```pre```. [http://google.com](http://google.com)')
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/), '
'[text-mention](tg://user?id=123456789) and ```pre```. '
'[http://google.com](http://google.com)')
caption_markdown = self.test_message.caption_markdown_urled
assert caption_markdown == test_md_string
@ -326,8 +336,9 @@ class TestMessage(object):
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')
test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/), '
'[text-mention](tg://user?id=123456789) and ```pre```. '
'http://google.com')
def test(*args, **kwargs):
cid = args[1] == message.chat_id
@ -351,8 +362,9 @@ class TestMessage(object):
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')
'<a href="http://github.com/">links</a>, '
'<a href="tg://user?id=123456789">text-mention</a> and '
'<pre>pre</pre>. http://google.com')
def test(*args, **kwargs):
cid = args[1] == message.chat_id

View file

@ -23,9 +23,10 @@ from telegram import ParseMode
class TestParseMode(object):
markdown_text = '*bold* _italic_ [link](http://google.com).'
html_text = '<b>bold</b> <i>italic</i> <a href="http://google.com">link</a>.'
formatted_text_formatted = u'bold italic link.'
markdown_text = '*bold* _italic_ [link](http://google.com) [name](tg://user?id=123456789).'
html_text = ('<b>bold</b> <i>italic</i> <a href="http://google.com">link</a> '
'<a href="tg://user?id=123456789">name</a>.')
formatted_text_formatted = u'bold italic link name.'
@flaky(3, 1)
@pytest.mark.timeout(10)