2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Enhance send_media_group by accepting file_ids

This commit is contained in:
Dan 2018-04-14 21:48:15 +02:00
parent 5e26ae1894
commit b705391f79
3 changed files with 93 additions and 48 deletions

View File

@ -52,7 +52,8 @@ from pyrogram.session.internals import MsgId
from . import message_parser from . import message_parser
from . import utils from . import utils
from .dispatcher import Dispatcher from .dispatcher import Dispatcher
from .input_media import InputMedia from .input_media_photo import InputMediaPhoto
from .input_media_video import InputMediaVideo
from .style import Markdown, HTML from .style import Markdown, HTML
from .syncer import Syncer from .syncer import Syncer
@ -1980,6 +1981,8 @@ class Client:
return message_parser.parse_message(self, i.message, users, chats) return message_parser.parse_message(self, i.message, users, chats)
# TODO: Add progress parameter # TODO: Add progress parameter
# TODO: Return new Message object
# TODO: Figure out how to send albums using URLs
def send_media_group(self, def send_media_group(self,
chat_id: int or str, chat_id: int or str,
media: list, media: list,
@ -1996,7 +1999,7 @@ class Client:
For a private channel/supergroup you can use its *t.me/joinchat/* link. For a private channel/supergroup you can use its *t.me/joinchat/* link.
media (``list``): media (``list``):
A list containing either :obj:`pyrogram.InputMedia.Photo` or :obj:`pyrogram.InputMedia.Video` objects A list containing either :obj:`pyrogram.InputMediaPhoto` or :obj:`pyrogram.InputMediaVideo` objects
describing photos and videos to be sent, must include 210 items. describing photos and videos to be sent, must include 210 items.
disable_notification (``bool``, optional): disable_notification (``bool``, optional):
@ -2009,66 +2012,104 @@ class Client:
multi_media = [] multi_media = []
for i in media: for i in media:
if isinstance(i, InputMedia.Photo): style = self.html if i.parse_mode.lower() == "html" else self.markdown
style = self.html if i.parse_mode.lower() == "html" else self.markdown
media = self.save_file(i.media)
media = self.send( if isinstance(i, InputMediaPhoto):
functions.messages.UploadMedia( if os.path.exists(i.media):
peer=self.resolve_peer(chat_id), media = self.send(
media=types.InputMediaUploadedPhoto( functions.messages.UploadMedia(
file=media peer=self.resolve_peer(chat_id),
media=types.InputMediaUploadedPhoto(
file=self.save_file(i.media)
)
) )
) )
)
single_media = types.InputSingleMedia( media = types.InputMediaPhoto(
media=types.InputMediaPhoto(
id=types.InputPhoto( id=types.InputPhoto(
id=media.photo.id, id=media.photo.id,
access_hash=media.photo.access_hash access_hash=media.photo.access_hash
) )
), )
random_id=self.rnd_id(), else:
**style.parse(i.caption) try:
) decoded = utils.decode(i.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] != 2:
media_type = Client.MEDIA_TYPE_ID.get(unpacked[0], None)
multi_media.append(single_media) if media_type:
elif isinstance(i, InputMedia.Video): raise FileIdInvalid("The file_id belongs to a {}".format(media_type))
style = self.html if i.parse_mode.lower() == "html" else self.markdown else:
media = self.save_file(i.media) raise FileIdInvalid("Unknown media type: {}".format(unpacked[0]))
media = self.send( media = types.InputMediaPhoto(
functions.messages.UploadMedia( id=types.InputPhoto(
peer=self.resolve_peer(chat_id), id=unpacked[2],
media=types.InputMediaUploadedDocument( access_hash=unpacked[3]
file=media, )
mime_type=mimetypes.types_map[".mp4"], )
attributes=[ elif isinstance(i, InputMediaVideo):
types.DocumentAttributeVideo( if os.path.exists(i.media):
supports_streaming=i.supports_streaming or None, media = self.send(
duration=i.duration, functions.messages.UploadMedia(
w=i.width, peer=self.resolve_peer(chat_id),
h=i.height media=types.InputMediaUploadedDocument(
), file=self.save_file(i.media),
types.DocumentAttributeFilename(os.path.basename(i.media)) mime_type=mimetypes.types_map[".mp4"],
] attributes=[
types.DocumentAttributeVideo(
supports_streaming=i.supports_streaming or None,
duration=i.duration,
w=i.width,
h=i.height
),
types.DocumentAttributeFilename(os.path.basename(i.media))
]
)
) )
) )
)
single_media = types.InputSingleMedia( media = types.InputMediaDocument(
media=types.InputMediaDocument(
id=types.InputDocument( id=types.InputDocument(
id=media.document.id, id=media.document.id,
access_hash=media.document.access_hash access_hash=media.document.access_hash
) )
), )
else:
try:
decoded = utils.decode(i.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] != 4:
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]
)
)
multi_media.append(
types.InputSingleMedia(
media=media,
random_id=self.rnd_id(), random_id=self.rnd_id(),
**style.parse(i.caption) **style.parse(i.caption)
) )
)
multi_media.append(single_media)
return self.send( return self.send(
functions.messages.SendMultiMedia( functions.messages.SendMultiMedia(

View File

@ -23,13 +23,15 @@ class InputMediaPhoto:
Args: Args:
media (:obj:`str`): media (:obj:`str`):
Photo file to send. Photo to send.
Pass a file path as string to send a photo that exists on your local machine. Pass a file_id as string to send a photo that exists on the Telegram servers or
pass a file path as string to upload a new photo that exists on your local machine.
Sending photo by a URL is currently unsupported.
caption (:obj:`str`): caption (:obj:`str`, optional):
Caption of the photo to be sent, 0-200 characters Caption of the photo to be sent, 0-200 characters
parse_mode (:obj:`str`): parse_mode (:obj:`str`, optional):
Use :obj:`MARKDOWN <pyrogram.ParseMode.MARKDOWN>` or :obj:`HTML <pyrogram.ParseMode.HTML>` Use :obj:`MARKDOWN <pyrogram.ParseMode.MARKDOWN>` or :obj:`HTML <pyrogram.ParseMode.HTML>`
if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your caption. if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your caption.
Defaults to Markdown. Defaults to Markdown.

View File

@ -23,8 +23,10 @@ class InputMediaVideo:
Args: Args:
media (:obj:`str`): media (:obj:`str`):
Video file to send. Video to send.
Pass a file path as string to send a video that exists on your local machine. Pass a file_id as string to send a video that exists on the Telegram servers or
pass a file path as string to upload a new video that exists on your local machine.
Sending video by a URL is currently unsupported.
caption (:obj:`str`, optional): caption (:obj:`str`, optional):
Caption of the video to be sent, 0-200 characters Caption of the video to be sent, 0-200 characters