Add InlineQueryResultAudio (#539)

* Added audio support for inline query

* mime-type removed

* Update inline_query_result_audio.py

Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com>
This commit is contained in:
Alan 2021-05-12 11:47:41 +05:00 committed by GitHub
parent 0c46b653ec
commit b4bdab1047
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 126 additions and 1 deletions

View File

@ -22,8 +22,9 @@ from .inline_query_result import InlineQueryResult
from .inline_query_result_animation import InlineQueryResultAnimation
from .inline_query_result_article import InlineQueryResultArticle
from .inline_query_result_photo import InlineQueryResultPhoto
from .inline_query_result_audio import InlineQueryResultAudio
__all__ = [
"InlineQuery", "InlineQueryResult", "InlineQueryResultArticle", "InlineQueryResultPhoto",
"InlineQueryResultAnimation", "ChosenInlineResult"
"InlineQueryResultAnimation", "InlineQueryResultAudio", "ChosenInlineResult"
]

View File

@ -0,0 +1,124 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2020 Dan <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 import raw
from pyrogram import types
from pyrogram.parser import Parser
from pyrogram.types import InlineQueryResult
class InlineQueryResultAudio(InlineQueryResult):
"""Link to an audio file.
By default, this audio 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
audio.
Parameters:
audio_url (``str``):
A valid URL for the audio file.
title (``str``):
Title for the result.
id (``str``, *optional*):
Unique identifier for this result, 1-64 bytes.
Defaults to a randomly generated UUID4.
performer (``str``, *optional*):
Audio performer.
audio_duration (``int``, *optional*):
Audio duration in seconds.
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.
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 photo.
"""
def __init__(
self,
audio_url: str,
title: str,
id: str = None,
performer: str = "",
audio_duration: int = 0,
caption: str = "",
parse_mode: Union[str, None] = object,
caption_entities: List["types.MessageEntity"] = None,
reply_markup: "types.InlineKeyboardMarkup" = None,
input_message_content: "types.InputMessageContent" = None
):
super().__init__("audio", id, input_message_content, reply_markup)
self.audio_url = audio_url
self.title = title
self.performer = performer
self.audio_duration = audio_duration
self.caption = caption
self.parse_mode = parse_mode
self.caption_entities = caption_entities
async def write(self):
audio = raw.types.InputWebDocument(
url=self.audio_url,
size=0,
mime_type="audio/mpeg",
attributes=[raw.types.DocumentAttributeAudio(
duration=self.audio_duration,
title=self.title,
performer=self.performer
)],
)
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
)
)
)