From be5f0c95298fdf6b19c51f301a1adf856ee5743e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 26 Jun 2019 16:06:50 +0200 Subject: [PATCH] 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 --- pyrogram/client/{style => parser}/html.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) rename pyrogram/client/{style => parser}/html.py (95%) diff --git a/pyrogram/client/style/html.py b/pyrogram/client/parser/html.py similarity index 95% rename from pyrogram/client/style/html.py rename to pyrogram/client/parser/html.py index 5617cb54..16c5922a 100644 --- a/pyrogram/client/style/html.py +++ b/pyrogram/client/parser/html.py @@ -20,6 +20,7 @@ import html import re from collections import OrderedDict from html.parser import HTMLParser +from typing import Union import pyrogram from pyrogram.api import types @@ -103,7 +104,7 @@ class Parser(HTMLParser): class HTML: - def __init__(self, client: "pyrogram.BaseClient" = None): + def __init__(self, client: Union["pyrogram.BaseClient", None]): self.client = client def parse(self, text: str): @@ -126,7 +127,8 @@ class HTML: for entity in parser.entities: if isinstance(entity, types.InputMessageEntityMentionName): 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: continue @@ -135,7 +137,7 @@ class HTML: # TODO: OrderedDict to be removed in Python 3.6 return OrderedDict([ ("message", utils.remove_surrogates(parser.text)), - ("entities", entities) + ("entities", sorted(entities, key=lambda e: e.offset)) ]) @staticmethod