Skip to content

Commit ff7273b

Browse files
author
Pietro Albini
committed
Rename and improve some of the types ParsedTextEntity
Some of the default ones made by Telegram were ugly, especially the distinct `url` and `text_link`. This commit adds a layer between the user and the raw types so it's possible to rename them.
1 parent ba9126a commit ff7273b

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed

botogram/objects/messages.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,23 @@ class ParsedTextEntity(BaseObject):
4343
}
4444
replace_keys = {
4545
"url": "_url", # Dynamically implemented
46+
"type": "_type", # Dynamically implemented
4647

4748
# Private attributes, use the ``text`` one
4849
"offset": "_offset",
4950
"length": "_length",
5051
}
5152

53+
# Bring some sanity to the Bot API
54+
replace_types = {
55+
"bot_command": "command",
56+
"text_link": "link",
57+
"url": "link",
58+
}
59+
replace_types_inverse = {
60+
"command": "bot_command",
61+
"link": "text_link",
62+
}
5263

5364
def __init__(self, data, api=None, message=None):
5465
super().__init__(data, api)
@@ -75,6 +86,32 @@ def set_message(self, message):
7586
"""Set the message instance related to this object"""
7687
self._message = message
7788

89+
@property
90+
def type(self):
91+
"""Get the type of the entity"""
92+
# Bring some sanity to the Bot API
93+
if self._type in self.replace_types:
94+
return self.replace_types[self._type]
95+
return self._type
96+
97+
@type.setter
98+
def type(self, value):
99+
"""Set the type of the entity"""
100+
# Special check for link, because two original types points to it
101+
if value == "link":
102+
# If the URL is not set or it's the same as the text, then it's a
103+
# normal URL, else it has a label
104+
if self.text == self._url or self._url is None:
105+
self._type = "url"
106+
else:
107+
self._type = "text_link"
108+
return
109+
110+
if value in self.replace_types_inverse:
111+
self._type = self.replace_types_inverse[value]
112+
else:
113+
self._type = value
114+
78115
@property
79116
@_require_message
80117
def text(self):
@@ -98,7 +135,7 @@ def url(self):
98135
if self._url is not None:
99136
return self._url
100137

101-
if self.type == "url":
138+
if self.type == "link":
102139
# Standard URLs
103140
return self.text
104141
elif self.type == "mention":

docs/api/telegram.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,9 @@ about its business.
663663

664664
* **hashtag**: an hashtag (for example ``#pythonftw``)
665665

666-
* **bot_command**: a command sent to a bot (for example ``/help``)
666+
* **command**: a command sent to a bot (for example ``/help``)
667667

668-
* **url**: a textual URL (for example ``https://pietroalbini.io``)
668+
* **url**: a link (the text can contain its label)
669669

670670
* **email**: an email address (for example ``[email protected]``)
671671

@@ -677,8 +677,6 @@ about its business.
677677

678678
* **pre**: a monospace-formatted block
679679

680-
* **text_link**: a link with a label
681-
682680
.. py:attribute:: text
683681
684682
Return the plaintext content of the entity. In pair with the type you can

tests/test_objects_messages.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_parsed_text_entity():
4040
"offset": 12,
4141
"length": 14,
4242
})
43-
assert entity.type == "url"
43+
assert entity.type == "link"
4444
# Those attributes requires a Message instance
4545
with pytest.raises(RuntimeError):
4646
entity.url
@@ -53,6 +53,81 @@ def test_parsed_text_entity():
5353
assert entity.text == "http://url.com"
5454

5555

56+
def test_parsed_text_entity_type():
57+
# Basic entity
58+
entity = botogram.objects.messages.ParsedTextEntity({
59+
"type": "",
60+
"offset": 0,
61+
"length": 23,
62+
})
63+
64+
#################################
65+
# A type which doesn't mutate #
66+
#################################
67+
68+
msg = get_dummy_message("#aaaaaaaaaaaaaaaaaaaaaaa")
69+
entity.set_message(msg)
70+
71+
# Getter
72+
entity._type = "hashtag"
73+
assert entity.type == "hashtag"
74+
75+
# Setter
76+
entity.type = "hashtag"
77+
assert entity._type == "hashtag"
78+
79+
#########################
80+
# A type which mutate #
81+
#########################
82+
83+
msg = get_dummy_message("/aaaaaaaaaaaaaaaaaaaaaaa")
84+
entity.set_message(msg)
85+
86+
# Getter
87+
entity._type = "bot_command"
88+
assert entity.type == "command"
89+
90+
# Setter
91+
entity.type = "command"
92+
assert entity._type == "bot_command"
93+
94+
#####################################
95+
# A link (which mutates as "url") #
96+
#####################################
97+
98+
msg = get_dummy_message("https://www.example.com")
99+
entity.set_message(msg)
100+
101+
# Getter
102+
entity._type = "url"
103+
assert entity.type == "link"
104+
105+
# Setter without URL explicitly defined
106+
entity.type = "link"
107+
assert entity._type == "url"
108+
109+
# Setter with URL explicitly defined
110+
entity._url = "https://www.example.com"
111+
entity.type = "link"
112+
assert entity._type == "url"
113+
114+
###########################################
115+
# A link (which mutates as "text_link") #
116+
###########################################
117+
118+
msg = get_dummy_message("This is a labelled link")
119+
entity._url = "https://www.example.com"
120+
entity.set_message(msg)
121+
122+
# Getter
123+
entity._type = "text_link"
124+
assert entity.type == "link"
125+
126+
# Setter
127+
entity.type = "link"
128+
assert entity._type == "text_link"
129+
130+
56131
def test_parsed_text_entity_url():
57132
# Basic entity
58133
entity = botogram.objects.messages.ParsedTextEntity({

0 commit comments

Comments
 (0)