Fixes for InlineQueryResult{Audio,Video}

This commit is contained in:
Dan 2021-05-12 09:11:52 +02:00
parent a56b1a3287
commit 5fdb361487
2 changed files with 66 additions and 49 deletions

View File

@ -16,11 +16,10 @@
# 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/>.
from typing import Union from typing import Union, List
from pyrogram import raw import pyrogram
from pyrogram import types from pyrogram import raw, types, utils
from pyrogram.parser import Parser
from pyrogram.types import InlineQueryResult from pyrogram.types import InlineQueryResult
@ -69,17 +68,17 @@ class InlineQueryResultAudio(InlineQueryResult):
""" """
def __init__( def __init__(
self, self,
audio_url: str, audio_url: str,
title: str, title: str,
id: str = None, id: str = None,
performer: str = "", performer: str = "",
audio_duration: int = 0, audio_duration: int = 0,
caption: str = "", caption: str = "",
parse_mode: Union[str, None] = object, parse_mode: Union[str, None] = object,
caption_entities: List["types.MessageEntity"] = None, caption_entities: List["types.MessageEntity"] = None,
reply_markup: "types.InlineKeyboardMarkup" = None, reply_markup: "types.InlineKeyboardMarkup" = None,
input_message_content: "types.InputMessageContent" = None input_message_content: "types.InputMessageContent" = None
): ):
super().__init__("audio", id, input_message_content, reply_markup) super().__init__("audio", id, input_message_content, reply_markup)
@ -91,7 +90,7 @@ class InlineQueryResultAudio(InlineQueryResult):
self.parse_mode = parse_mode self.parse_mode = parse_mode
self.caption_entities = caption_entities self.caption_entities = caption_entities
async def write(self): async def write(self, client: "pyrogram.Client"):
audio = raw.types.InputWebDocument( audio = raw.types.InputWebDocument(
url=self.audio_url, url=self.audio_url,
size=0, size=0,
@ -100,9 +99,9 @@ class InlineQueryResultAudio(InlineQueryResult):
duration=self.audio_duration, duration=self.audio_duration,
title=self.title, title=self.title,
performer=self.performer performer=self.performer
)], )]
) )
message, entities = (await utils.parse_text_entities( message, entities = (await utils.parse_text_entities(
client, self.caption, self.parse_mode, self.caption_entities client, self.caption, self.parse_mode, self.caption_entities
)).values() )).values()

View File

@ -16,18 +16,17 @@
# 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/>.
from typing import Optional from typing import Optional, List
from pyrogram import raw import pyrogram
from pyrogram import types from pyrogram import raw, types, utils
from pyrogram.parser import Parser
from .inline_query_result import InlineQueryResult from .inline_query_result import InlineQueryResult
class InlineQueryResultVideo(InlineQueryResult): class InlineQueryResultVideo(InlineQueryResult):
"""Link to a video. """Link to a page containing an embedded video player or a video file.
By default, this video will be sent by the user with optional caption. By default, this video file will be sent by the user with an optional caption.
Alternatively, you can use *input_message_content* to send a message with the specified content instead of the Alternatively, you can use *input_message_content* to send a message with the specified content instead of the
video. video.
@ -38,21 +37,31 @@ class InlineQueryResultVideo(InlineQueryResult):
thumb_url (``str``): thumb_url (``str``):
URL of the thumbnail (jpeg only) for the video. URL of the thumbnail (jpeg only) for the video.
title (``str``):
Title for the result.
id (``str``, *optional*): id (``str``, *optional*):
Unique identifier for this result, 1-64 bytes. Unique identifier for this result, 1-64 bytes.
Defaults to a randomly generated UUID4. Defaults to a randomly generated UUID4.
title (``str``, *optional*): mime_type (``str``):
Title for the result. Mime type of the content of video url, "text/html" or "video/mp4".
Defaults to "video/mp4".
mime_type (``str``, *optional*): video_width (``int``):
Mime type of the content of video url, text/html or video/mp4. Video width.
video_height (``int``):
Video height.
video_duration (``int``):
Video duration in seconds.
description (``str``, *optional*): description (``str``, *optional*):
Short description of the result. Short description of the result.
caption (``str``, *optional*): caption (``str``, *optional*):
Caption of the video to be sent, 0-1024 characters. Caption of the photo to be sent, 0-1024 characters.
parse_mode (``str``, *optional*): parse_mode (``str``, *optional*):
By default, texts are parsed using both Markdown and HTML styles. By default, texts are parsed using both Markdown and HTML styles.
@ -61,8 +70,11 @@ class InlineQueryResultVideo(InlineQueryResult):
Pass "html" to enable HTML-style parsing only. Pass "html" to enable HTML-style parsing only.
Pass None to completely disable style parsing. Pass None to completely disable style parsing.
caption_entities (List of :obj:`~pyrogram.types.MessageEntity`):
List of special entities that appear in the caption, which can be specified instead of *parse_mode*.
reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*): reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*):
An InlineKeyboardMarkup object. Inline keyboard attached to the message
input_message_content (:obj:`~pyrogram.types.InputMessageContent`): input_message_content (:obj:`~pyrogram.types.InputMessageContent`):
Content of the message to be sent instead of the video. This field is required if InlineQueryResultVideo is Content of the message to be sent instead of the video. This field is required if InlineQueryResultVideo is
@ -73,42 +85,43 @@ class InlineQueryResultVideo(InlineQueryResult):
self, self,
video_url: str, video_url: str,
thumb_url: str, thumb_url: str,
title: str,
id: str = None, id: str = None,
title: str = None, mime_type: str = "video/mp4",
mime_type: str = None, video_width: int = 0,
video_height: int = 0,
video_duration: int = 0,
description: str = None, description: str = None,
caption: str = "", caption: str = "",
parse_mode: Optional[str] = object, parse_mode: Optional[str] = object,
caption_entities: List["types.MessageEntity"] = None,
reply_markup: "types.InlineKeyboardMarkup" = None, reply_markup: "types.InlineKeyboardMarkup" = None,
input_message_content: "types.InputMessageContent" = None input_message_content: "types.InputMessageContent" = None
): ):
super().__init__("video", id, input_message_content, reply_markup) super().__init__("video", id, input_message_content, reply_markup)
self.video_url = video_url self.video_url = video_url
self.thumb_url = thumb_url self.thumb_url = thumb_url
self.title = title self.title = title
self.video_width = video_width
if mime_type != "text/html" and mime_type != "video/mp4": self.video_height = video_height
raise ValueError("Invalid mime type") self.video_duration = video_duration
self.mime_type = mime_type
self.description = description self.description = description
self.caption = caption self.caption = caption
self.parse_mode = parse_mode self.parse_mode = parse_mode
self.reply_markup = reply_markup self.caption_entities = caption_entities
self.mime_type = mime_type
if mime_type == "text/html" and input_message_content is None: async def write(self, client: "pyrogram.Client"):
raise ValueError("input_message_content is required for videos with `text/html` mime type")
self.input_message_content = input_message_content
async def write(self):
video = raw.types.InputWebDocument( video = raw.types.InputWebDocument(
url=self.video_url, url=self.video_url,
size=0, size=0,
mime_type=self.mime_type, mime_type=self.mime_type,
attributes=[] attributes=[raw.types.DocumentAttributeVideo(
duration=self.video_duration,
w=self.video_width,
h=self.video_height
)]
) )
thumb = raw.types.InputWebDocument( thumb = raw.types.InputWebDocument(
@ -118,6 +131,10 @@ class InlineQueryResultVideo(InlineQueryResult):
attributes=[] attributes=[]
) )
message, entities = (await utils.parse_text_entities(
client, self.caption, self.parse_mode, self.caption_entities
)).values()
return raw.types.InputBotInlineResult( return raw.types.InputBotInlineResult(
id=self.id, id=self.id,
type=self.type, type=self.type,
@ -126,11 +143,12 @@ class InlineQueryResultVideo(InlineQueryResult):
thumb=thumb, thumb=thumb,
content=video, content=video,
send_message=( send_message=(
await self.input_message_content.write(self.reply_markup) await self.input_message_content.write(client, self.reply_markup)
if self.input_message_content if self.input_message_content
else raw.types.InputBotInlineMessageMediaAuto( else raw.types.InputBotInlineMessageMediaAuto(
reply_markup=self.reply_markup.write() if self.reply_markup else None, reply_markup=await self.reply_markup.write(client) if self.reply_markup else None,
**await(Parser(None)).parse(self.caption, self.parse_mode) message=message,
entities=entities
) )
) )
) )