Skip to content

Commit 5adf7fc

Browse files
fix: parsing RichText in some cases
https://t.me/kurigram_chat/5/294423
1 parent 793ef24 commit 5adf7fc

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

pyrogram/types/messages_and_media/rich_text.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,31 +196,31 @@ async def _parse(
196196

197197
return RichTextMention(
198198
text=content,
199-
username=content.lstrip("@"),
199+
username=RichText._to_plain_text(content).lstrip("@"),
200200
)
201201

202202
if isinstance(rich_text, raw.types.TextHashtag):
203203
content = await RichText._parse(client, rich_text.text)
204204

205205
return RichTextHashtag(
206206
text=content,
207-
hashtag=content.lstrip("#"),
207+
hashtag=RichText._to_plain_text(content).lstrip("#"),
208208
)
209209

210210
if isinstance(rich_text, raw.types.TextCashtag):
211211
content = await RichText._parse(client, rich_text.text)
212212

213213
return RichTextCashtag(
214214
text=content,
215-
cashtag=content.lstrip("$"),
215+
cashtag=RichText._to_plain_text(content).lstrip("$"),
216216
)
217217

218218
if isinstance(rich_text, raw.types.TextBotCommand):
219219
content = await RichText._parse(client, rich_text.text)
220220

221221
return RichTextBotCommand(
222222
text=content,
223-
bot_command=content.lstrip("/"),
223+
bot_command=RichText._to_plain_text(content).lstrip("/"),
224224
)
225225

226226
if isinstance(rich_text, raw.types.TextAnchor):
@@ -235,6 +235,27 @@ async def _parse(
235235

236236
# TODO: if isinstance(rich_text, raw.types.TextImage):
237237

238+
@staticmethod
239+
def _to_plain_text(text: "RichText") -> str:
240+
if isinstance(text, str):
241+
return text
242+
243+
if isinstance(text, (list, types.List)):
244+
return "".join(RichText._to_plain_text(t) for t in text)
245+
246+
if hasattr(text, "text"):
247+
return RichText._to_plain_text(text.text)
248+
249+
# Math expression
250+
if hasattr(text, "expression"):
251+
return RichText._to_plain_text(text.expression)
252+
253+
# Custom emoji
254+
if hasattr(text, "alternative_text"):
255+
return RichText._to_plain_text(text.alternative_text)
256+
257+
return ""
258+
238259

239260
class RichTextBold(RichText):
240261
"""A bold text.

0 commit comments

Comments
 (0)