diff --git a/pyrogram/methods/messages/send_web_page.py b/pyrogram/methods/messages/send_web_page.py index 221f18d1..2050f423 100644 --- a/pyrogram/methods/messages/send_web_page.py +++ b/pyrogram/methods/messages/send_web_page.py @@ -28,7 +28,7 @@ class SendWebPage: async def send_web_page( self: "pyrogram.Client", chat_id: Union[int, str], - url: str, + url: str = None, text: str = "", parse_mode: Optional["enums.ParseMode"] = None, entities: List["types.MessageEntity"] = None, @@ -53,7 +53,7 @@ class SendWebPage: "types.ForceReply" ] = None ) -> "types.Message": - """Send text Web Page Preview. + """Send Web Page Preview. .. include:: /_includes/usable-by/users-bots.rst @@ -64,12 +64,13 @@ class SendWebPage: For a contact that exists in your Telegram address book you can use his phone number (str). You can also use chat public link in form of *t.me/* (str). - url (``str``): - Link that will be previewed. - text (``str``, *optional*): Text of the message to be sent. + url (``str``, *optional*): + Link that will be previewed. + If url not specified, the first URL found in the text will be used. + parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*): By default, texts are parsed using both Markdown and HTML styles. You can combine both syntaxes together. @@ -131,7 +132,7 @@ class SendWebPage: instructions to remove reply keyboard or to force a reply from the user. Returns: - :obj:`~pyrogram.types.Message`: On success, the sent text message is returned. + :obj:`~pyrogram.types.Message`: On success, the sent message is returned. Example: .. code-block:: python @@ -141,6 +142,18 @@ class SendWebPage: """ message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values() + if not url: + if entities: + for entity in entities: + if isinstance(entity, enums.MessageEntityType.URL): + url = entity.url + break + + if not url: + url = utils.get_first_url(message) + + if not url: + raise ValueError("URL not specified") reply_to = await utils.get_reply_to( client=self, diff --git a/pyrogram/utils.py b/pyrogram/utils.py index 6fc51b86..84e1deca 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -21,6 +21,7 @@ import asyncio import base64 import functools import hashlib +import re import os import struct from concurrent.futures.thread import ThreadPoolExecutor @@ -529,3 +530,11 @@ async def get_reply_to( story_id=reply_to_story_id ) return reply_to + +def get_first_url(text): + text = re.sub(r"^\s*(<[\w<>=\s\"]*>)\s*", r"\1", text) + text = re.sub(r"\s*(]*>)\s*$", r"\1", text) + + matches = re.findall(r"(https?):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])", text) + + return f"{matches[0][0]}://{matches[0][1]}{matches[0][2]}" if matches else None