Rework InlineQueryResultArticle. Also add *Photo and *Animation types

This commit is contained in:
Dan 2019-07-21 23:18:38 +02:00
parent 4274ef9639
commit f0c1cb00ca
6 changed files with 303 additions and 41 deletions

View File

@ -341,6 +341,8 @@ def pyrogram_api():
InlineQuery InlineQuery
InlineQueryResult InlineQueryResult
InlineQueryResultArticle InlineQueryResultArticle
InlineQueryResultPhoto
InlineQueryResultAnimation
""", """,
input_message_content=""" input_message_content="""
InputMessageContent InputMessageContent

View File

@ -18,8 +18,11 @@
from .inline_query import InlineQuery from .inline_query import InlineQuery
from .inline_query_result import InlineQueryResult from .inline_query_result import InlineQueryResult
from .inline_query_result_animation import InlineQueryResultAnimation
from .inline_query_result_article import InlineQueryResultArticle from .inline_query_result_article import InlineQueryResultArticle
from .inline_query_result_photo import InlineQueryResultPhoto
__all__ = [ __all__ = [
"InlineQuery", "InlineQueryResult", "InlineQueryResultArticle" "InlineQuery", "InlineQueryResult", "InlineQueryResultArticle", "InlineQueryResultPhoto",
"InlineQueryResultAnimation"
] ]

View File

@ -16,6 +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 uuid import uuid4
from ..bots_and_keyboards import InlineKeyboardMarkup
from ..input_message_content import InputMessageContent
from ..object import Object from ..object import Object
"""- :obj:`InlineQueryResultCachedAudio` """- :obj:`InlineQueryResultCachedAudio`
@ -45,15 +49,25 @@ class InlineQueryResult(Object):
Pyrogram currently supports results of the following types: Pyrogram currently supports results of the following types:
- :obj:`InlineQueryResultArticle` - :obj:`InlineQueryResultArticle`
- :obj:`InlineQueryResultPhoto`
- :obj:`InlineQueryResultAnimation`
""" """
__slots__ = ["type", "id"] __slots__ = ["type", "id", "input_message_content", "reply_markup"]
def __init__(self, type: str, id: str): def __init__(
self,
type: str,
id: str,
input_message_content: InputMessageContent,
reply_markup: InlineKeyboardMarkup
):
super().__init__() super().__init__()
self.type = type self.type = type
self.id = id self.id = str(uuid4()) if id is None else id
self.input_message_content = input_message_content
self.reply_markup = reply_markup
def write(self): def write(self):
pass pass

View File

@ -0,0 +1,132 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union
from pyrogram.api import types
from .inline_query_result import InlineQueryResult
from ..bots_and_keyboards import InlineKeyboardMarkup
from ..input_message_content import InputMessageContent
from ...parser import Parser
class InlineQueryResultAnimation(InlineQueryResult):
"""Link to an animated GIF file.
By default, this animated GIF file will be sent by the user with optional caption.
Alternatively, you can use *input_message_content* to send a message with the specified content instead of the
animation.
Parameters:
animation_url (``str``):
A valid URL for the animated GIF file.
File size must not exceed 1 MB.
thumb_url (``str``, *optional*):
URL of the static thumbnail for the result (jpeg or gif)
Defaults to the value passed in *animation_url*.
id (``str``, *optional*):
Unique identifier for this result, 1-64 bytes.
Defaults to a randomly generated UUID4.
title (``str``, *optional*):
Title for the result.
description (``str``, *optional*):
Short description of the result.
caption (``str``, *optional*):
Caption of the photo to be sent, 0-1024 characters.
parse_mode (``str``, *optional*):
By default, texts are parsed using both Markdown and HTML styles.
You can combine both syntaxes together.
Pass "markdown" or "md" to enable Markdown-style parsing only.
Pass "html" to enable HTML-style parsing only.
Pass None to completely disable style parsing.
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
An InlineKeyboardMarkup object.
input_message_content (:obj:`InputMessageContent`):
Content of the message to be sent instead of the photo.
"""
__slots__ = [
"animation_url", "thumb_url", "title", "description", "caption", "parse_mode", "reply_markup",
"input_message_content"
]
def __init__(
self,
animation_url: str,
thumb_url: str = None,
id: str = None,
title: str = None,
description: str = None,
caption: str = None,
parse_mode: Union[str, None] = object,
reply_markup: InlineKeyboardMarkup = None,
input_message_content: InputMessageContent = None
):
super().__init__("gif", id, input_message_content, reply_markup)
self.animation_url = animation_url
self.thumb_url = thumb_url
self.title = title
self.description = description
self.caption = caption
self.parse_mode = parse_mode
self.reply_markup = reply_markup
self.input_message_content = input_message_content
def write(self):
animation = types.InputWebDocument(
url=self.animation_url,
size=0,
mime_type="image/gif",
attributes=[]
)
if self.thumb_url is None:
thumb = animation
else:
thumb = types.InputWebDocument(
url=self.thumb_url,
size=0,
mime_type="image/gif",
attributes=[]
)
return types.InputBotInlineResult(
id=self.id,
type=self.type,
title=self.title,
description=self.description,
thumb=thumb,
content=animation,
send_message=(
self.input_message_content.write(self.reply_markup)
if self.input_message_content
else types.InputBotInlineMessageMediaAuto(
reply_markup=self.reply_markup.write() if self.reply_markup else None,
**(Parser(None)).parse(self.caption, self.parse_mode)
)
)
)

View File

@ -16,29 +16,25 @@
# 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 Any
from pyrogram.api import types from pyrogram.api import types
from .inline_query_result import InlineQueryResult from .inline_query_result import InlineQueryResult
from ..bots_and_keyboards import InlineKeyboardMarkup
from ..input_message_content import InputMessageContent
class InlineQueryResultArticle(InlineQueryResult): class InlineQueryResultArticle(InlineQueryResult):
"""Link to an article or web page. """Link to an article or web page.
TODO: Hide url?
Parameters: Parameters:
id (``str``):
Unique identifier for this result, 1-64 bytes.
title (``str``): title (``str``):
Title for the result. Title for the result.
input_message_content (:obj:`InputMessageContent`): input_message_content (:obj:`InputMessageContent`):
Content of the message to be sent. Content of the message to be sent.
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*): id (``str``, *optional*):
Inline keyboard attached to the message. Unique identifier for this result, 1-64 bytes.
Defaults to a randomly generated UUID4.
url (``str``, *optional*): url (``str``, *optional*):
URL of the result. URL of the result.
@ -47,46 +43,34 @@ class InlineQueryResultArticle(InlineQueryResult):
Short description of the result. Short description of the result.
thumb_url (``str``, *optional*): thumb_url (``str``, *optional*):
Url of the thumbnail for the result. URL of the thumbnail for the result.
thumb_width (``int``, *optional*): reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
Thumbnail width. Inline keyboard attached to the message.
thumb_height (``int``, *optional*):
Thumbnail height.
""" """
__slots__ = [ __slots__ = ["title", "url", "description", "thumb_url"]
"title", "input_message_content", "reply_markup", "url", "description", "thumb_url", "thumb_width",
"thumb_height"
]
def __init__( def __init__(
self, self,
id: Any,
title: str, title: str,
input_message_content, input_message_content: InputMessageContent,
reply_markup=None, id: str = None,
reply_markup: InlineKeyboardMarkup = None,
url: str = None, url: str = None,
description: str = None, description: str = None,
thumb_url: str = None, thumb_url: str = None
thumb_width: int = 0,
thumb_height: int = 0
): ):
super().__init__("article", id) super().__init__("article", id, input_message_content, reply_markup)
self.title = title self.title = title
self.input_message_content = input_message_content
self.reply_markup = reply_markup
self.url = url self.url = url
self.description = description self.description = description
self.thumb_url = thumb_url self.thumb_url = thumb_url
self.thumb_width = thumb_width
self.thumb_height = thumb_height
def write(self): def write(self):
return types.InputBotInlineResult( return types.InputBotInlineResult(
id=str(self.id), id=self.id,
type=self.type, type=self.type,
send_message=self.input_message_content.write(self.reply_markup), send_message=self.input_message_content.write(self.reply_markup),
title=self.title, title=self.title,
@ -96,11 +80,6 @@ class InlineQueryResultArticle(InlineQueryResult):
url=self.thumb_url, url=self.thumb_url,
size=0, size=0,
mime_type="image/jpeg", mime_type="image/jpeg",
attributes=[ attributes=[]
types.DocumentAttributeImageSize(
w=self.thumb_width,
h=self.thumb_height
)
]
) if self.thumb_url else None ) if self.thumb_url else None
) )

View File

@ -0,0 +1,132 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union
from pyrogram.api import types
from .inline_query_result import InlineQueryResult
from ..bots_and_keyboards import InlineKeyboardMarkup
from ..input_message_content import InputMessageContent
from ...parser import Parser
class InlineQueryResultPhoto(InlineQueryResult):
"""Link to a photo.
By default, this photo will be sent by the user with optional caption.
Alternatively, you can use *input_message_content* to send a message with the specified content instead of the
photo.
Parameters:
photo_url (``str``):
A valid URL of the photo.
Photo must be in jpeg format an must not exceed 5 MB.
thumb_url (``str``, *optional*):
URL of the thumbnail for the photo.
Defaults to the value passed in *photo_url*.
id (``str``, *optional*):
Unique identifier for this result, 1-64 bytes.
Defaults to a randomly generated UUID4.
title (``str``, *optional*):
Title for the result.
description (``str``, *optional*):
Short description of the result.
caption (``str``, *optional*):
Caption of the photo to be sent, 0-1024 characters.
parse_mode (``str``, *optional*):
By default, texts are parsed using both Markdown and HTML styles.
You can combine both syntaxes together.
Pass "markdown" or "md" to enable Markdown-style parsing only.
Pass "html" to enable HTML-style parsing only.
Pass None to completely disable style parsing.
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
An InlineKeyboardMarkup object.
input_message_content (:obj:`InputMessageContent`):
Content of the message to be sent instead of the photo.
"""
__slots__ = [
"photo_url", "thumb_url", "title", "description", "caption", "parse_mode", "reply_markup",
"input_message_content"
]
def __init__(
self,
photo_url: str,
thumb_url: str = None,
id: str = None,
title: str = None,
description: str = None,
caption: str = None,
parse_mode: Union[str, None] = object,
reply_markup: InlineKeyboardMarkup = None,
input_message_content: InputMessageContent = None
):
super().__init__("photo", id, input_message_content, reply_markup)
self.photo_url = photo_url
self.thumb_url = thumb_url
self.title = title
self.description = description
self.caption = caption
self.parse_mode = parse_mode
self.reply_markup = reply_markup
self.input_message_content = input_message_content
def write(self):
photo = types.InputWebDocument(
url=self.photo_url,
size=0,
mime_type="image/jpeg",
attributes=[]
)
if self.thumb_url is None:
thumb = photo
else:
thumb = types.InputWebDocument(
url=self.thumb_url,
size=0,
mime_type="image/jpeg",
attributes=[]
)
return types.InputBotInlineResult(
id=self.id,
type=self.type,
title=self.title,
description=self.description,
thumb=thumb,
content=photo,
send_message=(
self.input_message_content.write(self.reply_markup)
if self.input_message_content
else types.InputBotInlineMessageMediaAuto(
reply_markup=self.reply_markup.write() if self.reply_markup else None,
**(Parser(None)).parse(self.caption, self.parse_mode)
)
)
)