Repository: python-telegram-bot/python-telegram-bot

Issue:
message.text_* returns link as hyperlink.
after latest updates the library using "message.text_html" method (i didn't check with markdown) returns links as text_link.

so a message like "`test www.google.com`" is returned by message.text_html as "`test <a href="www.google.com">www.google.com</a>`" despite it was not an text_link.

before of the latest udpates it worked fine and it was able to reflect how the sent message was formatted. so if the message didn't contain html tags it didn't return html tags and viceversa.

actually it's not possible to distinguish if the user sent a link or text_link using that attribute and also if a bot wants to echoes messages respecting the formatting style a possible limit is when an user sends 101 link and the bot will try to echoes with 101 text_link (while the maximum formatting things in a message is 100).

**to reproduce:**
print a link sent to the bot using "update.message.text_html" example: "`t.me/telegram`"
**expcted behaviour:**
you get "`t.me/telegram`"
**actual behaviour:**
you get `<a href="t.me/telegram">t.me/telegram</a>`



Discussion:

Resolving patch:
diff --git a/telegram/message.py b/telegram/message.py
--- a/telegram/message.py
+++ b/telegram/message.py
@@ -697,23 +697,13 @@ def parse_entities(self, types=None):
             for entity in self.entities if entity.type in types
         }
 
-    @property
-    def text_html(self):
-        """
-        Creates an HTML-formatted string from the markup entities found in the message.
-
-        Use this if you want to retrieve the message text with the entities formatted as HTML.
-
-        Returns:
-            :obj:`str`: Message text with entities formatted as HTML.
-        """
-
+    def _text_html(self, urled=False):
         entities = self.parse_entities()
         message_text = self.text
         if not sys.maxunicode == 0xffff:
             message_text = message_text.encode('utf-16-le')
 
-        markdown_text = ''
+        html_text = ''
         last_offset = 0
 
         for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)):
@@ -721,7 +711,7 @@ def text_html(self):
 
             if entity.type == MessageEntity.TEXT_LINK:
                 insert = '<a href="{}">{}</a>'.format(entity.url, text)
-            elif entity.type == MessageEntity.URL:
+            elif (entity.type == MessageEntity.URL) and urled:
                 insert = '<a href="{0}">{0}</a>'.format(text)
             elif entity.type == MessageEntity.BOLD:
                 insert = '<b>' + text + '</b>'
@@ -735,30 +725,46 @@ def text_html(self):
                 insert = text
 
             if sys.maxunicode == 0xffff:
-                markdown_text += escape_html(message_text[last_offset:entity.offset]) + insert
+                html_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
+                html_text += escape_html(message_text[last_offset * 2:entity.offset * 2]
+                                         .decode('utf-16-le')) + insert
 
             last_offset = entity.offset + entity.length
 
         if sys.maxunicode == 0xffff:
-            markdown_text += escape_html(message_text[last_offset:])
+            html_text += escape_html(message_text[last_offset:])
         else:
-            markdown_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le'))
-        return markdown_text
+            html_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le'))
+        return html_text
 
     @property
-    def text_markdown(self):
+    def text_html(self):
         """
-        Creates an Markdown-formatted string from the markup entities found in the message.
+        Creates an HTML-formatted string from the markup entities found in the message.
 
-        Use this if you want to retrieve the message text with the entities formatted as Markdown.
+        Use this if you want to retrieve the message text with the entities formatted as HTML in
+        the same way the original message was formatted.
 
         Returns:
-            :obj:`str`: Message text with entities formatted as Markdown.
+            :obj:`str`: Message text with entities formatted as HTML.
+        """
+        return self._text_html(urled=False)
+
+    @property
+    def text_html_urled(self):
+        """
+        Creates an HTML-formatted string from the markup entities found in the message.
+
+        Use this if you want to retrieve the message text with the entities formatted as HTML.
+        This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink.
+
+        Returns:
+            :obj:`str`: Message text with entities formatted as HTML.
         """
+        return self._text_html(urled=True)
 
+    def _text_markdown(self, urled=False):
         entities = self.parse_entities()
         message_text = self.text
         if not sys.maxunicode == 0xffff:
@@ -772,7 +778,7 @@ def text_markdown(self):
 
             if entity.type == MessageEntity.TEXT_LINK:
                 insert = '[{}]({})'.format(text, entity.url)
-            elif entity.type == MessageEntity.URL:
+            elif (entity.type == MessageEntity.URL) and urled:
                 insert = '[{0}]({0})'.format(text)
             elif entity.type == MessageEntity.BOLD:
                 insert = '*' + text + '*'
@@ -798,6 +804,34 @@ def text_markdown(self):
             markdown_text += escape_markdown(message_text[last_offset * 2:].decode('utf-16-le'))
         return markdown_text
 
+    @property
+    def text_markdown(self):
+        """
+        Creates an Markdown-formatted string from the markup entities found in the message.
+
+        Use this if you want to retrieve the message text with the entities formatted as Markdown
+        in the same way the original message was formatted.
+
+        Returns:
+            :obj:`str`: Message text with entities formatted as Markdown.
+        """
+
+        return self._text_markdown(urled=False)
+
+    @property
+    def text_markdown_urled(self):
+        """
+        Creates an Markdown-formatted string from the markup entities found in the message.
+
+        Use this if you want to retrieve the message text with the entities formatted as Markdown.
+        This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink.
+
+        Returns:
+            :obj:`str`: Message text with entities formatted as Markdown.
+        """
+
+        return self._text_markdown(urled=True)
+
     @property
     def new_chat_member(self):
         """Deprecated"""


Test patch: