Update send_file to support grouping any file type (#1620)

This commit is contained in:
Richard 2020-11-07 17:32:00 +01:00 committed by GitHub
parent e1d2c81dca
commit ba3a090a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -308,41 +308,26 @@ class UploadMethods:
caption = ''
# First check if the user passed an iterable, in which case
# we may want to send as an album if all are photo files.
# we may want to send grouped.
if utils.is_list_like(file):
media_captions = []
document_captions = []
if utils.is_list_like(caption):
captions = caption
else:
captions = [caption]
# TODO Fix progress_callback
media = []
if force_document:
documents = file
else:
documents = []
for doc, cap in itertools.zip_longest(file, captions):
if utils.is_image(doc) or utils.is_video(doc):
media.append(doc)
media_captions.append(cap)
else:
documents.append(doc)
document_captions.append(cap)
result = []
while media:
while file:
result += await self._send_album(
entity, media[:10], caption=media_captions[:10],
entity, file[:10], caption=captions[:10],
progress_callback=progress_callback, reply_to=reply_to,
parse_mode=parse_mode, silent=silent, schedule=schedule,
supports_streaming=supports_streaming, clear_draft=clear_draft
supports_streaming=supports_streaming, clear_draft=clear_draft,
force_document=force_document
)
media = media[10:]
media_captions = media_captions[10:]
file = file[10:]
captions = captions[10:]
for doc, cap in zip(documents, captions):
for doc, cap in zip(file, captions):
result.append(await self.send_file(
entity, doc, allow_cache=allow_cache,
caption=cap, force_document=force_document,
@ -389,7 +374,8 @@ class UploadMethods:
async def _send_album(self: 'TelegramClient', entity, files, caption='',
progress_callback=None, reply_to=None,
parse_mode=(), silent=None, schedule=None,
supports_streaming=None, clear_draft=None):
supports_streaming=None, clear_draft=None,
force_document=False):
"""Specialized version of .send_file for albums"""
# We don't care if the user wants to avoid cache, we will use it
# anyway. Why? The cached version will be exactly the same thing
@ -418,7 +404,8 @@ class UploadMethods:
# make it `raise MediaInvalidError`, so we need to upload
# it as media and then convert that to :tl:`InputMediaPhoto`.
fh, fm, _ = await self._file_to_media(
file, supports_streaming=supports_streaming)
file, supports_streaming=supports_streaming,
force_document=force_document)
if isinstance(fm, types.InputMediaUploadedPhoto):
r = await self(functions.messages.UploadMediaRequest(
entity, media=fm