Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit f7b9137

Browse files
committed
Add support for "spoiler" MessageEntity
1 parent 42c6907 commit f7b9137

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

docs/source/topics/text-formatting.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ list of the basic styles currently supported by Pyrogram.
3434
- *italic*
3535
- :strike:`strike`
3636
- :underline:`underline`
37+
- spoiler
3738
- `text URL <https://pyrogram.org>`_
3839
- `user text mention <https://t.me/haskell>`_
3940
- ``inline fixed-width code``
@@ -63,6 +64,8 @@ To strictly use this mode, pass "markdown" to the *parse_mode* parameter when us
6364
6465
~~strike~~
6566
67+
##spoiler##
68+
6669
[text URL](https://docs.pyrogram.org/)
6770
6871
[text user mention](tg://user?id=23122162)
@@ -86,6 +89,7 @@ To strictly use this mode, pass "markdown" to the *parse_mode* parameter when us
8689
"__italic__, "
8790
"--underline--, "
8891
"~~strike~~, "
92+
"##spoiler##, "
8993
"[mention](tg://user?id=23122162), "
9094
"[URL](https://pyrogram.org), "
9195
"`code`, "
@@ -113,6 +117,8 @@ The following tags are currently supported:
113117
114118
<s>strike</s>, <del>strike</del>, <strike>strike</strike>
115119
120+
<spoiler>spoiler</spoiler>
121+
116122
<a href="http://docs.pyrogram.org/">text URL</a>
117123
118124
<a href="tg://user?id=23122162">inline mention</a>
@@ -136,6 +142,7 @@ The following tags are currently supported:
136142
"<i>italic</i>, "
137143
"<u>underline</u>, "
138144
"<s>strike</s>, "
145+
"<spoiler>spoiler</spoiler>, "
139146
"<a href=\"tg://user?id=23122162\">mention</a>, "
140147
"<a href=\"https://pyrogram.org/\">URL</a>, "
141148
"<code>code</code>\n\n"

pyrogram/parser/html.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def handle_starttag(self, tag, attrs):
6161
elif tag == "pre":
6262
entity = raw.types.MessageEntityPre
6363
extra["language"] = ""
64+
elif tag == "spoiler":
65+
entity = raw.types.MessageEntitySpoiler
6466
elif tag == "a":
6567
url = attrs.get("href", "")
6668

@@ -153,7 +155,7 @@ def unparse(text: str, entities: list):
153155
start = entity.offset
154156
end = start + entity.length
155157

156-
if entity_type in ("bold", "italic", "underline", "strikethrough"):
158+
if entity_type in ("bold", "italic", "underline", "strikethrough", "spoiler"):
157159
start_tag = f"<{entity_type[0]}>"
158160
end_tag = f"</{entity_type[0]}>"
159161
elif entity_type in ("code", "pre", "blockquote"):

pyrogram/parser/markdown.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
ITALIC_DELIM = "__"
2929
UNDERLINE_DELIM = "--"
3030
STRIKE_DELIM = "~~"
31+
SPOILER_DELIM = "##"
3132
CODE_DELIM = "`"
3233
PRE_DELIM = "```"
3334

@@ -41,7 +42,8 @@
4142
STRIKE_DELIM,
4243
UNDERLINE_DELIM,
4344
ITALIC_DELIM,
44-
BOLD_DELIM
45+
BOLD_DELIM,
46+
SPOILER_DELIM
4547
]
4648
]]
4749
)))
@@ -90,6 +92,8 @@ async def parse(self, text: str, strict: bool = False):
9092
tag = "code"
9193
elif delim == PRE_DELIM:
9294
tag = "pre"
95+
elif delim == SPOILER_DELIM:
96+
tag = "spoiler"
9397
else:
9498
continue
9599

@@ -127,6 +131,8 @@ def unparse(text: str, entities: list):
127131
start_tag = end_tag = CODE_DELIM
128132
elif entity_type in ("pre", "blockquote"):
129133
start_tag = end_tag = PRE_DELIM
134+
elif entity_type == "spoiler":
135+
start_tag = end_tag = SPOILER_DELIM
130136
elif entity_type == "text_link":
131137
url = entity.url
132138
start_tag = "["

pyrogram/types/messages_and_media/message_entity.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class MessageEntityType(AutoName):
4242
ITALIC = auto()
4343
UNDERLINE = auto()
4444
STRIKETHROUGH = auto()
45+
SPOILER = auto()
4546
CODE = auto()
4647
PRE = auto()
4748
TEXT_LINK = auto()
@@ -62,6 +63,7 @@ class MessageEntityType(AutoName):
6263
raw.types.MessageEntityPre: MessageEntityType.PRE,
6364
raw.types.MessageEntityUnderline: MessageEntityType.UNDERLINE,
6465
raw.types.MessageEntityStrike: MessageEntityType.STRIKETHROUGH,
66+
raw.types.MessageEntitySpoiler: MessageEntityType.SPOILER,
6567
raw.types.MessageEntityBlockquote: MessageEntityType.BLOCKQUOTE,
6668
raw.types.MessageEntityTextUrl: MessageEntityType.TEXT_LINK,
6769
raw.types.MessageEntityMentionName: MessageEntityType.TEXT_MENTION,
@@ -90,6 +92,7 @@ class MessageEntity(Object):
9092
- "italic": *italic text*.
9193
- "underline": underlined text.
9294
- "strikethrough": strikethrough text.
95+
- "spoiler": spoiler text.
9396
- "code": monowidth string.
9497
- "pre": monowidth block (see *language* below).
9598
- "text_link": for clickable text URLs.

0 commit comments

Comments
 (0)