Make url parameter optional for send_web_page

Signed-off-by: wulan17 <wulan17@komodos.id>
This commit is contained in:
KurimuzonAkuma 2024-01-17 11:39:00 +03:00 committed by wulan17
parent 757b081eeb
commit 96917949a9
No known key found for this signature in database
GPG key ID: 737814D4B5FF0420
2 changed files with 28 additions and 6 deletions

View file

@ -28,7 +28,7 @@ class SendWebPage:
async def send_web_page( async def send_web_page(
self: "pyrogram.Client", self: "pyrogram.Client",
chat_id: Union[int, str], chat_id: Union[int, str],
url: str, url: str = None,
text: str = "", text: str = "",
parse_mode: Optional["enums.ParseMode"] = None, parse_mode: Optional["enums.ParseMode"] = None,
entities: List["types.MessageEntity"] = None, entities: List["types.MessageEntity"] = None,
@ -53,7 +53,7 @@ class SendWebPage:
"types.ForceReply" "types.ForceReply"
] = None ] = None
) -> "types.Message": ) -> "types.Message":
"""Send text Web Page Preview. """Send Web Page Preview.
.. include:: /_includes/usable-by/users-bots.rst .. 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). 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/<username>* (str). You can also use chat public link in form of *t.me/<username>* (str).
url (``str``):
Link that will be previewed.
text (``str``, *optional*): text (``str``, *optional*):
Text of the message to be sent. 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*): parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*):
By default, texts are parsed using both Markdown and HTML styles. By default, texts are parsed using both Markdown and HTML styles.
You can combine both syntaxes together. 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. instructions to remove reply keyboard or to force a reply from the user.
Returns: 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: Example:
.. code-block:: python .. code-block:: python
@ -141,6 +142,18 @@ class SendWebPage:
""" """
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values() 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( reply_to = await utils.get_reply_to(
client=self, client=self,

View file

@ -21,6 +21,7 @@ import asyncio
import base64 import base64
import functools import functools
import hashlib import hashlib
import re
import os import os
import struct import struct
from concurrent.futures.thread import ThreadPoolExecutor from concurrent.futures.thread import ThreadPoolExecutor
@ -529,3 +530,11 @@ async def get_reply_to(
story_id=reply_to_story_id story_id=reply_to_story_id
) )
return reply_to return reply_to
def get_first_url(text):
text = re.sub(r"^\s*(<[\w<>=\s\"]*>)\s*", r"\1", text)
text = re.sub(r"\s*(</[\w</>]*>)\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