Skip to content

Commit 1009c1c

Browse files
author
Pietro Albini
committed
Add support for text mentions in ParsedTextEntity
Support for text mentions was added in the Bot API 2.1 update, and this commit brings support in the ParsedTextEntity. Issue: GH-61
1 parent 43e1786 commit 1009c1c

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

botogram/objects/messages.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ParsedTextEntity(BaseObject):
4444
}
4545
optional = {
4646
"url": str,
47+
"user": User,
4748
}
4849
replace_keys = {
4950
"url": "_url", # Dynamically implemented
@@ -59,10 +60,12 @@ class ParsedTextEntity(BaseObject):
5960
"bot_command": "command",
6061
"text_link": "link",
6162
"url": "link",
63+
"text_mention": "mention",
6264
}
6365
replace_types_inverse = {
6466
"command": "bot_command",
6567
"link": "text_link",
68+
"mention": "text_mention",
6669
}
6770

6871
def __init__(self, data, api=None, message=None):
@@ -120,9 +123,14 @@ def type(self, value):
120123
self._type = "url"
121124
else:
122125
self._type = "text_link"
123-
return
124126

125-
if value in self.replace_types_inverse:
127+
elif value == "mention":
128+
if self.user is not None:
129+
self._type = "text_mention"
130+
else:
131+
self._type = "mention"
132+
133+
elif value in self.replace_types_inverse:
126134
self._type = self.replace_types_inverse[value]
127135
else:
128136
self._type = value
@@ -153,8 +161,16 @@ def url(self):
153161
# Standard URLs
154162
url = self.text
155163
elif self.type == "mention":
164+
# Detect the correct username
165+
if self.user is not None and self.user.username is not None:
166+
username = self.user.username
167+
elif self.text.startswith("@"):
168+
username = self.text[1:]
169+
else:
170+
return None
171+
156172
# telegram.me URLs
157-
return "https://telegram.me/%s" % self.text[1:]
173+
return "https://telegram.me/%s" % username
158174
elif self.type == "email":
159175
# mailto: URL
160176
return "mailto:%s" % self.text

docs/api/telegram.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,13 +906,14 @@ about its business.
906906

907907
* **plain**: a plain string (with no formatting or special meaning)
908908

909-
* **mention**: a mention to another user (for example ``@pietroalbini``)
909+
* **mention**: a mention to another user (can contain the username or the
910+
full name, for example ``@pietroalbini`` or ``Pietro``)
910911

911912
* **hashtag**: an hashtag (for example ``#pythonftw``)
912913

913914
* **command**: a command sent to a bot (for example ``/help``)
914915

915-
* **url**: a link (the text can contain its label)
916+
* **link** a link (the text can contain its label)
916917

917918
* **email**: an email address (for example ``[email protected]``)
918919

@@ -932,8 +933,14 @@ about its business.
932933
.. py:attribute:: url
933934
934935
The attached URL for the entity. This includes the raw URL for the
935-
**url** and **text_link** types, the ``telegram.me`` link for the
936-
**mention** type, and the ``mailto:`` link for **email** type.
936+
**url** type, the ``telegram.me`` link for the **mention** type (if the
937+
user has an username), and the ``mailto:`` link for **email** type.
938+
939+
.. py:attribute:: user
940+
941+
The :py:class:`~botogram.User` mentioned in this entity. This isn't
942+
always provided by Telegram, currently only if the mentioned user doesn't
943+
have an username.
937944

938945
.. py:class:: botogram.Message
939946

tests/test_objects_messages.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
import botogram.objects.messages
11+
import botogram.objects.chats
1112

1213

1314
def get_dummy_message(text):
@@ -127,6 +128,39 @@ def test_parsed_text_entity_type():
127128
entity.type = "link"
128129
assert entity._type == "text_link"
129130

131+
############################################
132+
# A mention (which mutates as "mention") #
133+
############################################
134+
135+
msg = get_dummy_message("@test_image")
136+
entity.set_message(msg)
137+
138+
# Getter
139+
entity._type = "mention"
140+
assert entity.type == "mention"
141+
142+
# Setter
143+
entity.type = "mention"
144+
assert entity._type == "mention"
145+
146+
#################################################
147+
# A mention (which mutates as "text_mention") #
148+
#################################################
149+
150+
msg = get_dummy_message("@test_image")
151+
entity.user = botogram.objects.chats.User({"id": 1, "first_name": "Test"})
152+
entity.set_message(msg)
153+
154+
# Getter
155+
entity._type = "text_mention"
156+
assert entity.type == "mention"
157+
158+
# Setter
159+
entity.type = "mention"
160+
assert entity._type == "text_mention"
161+
162+
entity.user = None
163+
130164

131165
def test_parsed_text_entity_url():
132166
# Basic entity
@@ -142,12 +176,25 @@ def test_parsed_text_entity_url():
142176
entity.set_message(msg)
143177
assert entity.url == "https://www.example.com"
144178

145-
# Mention entity
179+
# Mention entity with @usernames
146180
msg = get_dummy_message("@a_botogram_users_group")
147181
entity.type = "mention"
148182
entity.set_message(msg)
149183
assert entity.url == "https://telegram.me/a_botogram_users_group"
150184

185+
# Text mention entities without usernames
186+
msg = get_dummy_message("A botogram users group!")
187+
entity.user = botogram.objects.chats.User({"id": 1, "first_name": "Test"})
188+
entity.type = "mention"
189+
entity.set_message(msg)
190+
assert entity.url is None
191+
192+
# Text mention entities with usernames
193+
entity.user.username = "test_username"
194+
assert entity.url == "https://telegram.me/test_username"
195+
196+
entity.user = None
197+
151198
# Email entity
152199
msg = get_dummy_message("[email protected]")
153200
entity.type = "email"

0 commit comments

Comments
 (0)