From 9c28ccdf60311d9709a3dfc8fa506b98faec0858 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Apr 2022 11:56:07 +0200 Subject: [PATCH] Add InlineQueryResultVoice --- pyrogram/types/inline_mode/__init__.py | 3 +- .../inline_mode/inline_query_result_voice.py | 114 ++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 pyrogram/types/inline_mode/inline_query_result_voice.py diff --git a/pyrogram/types/inline_mode/__init__.py b/pyrogram/types/inline_mode/__init__.py index 25a568f5..daff0117 100644 --- a/pyrogram/types/inline_mode/__init__.py +++ b/pyrogram/types/inline_mode/__init__.py @@ -26,9 +26,10 @@ from .inline_query_result_photo import InlineQueryResultPhoto from .inline_query_result_video import InlineQueryResultVideo from .inline_query_result_contact import InlineQueryResultContact from .inline_query_result_document import InlineQueryResultDocument +from .inline_query_result_voice import InlineQueryResultVoice __all__ = [ "InlineQuery", "InlineQueryResult", "InlineQueryResultArticle", "InlineQueryResultPhoto", "InlineQueryResultAnimation", "InlineQueryResultAudio", "InlineQueryResultVideo", "ChosenInlineResult", - "InlineQueryResultContact", "InlineQueryResultDocument" + "InlineQueryResultContact", "InlineQueryResultDocument", "InlineQueryResultVoice" ] diff --git a/pyrogram/types/inline_mode/inline_query_result_voice.py b/pyrogram/types/inline_mode/inline_query_result_voice.py new file mode 100644 index 00000000..31b422f8 --- /dev/null +++ b/pyrogram/types/inline_mode/inline_query_result_voice.py @@ -0,0 +1,114 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present 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 List, Optional + +import pyrogram +from pyrogram import raw, types, utils, enums +from .inline_query_result import InlineQueryResult + + +class InlineQueryResultVoice(InlineQueryResult): + """Link to a voice recording in an .OGG container encoded with OPUS. + + By default, this voice recording will be sent by the user. + Alternatively, you can use *input_message_content* to send a message with the specified content instead of the + voice message. + + Parameters: + voice_url (``str``): + A valid URL for the voice recording. + + title (``str``): + Title for the result. + + id (``str``, *optional*): + Unique identifier for this result, 1-64 bytes. + Defaults to a randomly generated UUID4. + + voice_duration (``int``, *optional*): + Recording duration in seconds. + + caption (``str``, *optional*): + Caption of the audio to be sent, 0-1024 characters. + + parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*): + By default, texts are parsed using both Markdown and HTML styles. + You can combine both syntaxes together. + + 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*): + Inline keyboard attached to the message. + + input_message_content (:obj:`~pyrogram.types.InputMessageContent`, *optional*): + Content of the message to be sent instead of the audio. + """ + + def __init__( + self, + voice_url: str, + title: str, + id: str = None, + voice_duration: int = 0, + caption: str = "", + parse_mode: Optional["enums.ParseMode"] = None, + caption_entities: List["types.MessageEntity"] = None, + reply_markup: "types.InlineKeyboardMarkup" = None, + input_message_content: "types.InputMessageContent" = None + ): + super().__init__("voice", id, input_message_content, reply_markup) + + self.voice_url = voice_url + self.title = title + self.voice_duration = voice_duration + self.caption = caption + self.parse_mode = parse_mode + self.caption_entities = caption_entities + + async def write(self, client: "pyrogram.Client"): + audio = raw.types.InputWebDocument( + url=self.voice_url, + size=0, + mime_type="audio/mpeg", + attributes=[raw.types.DocumentAttributeAudio( + duration=self.voice_duration, + title=self.title, + )] + ) + + message, entities = (await utils.parse_text_entities( + client, self.caption, self.parse_mode, self.caption_entities + )).values() + + return raw.types.InputBotInlineResult( + id=self.id, + type=self.type, + title=self.title, + content=audio, + 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, + message=message, + entities=entities + ) + ) + )