2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Parser's client can be None

In that case, check if is None and don't parse user mentions.
This happens only in text content for inline results
This commit is contained in:
Dan 2019-06-26 16:06:50 +02:00
parent 8d852cb47e
commit be5f0c9529

View File

@ -20,6 +20,7 @@ import html
import re import re
from collections import OrderedDict from collections import OrderedDict
from html.parser import HTMLParser from html.parser import HTMLParser
from typing import Union
import pyrogram import pyrogram
from pyrogram.api import types from pyrogram.api import types
@ -103,7 +104,7 @@ class Parser(HTMLParser):
class HTML: class HTML:
def __init__(self, client: "pyrogram.BaseClient" = None): def __init__(self, client: Union["pyrogram.BaseClient", None]):
self.client = client self.client = client
def parse(self, text: str): def parse(self, text: str):
@ -126,7 +127,8 @@ class HTML:
for entity in parser.entities: for entity in parser.entities:
if isinstance(entity, types.InputMessageEntityMentionName): if isinstance(entity, types.InputMessageEntityMentionName):
try: try:
entity.user_id = self.client.resolve_peer(entity.user_id) if self.client is not None:
entity.user_id = self.client.resolve_peer(entity.user_id)
except PeerIdInvalid: except PeerIdInvalid:
continue continue
@ -135,7 +137,7 @@ class HTML:
# TODO: OrderedDict to be removed in Python 3.6 # TODO: OrderedDict to be removed in Python 3.6
return OrderedDict([ return OrderedDict([
("message", utils.remove_surrogates(parser.text)), ("message", utils.remove_surrogates(parser.text)),
("entities", entities) ("entities", sorted(entities, key=lambda e: e.offset))
]) ])
@staticmethod @staticmethod