Enhance send_voice by accepting file_ids and URLs
This commit is contained in:
parent
1fdb3386ff
commit
c52ecadb76
@ -1762,7 +1762,9 @@ class Client:
|
||||
|
||||
voice (``str``):
|
||||
Audio file to send.
|
||||
Pass a file path as string to send an audio file that exists on your local machine.
|
||||
Pass a file_id as string to send an audio that exists on the Telegram servers,
|
||||
pass an HTTP URL as a string for Telegram to get an audio from the Internet, or
|
||||
pass a file path as string to upload a new audio that exists on your local machine.
|
||||
|
||||
caption (``str``, optional):
|
||||
Voice message caption, 0-200 characters.
|
||||
@ -1794,20 +1796,17 @@ class Client:
|
||||
The size of the file.
|
||||
|
||||
Returns:
|
||||
On success, the sent Message is returned.
|
||||
On success, the sent :obj:`Message <pyrogram.Message>` is returned.
|
||||
|
||||
Raises:
|
||||
:class:`Error <pyrogram.Error>`
|
||||
"""
|
||||
file = None
|
||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
||||
file = self.save_file(voice, progress=progress)
|
||||
|
||||
while True:
|
||||
try:
|
||||
r = self.send(
|
||||
functions.messages.SendMedia(
|
||||
peer=self.resolve_peer(chat_id),
|
||||
media=types.InputMediaUploadedDocument(
|
||||
if os.path.exists(voice):
|
||||
file = self.save_file(voice, progress=progress)
|
||||
media = types.InputMediaUploadedDocument(
|
||||
mime_type=mimetypes.types_map.get("." + voice.split(".")[-1], "audio/mpeg"),
|
||||
file=file,
|
||||
attributes=[
|
||||
@ -1816,7 +1815,40 @@ class Client:
|
||||
duration=duration
|
||||
)
|
||||
]
|
||||
),
|
||||
)
|
||||
elif voice.startswith("http"):
|
||||
media = types.InputMediaDocumentExternal(
|
||||
url=voice
|
||||
)
|
||||
else:
|
||||
try:
|
||||
decoded = utils.decode(voice)
|
||||
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] != 3:
|
||||
media_type = Client.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]))
|
||||
|
||||
media = types.InputMediaDocument(
|
||||
id=types.InputDocument(
|
||||
id=unpacked[2],
|
||||
access_hash=unpacked[3]
|
||||
)
|
||||
)
|
||||
|
||||
while True:
|
||||
try:
|
||||
r = self.send(
|
||||
functions.messages.SendMedia(
|
||||
peer=self.resolve_peer(chat_id),
|
||||
media=media,
|
||||
silent=disable_notification or None,
|
||||
reply_to_msg_id=reply_to_message_id,
|
||||
random_id=self.rnd_id(),
|
||||
@ -1826,7 +1858,12 @@ class Client:
|
||||
except FilePartMissing as e:
|
||||
self.save_file(voice, file_id=file.id, file_part=e.x)
|
||||
else:
|
||||
return r
|
||||
for i in r.updates:
|
||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||
users = {i.id: i for i in r.users}
|
||||
chats = {i.id: i for i in r.chats}
|
||||
|
||||
return message_parser.parse_message(self, i.message, users, chats)
|
||||
|
||||
def send_video_note(self,
|
||||
chat_id: int or str,
|
||||
|
Loading…
Reference in New Issue
Block a user