Skip to content

Commit a56b1a3

Browse files
LёNyadelivrance
authored andcommitted
add InlineQueryResultVideo
1 parent b4bdab1 commit a56b1a3

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

pyrogram/types/inline_mode/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
from .inline_query_result_animation import InlineQueryResultAnimation
2323
from .inline_query_result_article import InlineQueryResultArticle
2424
from .inline_query_result_photo import InlineQueryResultPhoto
25+
from .inline_query_result_video import InlineQueryResultVideo
2526
from .inline_query_result_audio import InlineQueryResultAudio
2627

2728
__all__ = [
2829
"InlineQuery", "InlineQueryResult", "InlineQueryResultArticle", "InlineQueryResultPhoto",
29-
"InlineQueryResultAnimation", "InlineQueryResultAudio", "ChosenInlineResult"
30+
"InlineQueryResultAnimation", "InlineQueryResultAudio", "InlineQueryResultVideo", "ChosenInlineResult"
3031
]
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Optional
20+
21+
from pyrogram import raw
22+
from pyrogram import types
23+
from pyrogram.parser import Parser
24+
from .inline_query_result import InlineQueryResult
25+
26+
27+
class InlineQueryResultVideo(InlineQueryResult):
28+
"""Link to a video.
29+
30+
By default, this video will be sent by the user with optional caption.
31+
Alternatively, you can use *input_message_content* to send a message with the specified content instead of the
32+
video.
33+
34+
Parameters:
35+
video_url (``str``):
36+
A valid URL for the embedded video player or video file.
37+
38+
thumb_url (``str``):
39+
URL of the thumbnail (jpeg only) for the video.
40+
41+
id (``str``, *optional*):
42+
Unique identifier for this result, 1-64 bytes.
43+
Defaults to a randomly generated UUID4.
44+
45+
title (``str``, *optional*):
46+
Title for the result.
47+
48+
mime_type (``str``, *optional*):
49+
Mime type of the content of video url, “text/html” or “video/mp4”.
50+
51+
description (``str``, *optional*):
52+
Short description of the result.
53+
54+
caption (``str``, *optional*):
55+
Caption of the video to be sent, 0-1024 characters.
56+
57+
parse_mode (``str``, *optional*):
58+
By default, texts are parsed using both Markdown and HTML styles.
59+
You can combine both syntaxes together.
60+
Pass "markdown" or "md" to enable Markdown-style parsing only.
61+
Pass "html" to enable HTML-style parsing only.
62+
Pass None to completely disable style parsing.
63+
64+
reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*):
65+
An InlineKeyboardMarkup object.
66+
67+
input_message_content (:obj:`~pyrogram.types.InputMessageContent`):
68+
Content of the message to be sent instead of the video. This field is required if InlineQueryResultVideo is
69+
used to send an HTML-page as a result (e.g., a YouTube video).
70+
"""
71+
72+
def __init__(
73+
self,
74+
video_url: str,
75+
thumb_url: str,
76+
id: str = None,
77+
title: str = None,
78+
mime_type: str = None,
79+
description: str = None,
80+
caption: str = "",
81+
parse_mode: Optional[str] = object,
82+
reply_markup: "types.InlineKeyboardMarkup" = None,
83+
input_message_content: "types.InputMessageContent" = None
84+
):
85+
super().__init__("video", id, input_message_content, reply_markup)
86+
87+
self.video_url = video_url
88+
89+
self.thumb_url = thumb_url
90+
self.title = title
91+
92+
if mime_type != "text/html" and mime_type != "video/mp4":
93+
raise ValueError("Invalid mime type")
94+
95+
self.mime_type = mime_type
96+
self.description = description
97+
self.caption = caption
98+
self.parse_mode = parse_mode
99+
self.reply_markup = reply_markup
100+
101+
if mime_type == "text/html" and input_message_content is None:
102+
raise ValueError("input_message_content is required for videos with `text/html` mime type")
103+
104+
self.input_message_content = input_message_content
105+
106+
async def write(self):
107+
video = raw.types.InputWebDocument(
108+
url=self.video_url,
109+
size=0,
110+
mime_type=self.mime_type,
111+
attributes=[]
112+
)
113+
114+
thumb = raw.types.InputWebDocument(
115+
url=self.thumb_url,
116+
size=0,
117+
mime_type="image/jpeg",
118+
attributes=[]
119+
)
120+
121+
return raw.types.InputBotInlineResult(
122+
id=self.id,
123+
type=self.type,
124+
title=self.title,
125+
description=self.description,
126+
thumb=thumb,
127+
content=video,
128+
send_message=(
129+
await self.input_message_content.write(self.reply_markup)
130+
if self.input_message_content
131+
else raw.types.InputBotInlineMessageMediaAuto(
132+
reply_markup=self.reply_markup.write() if self.reply_markup else None,
133+
**await(Parser(None)).parse(self.caption, self.parse_mode)
134+
)
135+
)
136+
)

0 commit comments

Comments
 (0)