Add process controls and restart command
This commit is contained in:
54
app/pyrogram_compat.py
Normal file
54
app/pyrogram_compat.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""Small compatibility fixes for Kurigram versions with incomplete rich-text parsing."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _plain_text(value: Any) -> str:
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
if isinstance(value, (list, tuple)):
|
||||
return "".join(_plain_text(item) for item in value)
|
||||
nested = getattr(value, "text", None)
|
||||
if nested is not None and nested is not value:
|
||||
return _plain_text(nested)
|
||||
return str(value)
|
||||
|
||||
|
||||
def install_kurigram_compat() -> None:
|
||||
"""Patch nested hashtag-like entities in Kurigram 2.2.x.
|
||||
|
||||
Kurigram's parser assumes TextMention/TextHashtag/TextCashtag/TextBotCommand
|
||||
contain TextPlain. Telegram can legally nest formatting there, so the
|
||||
parsed RichText object is passed to ``lstrip`` and message parsing crashes.
|
||||
"""
|
||||
from pyrogram import raw, types
|
||||
from pyrogram.types.messages_and_media.rich_text import RichText
|
||||
|
||||
if getattr(RichText, "_nested_entity_compat", False):
|
||||
return
|
||||
|
||||
original = RichText._parse
|
||||
|
||||
async def patched(client: Any, rich_text: Any, users: dict = {}, chats: dict = {}):
|
||||
try:
|
||||
return await original(client, rich_text, users, chats)
|
||||
except AttributeError as exc:
|
||||
if "lstrip" not in str(exc):
|
||||
raise
|
||||
|
||||
handlers = (
|
||||
(raw.types.TextMention, types.RichTextMention, "@", "username"),
|
||||
(raw.types.TextHashtag, types.RichTextHashtag, "#", "hashtag"),
|
||||
(raw.types.TextCashtag, types.RichTextCashtag, "$", "cashtag"),
|
||||
(raw.types.TextBotCommand, types.RichTextBotCommand, "/", "bot_command"),
|
||||
)
|
||||
for raw_type, rich_type, prefix, field in handlers:
|
||||
if isinstance(rich_text, raw_type):
|
||||
content = await patched(client, rich_text.text, users, chats)
|
||||
return rich_type(text=content, **{field: _plain_text(content).lstrip(prefix)})
|
||||
raise
|
||||
|
||||
RichText._parse = staticmethod(patched)
|
||||
RichText._nested_entity_compat = True
|
||||
Reference in New Issue
Block a user