Merge pull request #316 from aiogram/dev-3.x-fix-text-decorations-2

Fix phone_number and bot_command entity types generation
This commit is contained in:
Alex Root Junior 2020-05-02 02:55:59 +03:00 committed by GitHub
commit 2553f5f19e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 13 deletions

View file

@ -26,7 +26,10 @@ class TextDecoration(ABC):
:param text: :param text:
:return: :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)) return cast(str, getattr(self, entity.type)(value=text))
if entity.type == "pre": if entity.type == "pre":
return ( return (
@ -34,17 +37,14 @@ class TextDecoration(ABC):
if entity.language if entity.language
else self.pre(value=text) else self.pre(value=text)
) )
elif entity.type == "text_mention": if entity.type == "text_mention":
from aiogram.api.types import User from aiogram.api.types import User
user = cast(User, entity.user) user = cast(User, entity.user)
return self.link(value=text, link=f"tg://user?id={user.id}") return self.link(value=text, link=f"tg://user?id={user.id}")
elif entity.type == "mention": if entity.type == "text_link":
return text
elif entity.type == "text_link":
return self.link(value=text, link=cast(str, entity.url)) return self.link(value=text, link=cast(str, entity.url))
elif entity.type == "url":
return text
return self.quote(text) return self.quote(text)
def unparse(self, text: str, entities: Optional[List[MessageEntity]] = None) -> str: def unparse(self, text: str, entities: Optional[List[MessageEntity]] = None) -> str:

View file

@ -184,15 +184,29 @@ class TestTextDecoration:
html_decoration, html_decoration,
"@username", "@username",
[ [
MessageEntity( MessageEntity(type="mention", offset=0, length=9),
type="mention", offset=0, length=9, url=None, user=None, language=None MessageEntity(type="bold", offset=0, length=9),
),
MessageEntity(
type="bold", offset=0, length=9, url=None, user=None, language=None
),
], ],
"<b>@username</b>", "<b>@username</b>",
], ],
[
html_decoration,
"/command",
[
MessageEntity(type="bot_command", offset=0, length=8),
MessageEntity(type="bold", offset=0, length=8),
],
"<b>/command</b>",
],
[
html_decoration,
"+1-212-555-0123",
[
MessageEntity(type="phone_number", offset=0, length=15),
MessageEntity(type="bold", offset=0, length=15),
],
"<b>+1-212-555-0123</b>",
],
[ [
html_decoration, html_decoration,
"test te👍🏿st test", "test te👍🏿st test",