Implement InlineQueryResultCachedAudio

This commit is contained in:
Dan 2018-10-16 11:34:44 +02:00
parent 797a0df087
commit c94c79edac
3 changed files with 67 additions and 25 deletions

View File

@ -32,7 +32,7 @@ from .client.types import (
Location, Message, MessageEntity, Dialog, Dialogs, Photo, PhotoSize, Sticker, Update, User,
UserProfilePhotos, Venue, Animation, Video, VideoNote, Voice, CallbackQuery, Messages, ForceReply,
InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove,
InlineQuery, InlineQueryResultArticle, InlineQueryResultPhoto, InputTextMessageContent
InlineQuery, InlineQueryResultArticle, InlineQueryResultPhoto, InputTextMessageContent, InlineQueryResultCachedAudio
)
from .client import (
Client, ChatAction, ParseMode, Emoji,

View File

@ -19,7 +19,7 @@
from .bots import (
CallbackQuery, ForceReply, InlineKeyboardButton, InlineKeyboardMarkup,
KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineQuery,
InlineQueryResultArticle, InlineQueryResultPhoto
InlineQueryResultArticle, InlineQueryResultPhoto, InlineQueryResultCachedAudio
)
from .bots import (
ForceReply, InlineKeyboardButton, InlineKeyboardMarkup,

View File

@ -16,45 +16,87 @@
# 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 pyrogram.api.core import Object
import binascii
import struct
from pyrogram.api import types
from pyrogram.api.errors import FileIdInvalid
from pyrogram.client.ext import utils, BaseClient
from pyrogram.client.style import HTML, Markdown
class InlineQueryResultCachedAudio(Object):
"""Represents a link to an mp3 audio file stored on the Telegram servers. By default, this audio file will be sent by the user. Alternatively, you can use input_message_content to send a message with the specified content instead of the audio.
Attributes:
ID: ``0xb0700018``
class InlineQueryResultCachedAudio:
"""Represents a link to an audio file stored on the Telegram servers.
By default, this audio file will be sent by the user. Alternatively, you can use *input_message_content* to send a
message with the specified content instead of the audio.
Args:
type (``str``):
Type of the result, must be audio.
id (``str``):
Unique identifier for this result, 1-64 bytes.
audio_file_id (``str``):
A valid file identifier for the audio file.
caption (``str``, optional):
caption (``str``, *optional*):
Caption, 0-200 characters.
parse_mode (``str``, optional):
Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in the media caption.
parse_mode (``str``, *optional*):
Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in
the media caption.
reply_markup (:obj:`InlineKeyboardMarkup <pyrogram.types.InlineKeyboardMarkup>`, optional):
reply_markup (:obj:`InlineKeyboardMarkup <pyrogram.types.InlineKeyboardMarkup>`, *optional*):
Inline keyboard attached to the message.
input_message_content (:obj:`InputMessageContent <pyrogram.types.InputMessageContent>`, optional):
input_message_content (:obj:`InputMessageContent <pyrogram.types.InputMessageContent>`, *optional*):
Content of the message to be sent instead of the audio.
"""
ID = 0xb0700018
def __init__(self, type: str, id: str, audio_file_id: str, caption: str = None, parse_mode: str = None, reply_markup=None, input_message_content=None):
self.type = type # string
self.id = id # string
self.audio_file_id = audio_file_id # string
self.caption = caption # flags.0?string
self.parse_mode = parse_mode # flags.1?string
self.reply_markup = reply_markup # flags.2?InlineKeyboardMarkup
self.input_message_content = input_message_content # flags.3?InputMessageContent
def __init__(
self,
id: str,
audio_file_id: str,
caption: str = "",
parse_mode: str = "",
reply_markup=None,
input_message_content=None
):
self.id = id
self.audio_file_id = audio_file_id
self.caption = caption
self.parse_mode = parse_mode
self.reply_markup = reply_markup
self.input_message_content = input_message_content
self.style = HTML() if parse_mode.lower() == "html" else Markdown()
def write(self):
try:
decoded = utils.decode(self.audio_file_id)
fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
unpacked = struct.unpack(fmt, decoded)
except (AssertionError, binascii.Error, struct.error):
raise FileIdInvalid from None
else:
if unpacked[0] != 9:
media_type = BaseClient.MEDIA_TYPE_ID.get(unpacked[0], None)
if media_type:
raise FileIdInvalid("The file_id belongs to a {}".format(media_type))
else:
raise FileIdInvalid("Unknown media type: {}".format(unpacked[0]))
audio = types.InputDocument(
id=unpacked[2],
access_hash=unpacked[3]
)
return types.InputBotInlineResultDocument(
id=self.id,
type="audio",
document=audio,
send_message=types.InputBotInlineMessageMediaAuto(
reply_markup=self.reply_markup.write() if self.reply_markup else None,
**self.style.parse(self.caption)
)
)