Merge branch 'dev-3.x' into examples/dev-3x-refactor

This commit is contained in:
Alex Root Junior 2023-08-13 17:56:22 +03:00 committed by GitHub
commit a37b47f5d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 3 deletions

1
CHANGES/1259.bugfix.rst Normal file
View file

@ -0,0 +1 @@
Fixed nested hashtag, cashtag and email message entities not being parsed correctly when these entities are inside another entity.

1
CHANGES/5780.doc.rst Normal file
View file

@ -0,0 +1 @@
Refactored examples code to use aiogram enumerations and enhanced chat messages with markdown beautifications for a more user-friendly display.

View file

@ -43,8 +43,11 @@ class TextDecoration(ABC):
MessageEntityType.URL,
MessageEntityType.MENTION,
MessageEntityType.PHONE_NUMBER,
MessageEntityType.HASHTAG,
MessageEntityType.CASHTAG,
MessageEntityType.EMAIL,
}:
# This entities should not be changed
# These entities should not be changed
return text
if entity.type in {
MessageEntityType.BOLD,
@ -71,6 +74,8 @@ class TextDecoration(ABC):
if entity.type == MessageEntityType.CUSTOM_EMOJI:
return self.custom_emoji(value=text, custom_emoji_id=cast(str, entity.custom_emoji_id))
# This case is not possible because of `if` above, but if any new entity is added to
# API it will be here too
return self.quote(text)
def unparse(self, text: str, entities: Optional[List[MessageEntity]] = None) -> str:

View file

@ -72,8 +72,8 @@ deep_dark_router = Router()
async def my_chat_member_change(chat_member: ChatMemberUpdated, bot: Bot) -> None:
await bot.send_message(
chat_member.chat.id,
"This Bot`s status was changed from "
+ f"{hbold(chat_member.old_chat_member.status)} to {hbold(chat_member.new_chat_member.status)}",
f"This Bot`s status was changed from {hbold(chat_member.old_chat_member.status)} "
f"to {hbold(chat_member.new_chat_member.status)}",
)

View file

@ -113,6 +113,14 @@ class TestTextDecoration:
):
assert decorator.apply_entity(entity, "test") == result
def test_unknown_apply_entity(self):
assert (
html_decoration.apply_entity(
MessageEntity(type="unknown", offset=0, length=5), "<test>"
)
== "&lt;test&gt;"
)
@pytest.mark.parametrize(
"decorator,before,after",
[
@ -243,6 +251,33 @@ class TestTextDecoration:
[MessageEntity(type="bold", offset=0, length=8, url=None, user=None)],
"<b>👋🏾 Hi!</b>",
],
[
html_decoration,
"#test",
[
MessageEntity(type="hashtag", offset=0, length=5),
MessageEntity(type="bold", offset=0, length=5),
],
"<b>#test</b>",
],
[
html_decoration,
"$TEST",
[
MessageEntity(type="cashtag", offset=0, length=5),
MessageEntity(type="bold", offset=0, length=5),
],
"<b>$TEST</b>",
],
[
html_decoration,
"test@example.com",
[
MessageEntity(type="email", offset=0, length=16),
MessageEntity(type="bold", offset=0, length=16),
],
"<b>test@example.com</b>",
],
],
)
def test_unparse(