Add Mention link

This commit is contained in:
Dan 2020-07-08 23:37:24 +02:00
parent 55d0b93cf0
commit 74e5a5a5e1
3 changed files with 57 additions and 6 deletions

View File

@ -20,4 +20,5 @@ from .base_client import BaseClient
from .dispatcher import Dispatcher from .dispatcher import Dispatcher
from .emoji import Emoji from .emoji import Emoji
from .file_data import FileData from .file_data import FileData
from .link import Link
from .syncer import Syncer from .syncer import Syncer

View File

@ -0,0 +1,52 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import html
class Link(str):
HTML = "<a href={url}>{text}</a>"
MD = "[{text}]({url})"
def __init__(self, url: str, text: str, style: str):
super().__init__()
self.url = url
self.text = text
self.style = style
@staticmethod
def format(url: str, text: str, style: str):
if style in ["md", "markdown"]:
fmt = Link.MD
elif style in ["combined", "html", None]:
fmt = Link.HTML
else:
raise ValueError("{} is not a valid style/parse mode".format(style))
return fmt.format(url=url, text=html.escape(text))
# noinspection PyArgumentList
def __new__(cls, url, text, style):
return str.__new__(cls, Link.format(url, text, style))
def __call__(self, other: str = None, *, style: str = None):
return Link.format(self.url, other or self.text, style or self.style)
def __str__(self):
return Link.format(self.url, self.text, self.style)

View File

@ -16,11 +16,11 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import html
from typing import List from typing import List
import pyrogram import pyrogram
from pyrogram.api import types from pyrogram.api import types
from pyrogram.client.ext import Link
from .chat_photo import ChatPhoto from .chat_photo import ChatPhoto
from .restriction import Restriction from .restriction import Restriction
from ..object import Object from ..object import Object
@ -158,11 +158,9 @@ class User(Object, Update):
self.photo = photo self.photo = photo
self.restrictions = restrictions self.restrictions = restrictions
def __format__(self, format_spec): @property
if format_spec == "mention": def mention(self):
return '<a href="tg://user?id={0}">{1}</a>'.format(self.id, html.escape(self.first_name)) return Link("tg://user?id={}".format(self.id), self.first_name, self._client.parse_mode)
return html.escape(str(self))
@staticmethod @staticmethod
def _parse(client, user: types.User) -> "User" or None: def _parse(client, user: types.User) -> "User" or None: