Add support for "spoiler" MessageEntity

This commit is contained in:
Dan 2021-12-30 09:14:49 +01:00
parent 42c690757d
commit f7b9137a68
4 changed files with 20 additions and 2 deletions

View File

@ -34,6 +34,7 @@ list of the basic styles currently supported by Pyrogram.
- *italic* - *italic*
- :strike:`strike` - :strike:`strike`
- :underline:`underline` - :underline:`underline`
- spoiler
- `text URL <https://pyrogram.org>`_ - `text URL <https://pyrogram.org>`_
- `user text mention <https://t.me/haskell>`_ - `user text mention <https://t.me/haskell>`_
- ``inline fixed-width code`` - ``inline fixed-width code``
@ -63,6 +64,8 @@ To strictly use this mode, pass "markdown" to the *parse_mode* parameter when us
~~strike~~ ~~strike~~
##spoiler##
[text URL](https://docs.pyrogram.org/) [text URL](https://docs.pyrogram.org/)
[text user mention](tg://user?id=23122162) [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
"__italic__, " "__italic__, "
"--underline--, " "--underline--, "
"~~strike~~, " "~~strike~~, "
"##spoiler##, "
"[mention](tg://user?id=23122162), " "[mention](tg://user?id=23122162), "
"[URL](https://pyrogram.org), " "[URL](https://pyrogram.org), "
"`code`, " "`code`, "
@ -113,6 +117,8 @@ The following tags are currently supported:
<s>strike</s>, <del>strike</del>, <strike>strike</strike> <s>strike</s>, <del>strike</del>, <strike>strike</strike>
<spoiler>spoiler</spoiler>
<a href="http://docs.pyrogram.org/">text URL</a> <a href="http://docs.pyrogram.org/">text URL</a>
<a href="tg://user?id=23122162">inline mention</a> <a href="tg://user?id=23122162">inline mention</a>
@ -136,6 +142,7 @@ The following tags are currently supported:
"<i>italic</i>, " "<i>italic</i>, "
"<u>underline</u>, " "<u>underline</u>, "
"<s>strike</s>, " "<s>strike</s>, "
"<spoiler>spoiler</spoiler>, "
"<a href=\"tg://user?id=23122162\">mention</a>, " "<a href=\"tg://user?id=23122162\">mention</a>, "
"<a href=\"https://pyrogram.org/\">URL</a>, " "<a href=\"https://pyrogram.org/\">URL</a>, "
"<code>code</code>\n\n" "<code>code</code>\n\n"

View File

@ -61,6 +61,8 @@ class Parser(HTMLParser):
elif tag == "pre": elif tag == "pre":
entity = raw.types.MessageEntityPre entity = raw.types.MessageEntityPre
extra["language"] = "" extra["language"] = ""
elif tag == "spoiler":
entity = raw.types.MessageEntitySpoiler
elif tag == "a": elif tag == "a":
url = attrs.get("href", "") url = attrs.get("href", "")
@ -153,7 +155,7 @@ class HTML:
start = entity.offset start = entity.offset
end = start + entity.length end = start + entity.length
if entity_type in ("bold", "italic", "underline", "strikethrough"): if entity_type in ("bold", "italic", "underline", "strikethrough", "spoiler"):
start_tag = f"<{entity_type[0]}>" start_tag = f"<{entity_type[0]}>"
end_tag = f"</{entity_type[0]}>" end_tag = f"</{entity_type[0]}>"
elif entity_type in ("code", "pre", "blockquote"): elif entity_type in ("code", "pre", "blockquote"):

View File

@ -28,6 +28,7 @@ BOLD_DELIM = "**"
ITALIC_DELIM = "__" ITALIC_DELIM = "__"
UNDERLINE_DELIM = "--" UNDERLINE_DELIM = "--"
STRIKE_DELIM = "~~" STRIKE_DELIM = "~~"
SPOILER_DELIM = "##"
CODE_DELIM = "`" CODE_DELIM = "`"
PRE_DELIM = "```" PRE_DELIM = "```"
@ -41,7 +42,8 @@ MARKDOWN_RE = re.compile(r"({d})|\[(.+?)\]\((.+?)\)".format(
STRIKE_DELIM, STRIKE_DELIM,
UNDERLINE_DELIM, UNDERLINE_DELIM,
ITALIC_DELIM, ITALIC_DELIM,
BOLD_DELIM BOLD_DELIM,
SPOILER_DELIM
] ]
]] ]]
))) )))
@ -90,6 +92,8 @@ class Markdown:
tag = "code" tag = "code"
elif delim == PRE_DELIM: elif delim == PRE_DELIM:
tag = "pre" tag = "pre"
elif delim == SPOILER_DELIM:
tag = "spoiler"
else: else:
continue continue
@ -127,6 +131,8 @@ class Markdown:
start_tag = end_tag = CODE_DELIM start_tag = end_tag = CODE_DELIM
elif entity_type in ("pre", "blockquote"): elif entity_type in ("pre", "blockquote"):
start_tag = end_tag = PRE_DELIM start_tag = end_tag = PRE_DELIM
elif entity_type == "spoiler":
start_tag = end_tag = SPOILER_DELIM
elif entity_type == "text_link": elif entity_type == "text_link":
url = entity.url url = entity.url
start_tag = "[" start_tag = "["

View File

@ -42,6 +42,7 @@ class MessageEntityType(AutoName):
ITALIC = auto() ITALIC = auto()
UNDERLINE = auto() UNDERLINE = auto()
STRIKETHROUGH = auto() STRIKETHROUGH = auto()
SPOILER = auto()
CODE = auto() CODE = auto()
PRE = auto() PRE = auto()
TEXT_LINK = auto() TEXT_LINK = auto()
@ -62,6 +63,7 @@ RAW_ENTITIES_TO_TYPE = {
raw.types.MessageEntityPre: MessageEntityType.PRE, raw.types.MessageEntityPre: MessageEntityType.PRE,
raw.types.MessageEntityUnderline: MessageEntityType.UNDERLINE, raw.types.MessageEntityUnderline: MessageEntityType.UNDERLINE,
raw.types.MessageEntityStrike: MessageEntityType.STRIKETHROUGH, raw.types.MessageEntityStrike: MessageEntityType.STRIKETHROUGH,
raw.types.MessageEntitySpoiler: MessageEntityType.SPOILER,
raw.types.MessageEntityBlockquote: MessageEntityType.BLOCKQUOTE, raw.types.MessageEntityBlockquote: MessageEntityType.BLOCKQUOTE,
raw.types.MessageEntityTextUrl: MessageEntityType.TEXT_LINK, raw.types.MessageEntityTextUrl: MessageEntityType.TEXT_LINK,
raw.types.MessageEntityMentionName: MessageEntityType.TEXT_MENTION, raw.types.MessageEntityMentionName: MessageEntityType.TEXT_MENTION,
@ -90,6 +92,7 @@ class MessageEntity(Object):
- "italic": *italic text*. - "italic": *italic text*.
- "underline": underlined text. - "underline": underlined text.
- "strikethrough": strikethrough text. - "strikethrough": strikethrough text.
- "spoiler": spoiler text.
- "code": monowidth string. - "code": monowidth string.
- "pre": monowidth block (see *language* below). - "pre": monowidth block (see *language* below).
- "text_link": for clickable text URLs. - "text_link": for clickable text URLs.