mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Add support for editing messages with Audio
This commit is contained in:
parent
9e5976e755
commit
f7c2dc9d30
@ -25,7 +25,7 @@ from pyrogram.api import functions, types
|
|||||||
from pyrogram.api.errors import FileIdInvalid
|
from pyrogram.api.errors import FileIdInvalid
|
||||||
from pyrogram.client.ext import BaseClient, utils
|
from pyrogram.client.ext import BaseClient, utils
|
||||||
from pyrogram.client.types import (
|
from pyrogram.client.types import (
|
||||||
InputMediaPhoto, InputMediaVideo
|
InputMediaPhoto, InputMediaVideo, InputMediaAudio
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -136,6 +136,59 @@ class EditMessageMedia(BaseClient):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if isinstance(media, InputMediaAudio):
|
||||||
|
if os.path.exists(media.media):
|
||||||
|
media = self.send(
|
||||||
|
functions.messages.UploadMedia(
|
||||||
|
peer=self.resolve_peer(chat_id),
|
||||||
|
media=types.InputMediaUploadedDocument(
|
||||||
|
mime_type=mimetypes.types_map.get("." + media.media.split(".")[-1], "audio/mpeg"),
|
||||||
|
file=self.save_file(media.media),
|
||||||
|
attributes=[
|
||||||
|
types.DocumentAttributeAudio(
|
||||||
|
duration=media.duration,
|
||||||
|
performer=media.performer,
|
||||||
|
title=media.title
|
||||||
|
),
|
||||||
|
types.DocumentAttributeFilename(os.path.basename(media.media))
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
media = types.InputMediaDocument(
|
||||||
|
id=types.InputDocument(
|
||||||
|
id=media.document.id,
|
||||||
|
access_hash=media.document.access_hash
|
||||||
|
)
|
||||||
|
)
|
||||||
|
elif media.media.startswith("http"):
|
||||||
|
media = types.InputMediaDocumentExternal(
|
||||||
|
url=media.media
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
decoded = utils.decode(media.media)
|
||||||
|
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]))
|
||||||
|
|
||||||
|
media = types.InputMediaDocument(
|
||||||
|
id=types.InputDocument(
|
||||||
|
id=unpacked[2],
|
||||||
|
access_hash=unpacked[3]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
r = self.send(
|
r = self.send(
|
||||||
functions.messages.EditMessage(
|
functions.messages.EditMessage(
|
||||||
peer=self.resolve_peer(chat_id),
|
peer=self.resolve_peer(chat_id),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user