From 2380e14dd9c96ca17a4ecb10adf02dcbf2d39898 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sat, 2 May 2020 02:43:19 +0300 Subject: [PATCH] Fix phone_number and bot_command entity types generation --- aiogram/utils/text_decorations.py | 14 ++++++------ tests/test_utils/test_text_decorations.py | 26 +++++++++++++++++------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/aiogram/utils/text_decorations.py b/aiogram/utils/text_decorations.py index 4e01bc8b..ac5262c1 100644 --- a/aiogram/utils/text_decorations.py +++ b/aiogram/utils/text_decorations.py @@ -26,7 +26,10 @@ class TextDecoration(ABC): :param text: :return: """ - if entity.type in ("bold", "italic", "code", "underline", "strikethrough"): + if entity.type in {"bot_command", "url", "mention", "phone_number"}: + # This entities should not be changed + return text + if entity.type in {"bold", "italic", "code", "underline", "strikethrough"}: return cast(str, getattr(self, entity.type)(value=text)) if entity.type == "pre": return ( @@ -34,17 +37,14 @@ class TextDecoration(ABC): if entity.language else self.pre(value=text) ) - elif entity.type == "text_mention": + if entity.type == "text_mention": from aiogram.api.types import User user = cast(User, entity.user) return self.link(value=text, link=f"tg://user?id={user.id}") - elif entity.type == "mention": - return text - elif entity.type == "text_link": + if entity.type == "text_link": return self.link(value=text, link=cast(str, entity.url)) - elif entity.type == "url": - return text + return self.quote(text) def unparse(self, text: str, entities: Optional[List[MessageEntity]] = None) -> str: diff --git a/tests/test_utils/test_text_decorations.py b/tests/test_utils/test_text_decorations.py index 5501c08f..790d327c 100644 --- a/tests/test_utils/test_text_decorations.py +++ b/tests/test_utils/test_text_decorations.py @@ -184,15 +184,29 @@ class TestTextDecoration: html_decoration, "@username", [ - MessageEntity( - type="mention", offset=0, length=9, url=None, user=None, language=None - ), - MessageEntity( - type="bold", offset=0, length=9, url=None, user=None, language=None - ), + MessageEntity(type="mention", offset=0, length=9), + MessageEntity(type="bold", offset=0, length=9), ], "@username", ], + [ + html_decoration, + "/command", + [ + MessageEntity(type="bot_command", offset=0, length=8), + MessageEntity(type="bold", offset=0, length=8), + ], + "/command", + ], + [ + html_decoration, + "+1-212-555-0123", + [ + MessageEntity(type="phone_number", offset=0, length=15), + MessageEntity(type="bold", offset=0, length=15), + ], + "+1-212-555-0123", + ], [ html_decoration, "test te👍🏿st test",