diff --git a/pyrogram/types/inline_mode/__init__.py b/pyrogram/types/inline_mode/__init__.py index c1f5b87f87..5a090fc770 100644 --- a/pyrogram/types/inline_mode/__init__.py +++ b/pyrogram/types/inline_mode/__init__.py @@ -24,8 +24,12 @@ from .inline_query_result_audio import InlineQueryResultAudio from .inline_query_result_photo import InlineQueryResultPhoto from .inline_query_result_video import InlineQueryResultVideo +from .inline_query_result_cached_photo import InlineQueryResultCachedPhoto +from .inline_query_result_cached_document import InlineQueryResultCachedDocument + __all__ = [ "InlineQuery", "InlineQueryResult", "InlineQueryResultArticle", "InlineQueryResultPhoto", - "InlineQueryResultAnimation", "InlineQueryResultAudio", "InlineQueryResultVideo", "ChosenInlineResult" + "InlineQueryResultAnimation", "InlineQueryResultCachedPhoto", "InlineQueryResultCachedDocument", + "ChosenInlineResult", ] diff --git a/pyrogram/types/inline_mode/inline_query_result_cached_document.py b/pyrogram/types/inline_mode/inline_query_result_cached_document.py new file mode 100644 index 0000000000..342b8a86c1 --- /dev/null +++ b/pyrogram/types/inline_mode/inline_query_result_cached_document.py @@ -0,0 +1,102 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2020 Dan +# +# 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 . + +from typing import Union + +import pyrogram +from pyrogram import raw +from pyrogram import utils +from pyrogram import types +from pyrogram.parser import Parser +from .inline_query_result import InlineQueryResult + + +class InlineQueryResultCachedDocument(InlineQueryResult): + """Link to a file stored on the Telegram servers. + By default, this 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 file. + Parameters: + title (``str``): + Title for the result. + + file_id (``str``): + Pass a file_id as string to send a media that exists on the Telegram servers. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + id (``str``, *optional*): + Unique identifier for this result, 1-64 bytes. + Defaults to a randomly generated UUID4. + 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:`~pyrogram.types.InlineKeyboardMarkup`, *optional*): + Inline keyboard attached to the message. + input_message_content (:obj:`~pyrogram.types.InputMessageContent`): + Content of the message to be sent. + """ + + def __init__( + self, + title: str, + file_id: str, + file_ref: str = None, + id: str = None, + description: str = None, + caption: str = "", + parse_mode: Union[str, None] = object, + reply_markup: "types.InlineKeyboardMarkup" = None, + input_message_content: "types.InputMessageContent" = None + ): + super().__init__("file", id, input_message_content, reply_markup) + + self.file_id = file_id + self.file_ref = file_ref + 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 + + async def write(self, client: "pyrogram.Client"): + document = utils.get_input_file_from_file_id(self.file_id, self.file_ref) + + return raw.types.InputBotInlineResultDocument( + id=self.id, + type=self.type, + title=self.title, + description=self.description, + document=document, + send_message=( + await self.input_message_content.write(client, self.reply_markup) + if self.input_message_content + else raw.types.InputBotInlineMessageMediaAuto( + reply_markup=await self.reply_markup.write(client) if self.reply_markup else None, + **await(Parser(None)).parse(self.caption, self.parse_mode) + ) + ) + ) diff --git a/pyrogram/types/inline_mode/inline_query_result_cached_photo.py b/pyrogram/types/inline_mode/inline_query_result_cached_photo.py new file mode 100644 index 0000000000..3b7aa2a3c0 --- /dev/null +++ b/pyrogram/types/inline_mode/inline_query_result_cached_photo.py @@ -0,0 +1,91 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2020 Dan +# +# 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 . + +from typing import Union + +import pyrogram +from pyrogram import raw +from pyrogram import utils +from pyrogram import types +from pyrogram.parser import Parser +from .inline_query_result import InlineQueryResult + + +class InlineQueryResultCachedPhoto(InlineQueryResult): + """Link to a photo stored on the Telegram servers. + By default, this photo 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 photo. + Parameters: + file_id (``str``): + Pass a file_id as string to send a media that exists on the Telegram servers. + file_ref (``str``, *optional*): + A valid file reference obtained by a recently fetched media message. + To be used in combination with a file id in case a file reference is needed. + id (``str``, *optional*): + Unique identifier for this result, 1-64 bytes. + Defaults to a randomly generated UUID4. + 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:`~pyrogram.types.InlineKeyboardMarkup`, *optional*): + Inline keyboard attached to the message. + input_message_content (:obj:`~pyrogram.types.InputMessageContent`): + Content of the message to be sent. + """ + + def __init__( + self, + file_id: str, + file_ref: str = None, + id: str = None, + caption: str = "", + parse_mode: Union[str, None] = object, + reply_markup: "types.InlineKeyboardMarkup" = None, + input_message_content: "types.InputMessageContent" = None + ): + super().__init__("photo", id, input_message_content, reply_markup) + + self.file_id = file_id + self.file_ref = file_ref + self.caption = caption + self.parse_mode = parse_mode + self.reply_markup = reply_markup + self.input_message_content = input_message_content + + async def write(self, client: "pyrogram.Client"): + photo = utils.get_input_file_from_file_id(self.file_id, self.file_ref) + + return raw.types.InputBotInlineResultPhoto( + id=self.id, + type=self.type, + photo=photo, + send_message=( + await self.input_message_content.write(client, self.reply_markup) + if self.input_message_content + else raw.types.InputBotInlineMessageMediaAuto( + reply_markup=await self.reply_markup.write(client) if self.reply_markup else None, + **await(Parser(None)).parse(self.caption, self.parse_mode) + ) + ) + ) diff --git a/pyrogram/utils.py b/pyrogram/utils.py index 6a0c8bac91..24d26a43ad 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -78,6 +78,40 @@ def get_input_media_from_file_id( raise ValueError(f"Unknown file id: {file_id}") +def get_input_file_from_file_id( + file_id: str, + expected_file_type: FileType = None +) -> Union["raw.types.InputPhoto", "raw.types.InputDocument"]: + try: + decoded = FileId.decode(file_id) + except Exception: + raise ValueError(f'Failed to decode "{file_id}". The value does not represent an existing local file, ' + f'HTTP URL, or valid file id.') + + file_type = decoded.file_type + + if expected_file_type is not None and file_type != expected_file_type: + raise ValueError(f'Expected: "{expected_file_type}", got "{file_type}" file_id instead') + + if file_type in (FileType.THUMBNAIL, FileType.CHAT_PHOTO): + raise ValueError(f"This file_id can only be used for download: {file_id}") + + if file_type in PHOTO_TYPES: + return raw.types.InputPhoto( + id=decoded.media_id, + access_hash=decoded.access_hash, + file_reference=decoded.file_reference + ) + + if file_type in DOCUMENT_TYPES: + return raw.types.InputDocument( + id=decoded.media_id, + access_hash=decoded.access_hash, + file_reference=decoded.file_reference + ) + + raise ValueError(f"Unknown file id: {file_id}") + async def parse_messages(client, messages: "raw.types.messages.Messages", replies: int = 1) -> List["types.Message"]: users = {i.id: i for i in messages.users} chats = {i.id: i for i in messages.chats}