mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Fix broken mentions for both HTML and Markdown
This commit is contained in:
parent
f4cb31d7b8
commit
ef5dd96bdc
@ -81,8 +81,8 @@ class BaseClient:
|
|||||||
self.peers_by_username = {}
|
self.peers_by_username = {}
|
||||||
self.peers_by_phone = {}
|
self.peers_by_phone = {}
|
||||||
|
|
||||||
self.markdown = Markdown(self.peers_by_id)
|
self.markdown = Markdown(self)
|
||||||
self.html = HTML(self.peers_by_id)
|
self.html = HTML(self)
|
||||||
|
|
||||||
self.session = None
|
self.session = None
|
||||||
self.media_sessions = {}
|
self.media_sessions = {}
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
import re
|
import re
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
import pyrogram
|
||||||
from pyrogram.api.types import (
|
from pyrogram.api.types import (
|
||||||
MessageEntityBold as Bold,
|
MessageEntityBold as Bold,
|
||||||
MessageEntityItalic as Italic,
|
MessageEntityItalic as Italic,
|
||||||
@ -28,6 +29,7 @@ from pyrogram.api.types import (
|
|||||||
MessageEntityMentionName as MentionInvalid,
|
MessageEntityMentionName as MentionInvalid,
|
||||||
InputMessageEntityMentionName as Mention,
|
InputMessageEntityMentionName as Mention,
|
||||||
)
|
)
|
||||||
|
from pyrogram.errors import PeerIdInvalid
|
||||||
from . import utils
|
from . import utils
|
||||||
|
|
||||||
|
|
||||||
@ -35,8 +37,8 @@ class HTML:
|
|||||||
HTML_RE = re.compile(r"<(\w+)(?: href=([\"'])([^<]+)\2)?>([^>]+)</\1>")
|
HTML_RE = re.compile(r"<(\w+)(?: href=([\"'])([^<]+)\2)?>([^>]+)</\1>")
|
||||||
MENTION_RE = re.compile(r"tg://user\?id=(\d+)")
|
MENTION_RE = re.compile(r"tg://user\?id=(\d+)")
|
||||||
|
|
||||||
def __init__(self, peers_by_id: dict = None):
|
def __init__(self, client: "pyrogram.BaseClient" = None):
|
||||||
self.peers_by_id = peers_by_id or {}
|
self.client = client
|
||||||
|
|
||||||
def parse(self, message: str):
|
def parse(self, message: str):
|
||||||
entities = []
|
entities = []
|
||||||
@ -52,7 +54,11 @@ class HTML:
|
|||||||
|
|
||||||
if mention:
|
if mention:
|
||||||
user_id = int(mention.group(1))
|
user_id = int(mention.group(1))
|
||||||
input_user = self.peers_by_id.get(user_id, None)
|
|
||||||
|
try:
|
||||||
|
input_user = self.client.resolve_peer(user_id)
|
||||||
|
except PeerIdInvalid:
|
||||||
|
input_user = None
|
||||||
|
|
||||||
entity = (
|
entity = (
|
||||||
Mention(offset=start, length=len(body), user_id=input_user)
|
Mention(offset=start, length=len(body), user_id=input_user)
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
import re
|
import re
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
import pyrogram
|
||||||
from pyrogram.api.types import (
|
from pyrogram.api.types import (
|
||||||
MessageEntityBold as Bold,
|
MessageEntityBold as Bold,
|
||||||
MessageEntityItalic as Italic,
|
MessageEntityItalic as Italic,
|
||||||
@ -28,6 +29,7 @@ from pyrogram.api.types import (
|
|||||||
MessageEntityMentionName as MentionInvalid,
|
MessageEntityMentionName as MentionInvalid,
|
||||||
InputMessageEntityMentionName as Mention
|
InputMessageEntityMentionName as Mention
|
||||||
)
|
)
|
||||||
|
from pyrogram.errors import PeerIdInvalid
|
||||||
from . import utils
|
from . import utils
|
||||||
|
|
||||||
|
|
||||||
@ -52,8 +54,8 @@ class Markdown:
|
|||||||
))
|
))
|
||||||
MENTION_RE = re.compile(r"tg://user\?id=(\d+)")
|
MENTION_RE = re.compile(r"tg://user\?id=(\d+)")
|
||||||
|
|
||||||
def __init__(self, peers_by_id: dict = None):
|
def __init__(self, client: "pyrogram.BaseClient" = None):
|
||||||
self.peers_by_id = peers_by_id or {}
|
self.client = client
|
||||||
|
|
||||||
def parse(self, message: str):
|
def parse(self, message: str):
|
||||||
message = utils.add_surrogates(str(message or "")).strip()
|
message = utils.add_surrogates(str(message or "")).strip()
|
||||||
@ -69,12 +71,15 @@ class Markdown:
|
|||||||
|
|
||||||
if mention:
|
if mention:
|
||||||
user_id = int(mention.group(1))
|
user_id = int(mention.group(1))
|
||||||
input_user = self.peers_by_id.get(user_id, None)
|
|
||||||
|
try:
|
||||||
|
input_user = self.client.resolve_peer(user_id)
|
||||||
|
except PeerIdInvalid:
|
||||||
|
input_user = None
|
||||||
|
|
||||||
entity = (
|
entity = (
|
||||||
Mention(offset=start, length=len(text), user_id=input_user)
|
Mention(offset=start, length=len(text), user_id=input_user)
|
||||||
if input_user
|
if input_user else MentionInvalid(offset=start, length=len(text), user_id=user_id)
|
||||||
else MentionInvalid(offset=start, length=len(text), user_id=user_id)
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
entity = Url(offset=start, length=len(text), url=url)
|
entity = Url(offset=start, length=len(text), url=url)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user