Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit b4bdab1

Browse files
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>
1 parent 0c46b65 commit b4bdab1

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

pyrogram/types/inline_mode/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
from .inline_query_result_animation import InlineQueryResultAnimation
2323
from .inline_query_result_article import InlineQueryResultArticle
2424
from .inline_query_result_photo import InlineQueryResultPhoto
25+
from .inline_query_result_audio import InlineQueryResultAudio
2526

2627
__all__ = [
2728
"InlineQuery", "InlineQueryResult", "InlineQueryResultArticle", "InlineQueryResultPhoto",
28-
"InlineQueryResultAnimation", "ChosenInlineResult"
29+
"InlineQueryResultAnimation", "InlineQueryResultAudio", "ChosenInlineResult"
2930
]
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2020 Dan <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 import raw
22+
from pyrogram import types
23+
from pyrogram.parser import Parser
24+
from pyrogram.types import InlineQueryResult
25+
26+
27+
class InlineQueryResultAudio(InlineQueryResult):
28+
"""Link to an audio file.
29+
30+
By default, this audio file will be sent by the user with optional caption.
31+
Alternatively, you can use *input_message_content* to send a message with the specified content instead of the
32+
audio.
33+
34+
Parameters:
35+
audio_url (``str``):
36+
A valid URL for the audio file.
37+
38+
title (``str``):
39+
Title for the result.
40+
41+
id (``str``, *optional*):
42+
Unique identifier for this result, 1-64 bytes.
43+
Defaults to a randomly generated UUID4.
44+
45+
performer (``str``, *optional*):
46+
Audio performer.
47+
48+
audio_duration (``int``, *optional*):
49+
Audio duration in seconds.
50+
51+
caption (``str``, *optional*):
52+
Caption of the photo to be sent, 0-1024 characters.
53+
54+
parse_mode (``str``, *optional*):
55+
By default, texts are parsed using both Markdown and HTML styles.
56+
You can combine both syntaxes together.
57+
Pass "markdown" or "md" to enable Markdown-style parsing only.
58+
Pass "html" to enable HTML-style parsing only.
59+
Pass None to completely disable style parsing.
60+
61+
caption_entities (List of :obj:`~pyrogram.types.MessageEntity`):
62+
List of special entities that appear in the caption, which can be specified instead of *parse_mode*.
63+
64+
reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*):
65+
Inline keyboard attached to the message.
66+
67+
input_message_content (:obj:`~pyrogram.types.InputMessageContent`, *optional*):
68+
Content of the message to be sent instead of the photo.
69+
"""
70+
71+
def __init__(
72+
self,
73+
audio_url: str,
74+
title: str,
75+
id: str = None,
76+
performer: str = "",
77+
audio_duration: int = 0,
78+
caption: str = "",
79+
parse_mode: Union[str, None] = object,
80+
caption_entities: List["types.MessageEntity"] = None,
81+
reply_markup: "types.InlineKeyboardMarkup" = None,
82+
input_message_content: "types.InputMessageContent" = None
83+
):
84+
super().__init__("audio", id, input_message_content, reply_markup)
85+
86+
self.audio_url = audio_url
87+
self.title = title
88+
self.performer = performer
89+
self.audio_duration = audio_duration
90+
self.caption = caption
91+
self.parse_mode = parse_mode
92+
self.caption_entities = caption_entities
93+
94+
async def write(self):
95+
audio = raw.types.InputWebDocument(
96+
url=self.audio_url,
97+
size=0,
98+
mime_type="audio/mpeg",
99+
attributes=[raw.types.DocumentAttributeAudio(
100+
duration=self.audio_duration,
101+
title=self.title,
102+
performer=self.performer
103+
)],
104+
)
105+
106+
message, entities = (await utils.parse_text_entities(
107+
client, self.caption, self.parse_mode, self.caption_entities
108+
)).values()
109+
110+
return raw.types.InputBotInlineResult(
111+
id=self.id,
112+
type=self.type,
113+
title=self.title,
114+
content=audio,
115+
send_message=(
116+
await self.input_message_content.write(client, self.reply_markup)
117+
if self.input_message_content
118+
else raw.types.InputBotInlineMessageMediaAuto(
119+
reply_markup=await self.reply_markup.write(client) if self.reply_markup else None,
120+
message=message,
121+
entities=entities
122+
)
123+
)
124+
)

0 commit comments

Comments
 (0)