Skip to content

Commit cd54c91

Browse files
author
Pietro Albini
committed
Be sure to return a valid URL in ParsedTextEntity.url
The URL received from Telegram in message entities is the raw one posted by the sender of the message, which might lack a protocol. This commit enforces a protocol to be present in ParsedTextEntity's URLs, defaulting to HTTP if none is provided.
1 parent 9daae04 commit cd54c91

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

botogram/objects/messages.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Released under the MIT license
77
"""
88

9+
import re
910

1011
from .base import BaseObject, _itself
1112
from . import mixins
@@ -16,6 +17,9 @@
1617
Location
1718

1819

20+
_url_protocol_re = re.compile(r"^https?:\/\/|s?ftp:\/\/|mailto:", re.I)
21+
22+
1923
def _require_message(func):
2024
"""Decorator which forces the object to have an attached message"""
2125
@utils.wraps(func)
@@ -142,13 +146,12 @@ def text(self):
142146
@_require_message
143147
def url(self):
144148
"""Get the URL attached to the message"""
145-
# Use the provided if available
146149
if self._url is not None:
147-
return self._url
148-
149-
if self.type == "link":
150+
# Use the provided if available
151+
url = self._url
152+
elif self.type == "link":
150153
# Standard URLs
151-
return self.text
154+
url = self.text
152155
elif self.type == "mention":
153156
# telegram.me URLs
154157
return "https://telegram.me/%s" % self.text[1:]
@@ -159,6 +162,13 @@ def url(self):
159162
# Sorry!
160163
return None
161164

165+
# Be sure to have a protocol in the URL (default to HTTP)
166+
# Apparently Telegram doesn't provide always a valid URL, but whatever
167+
# the user sends in the message
168+
if not _url_protocol_re.match(url):
169+
url = "http://%s" % url
170+
return url
171+
162172

163173
class ParsedText:
164174
"""Collection of ParsedTextEntity.

tests/test_objects_messages.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ def test_parsed_text_entity_url():
160160
entity._url = "https://www.example.com"
161161
entity.set_message(msg)
162162
assert entity.url == "https://www.example.com"
163+
entity._url = None
164+
165+
# Url entity without a scheme
166+
msg = get_dummy_message("www.example.com/abcdefg")
167+
entity.type = "url"
168+
entity.set_message(msg)
169+
assert entity.url == "http://www.example.com/abcdefg"
163170

164171

165172
def test_parsed_text():

0 commit comments

Comments
 (0)