Skip to content

Commit f0c1cb0

Browse files
committed
Rework InlineQueryResultArticle. Also add *Photo and *Animation types
1 parent 4274ef9 commit f0c1cb0

File tree

6 files changed

+303
-41
lines changed

6 files changed

+303
-41
lines changed

compiler/docs/compiler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ def get_title_list(s: str) -> list:
341341
InlineQuery
342342
InlineQueryResult
343343
InlineQueryResultArticle
344+
InlineQueryResultPhoto
345+
InlineQueryResultAnimation
344346
""",
345347
input_message_content="""
346348
InputMessageContent

pyrogram/client/types/inline_mode/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
from .inline_query import InlineQuery
2020
from .inline_query_result import InlineQueryResult
21+
from .inline_query_result_animation import InlineQueryResultAnimation
2122
from .inline_query_result_article import InlineQueryResultArticle
23+
from .inline_query_result_photo import InlineQueryResultPhoto
2224

2325
__all__ = [
24-
"InlineQuery", "InlineQueryResult", "InlineQueryResultArticle"
26+
"InlineQuery", "InlineQueryResult", "InlineQueryResultArticle", "InlineQueryResultPhoto",
27+
"InlineQueryResultAnimation"
2528
]

pyrogram/client/types/inline_mode/inline_query_result.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from uuid import uuid4
20+
21+
from ..bots_and_keyboards import InlineKeyboardMarkup
22+
from ..input_message_content import InputMessageContent
1923
from ..object import Object
2024

2125
"""- :obj:`InlineQueryResultCachedAudio`
@@ -45,15 +49,25 @@ class InlineQueryResult(Object):
4549
Pyrogram currently supports results of the following types:
4650
4751
- :obj:`InlineQueryResultArticle`
52+
- :obj:`InlineQueryResultPhoto`
53+
- :obj:`InlineQueryResultAnimation`
4854
"""
4955

50-
__slots__ = ["type", "id"]
56+
__slots__ = ["type", "id", "input_message_content", "reply_markup"]
5157

52-
def __init__(self, type: str, id: str):
58+
def __init__(
59+
self,
60+
type: str,
61+
id: str,
62+
input_message_content: InputMessageContent,
63+
reply_markup: InlineKeyboardMarkup
64+
):
5365
super().__init__()
5466

5567
self.type = type
56-
self.id = id
68+
self.id = str(uuid4()) if id is None else id
69+
self.input_message_content = input_message_content
70+
self.reply_markup = reply_markup
5771

5872
def write(self):
5973
pass
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2018 Dan Tès <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 Union
20+
21+
from pyrogram.api import types
22+
from .inline_query_result import InlineQueryResult
23+
from ..bots_and_keyboards import InlineKeyboardMarkup
24+
from ..input_message_content import InputMessageContent
25+
from ...parser import Parser
26+
27+
28+
class InlineQueryResultAnimation(InlineQueryResult):
29+
"""Link to an animated GIF file.
30+
31+
By default, this animated GIF file will be sent by the user with optional caption.
32+
Alternatively, you can use *input_message_content* to send a message with the specified content instead of the
33+
animation.
34+
35+
Parameters:
36+
animation_url (``str``):
37+
A valid URL for the animated GIF file.
38+
File size must not exceed 1 MB.
39+
40+
thumb_url (``str``, *optional*):
41+
URL of the static thumbnail for the result (jpeg or gif)
42+
Defaults to the value passed in *animation_url*.
43+
44+
id (``str``, *optional*):
45+
Unique identifier for this result, 1-64 bytes.
46+
Defaults to a randomly generated UUID4.
47+
48+
title (``str``, *optional*):
49+
Title for the result.
50+
51+
description (``str``, *optional*):
52+
Short description of the result.
53+
54+
caption (``str``, *optional*):
55+
Caption of the photo 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:`InlineKeyboardMarkup`, *optional*):
65+
An InlineKeyboardMarkup object.
66+
67+
input_message_content (:obj:`InputMessageContent`):
68+
Content of the message to be sent instead of the photo.
69+
"""
70+
71+
__slots__ = [
72+
"animation_url", "thumb_url", "title", "description", "caption", "parse_mode", "reply_markup",
73+
"input_message_content"
74+
]
75+
76+
def __init__(
77+
self,
78+
animation_url: str,
79+
thumb_url: str = None,
80+
id: str = None,
81+
title: str = None,
82+
description: str = None,
83+
caption: str = None,
84+
parse_mode: Union[str, None] = object,
85+
reply_markup: InlineKeyboardMarkup = None,
86+
input_message_content: InputMessageContent = None
87+
):
88+
super().__init__("gif", id, input_message_content, reply_markup)
89+
90+
self.animation_url = animation_url
91+
self.thumb_url = thumb_url
92+
self.title = title
93+
self.description = description
94+
self.caption = caption
95+
self.parse_mode = parse_mode
96+
self.reply_markup = reply_markup
97+
self.input_message_content = input_message_content
98+
99+
def write(self):
100+
animation = types.InputWebDocument(
101+
url=self.animation_url,
102+
size=0,
103+
mime_type="image/gif",
104+
attributes=[]
105+
)
106+
107+
if self.thumb_url is None:
108+
thumb = animation
109+
else:
110+
thumb = types.InputWebDocument(
111+
url=self.thumb_url,
112+
size=0,
113+
mime_type="image/gif",
114+
attributes=[]
115+
)
116+
117+
return types.InputBotInlineResult(
118+
id=self.id,
119+
type=self.type,
120+
title=self.title,
121+
description=self.description,
122+
thumb=thumb,
123+
content=animation,
124+
send_message=(
125+
self.input_message_content.write(self.reply_markup)
126+
if self.input_message_content
127+
else types.InputBotInlineMessageMediaAuto(
128+
reply_markup=self.reply_markup.write() if self.reply_markup else None,
129+
**(Parser(None)).parse(self.caption, self.parse_mode)
130+
)
131+
)
132+
)

pyrogram/client/types/inline_mode/inline_query_result_article.py

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,25 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from typing import Any
20-
2119
from pyrogram.api import types
2220
from .inline_query_result import InlineQueryResult
21+
from ..bots_and_keyboards import InlineKeyboardMarkup
22+
from ..input_message_content import InputMessageContent
2323

2424

2525
class InlineQueryResultArticle(InlineQueryResult):
2626
"""Link to an article or web page.
2727
28-
TODO: Hide url?
29-
3028
Parameters:
31-
id (``str``):
32-
Unique identifier for this result, 1-64 bytes.
33-
3429
title (``str``):
3530
Title for the result.
3631
3732
input_message_content (:obj:`InputMessageContent`):
3833
Content of the message to be sent.
3934
40-
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
41-
Inline keyboard attached to the message.
35+
id (``str``, *optional*):
36+
Unique identifier for this result, 1-64 bytes.
37+
Defaults to a randomly generated UUID4.
4238
4339
url (``str``, *optional*):
4440
URL of the result.
@@ -47,46 +43,34 @@ class InlineQueryResultArticle(InlineQueryResult):
4743
Short description of the result.
4844
4945
thumb_url (``str``, *optional*):
50-
Url of the thumbnail for the result.
51-
52-
thumb_width (``int``, *optional*):
53-
Thumbnail width.
46+
URL of the thumbnail for the result.
5447
55-
thumb_height (``int``, *optional*):
56-
Thumbnail height.
48+
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
49+
Inline keyboard attached to the message.
5750
"""
5851

59-
__slots__ = [
60-
"title", "input_message_content", "reply_markup", "url", "description", "thumb_url", "thumb_width",
61-
"thumb_height"
62-
]
52+
__slots__ = ["title", "url", "description", "thumb_url"]
6353

6454
def __init__(
6555
self,
66-
id: Any,
6756
title: str,
68-
input_message_content,
69-
reply_markup=None,
57+
input_message_content: InputMessageContent,
58+
id: str = None,
59+
reply_markup: InlineKeyboardMarkup = None,
7060
url: str = None,
7161
description: str = None,
72-
thumb_url: str = None,
73-
thumb_width: int = 0,
74-
thumb_height: int = 0
62+
thumb_url: str = None
7563
):
76-
super().__init__("article", id)
64+
super().__init__("article", id, input_message_content, reply_markup)
7765

7866
self.title = title
79-
self.input_message_content = input_message_content
80-
self.reply_markup = reply_markup
8167
self.url = url
8268
self.description = description
8369
self.thumb_url = thumb_url
84-
self.thumb_width = thumb_width
85-
self.thumb_height = thumb_height
8670

8771
def write(self):
8872
return types.InputBotInlineResult(
89-
id=str(self.id),
73+
id=self.id,
9074
type=self.type,
9175
send_message=self.input_message_content.write(self.reply_markup),
9276
title=self.title,
@@ -96,11 +80,6 @@ def write(self):
9680
url=self.thumb_url,
9781
size=0,
9882
mime_type="image/jpeg",
99-
attributes=[
100-
types.DocumentAttributeImageSize(
101-
w=self.thumb_width,
102-
h=self.thumb_height
103-
)
104-
]
83+
attributes=[]
10584
) if self.thumb_url else None
10685
)

0 commit comments

Comments
 (0)