Fix caption when using send_file for albums / mixed documents

This commit is contained in:
Lonami Exo 2019-06-15 16:59:16 +02:00
parent fd37e44854
commit 1a056899d7

View File

@ -1,5 +1,6 @@
import hashlib import hashlib
import io import io
import itertools
import os import os
import pathlib import pathlib
import re import re
@ -90,9 +91,9 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods):
async def send_file( async def send_file(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
file: 'hints.FileLike', file: 'typing.Union[hints.FileLike, typing.Sequence[hints.FileLike]]',
*, *,
caption: str = None, caption: typing.Union[str, typing.Sequence[str]] = None,
force_document: bool = False, force_document: bool = False,
progress_callback: 'hints.ProgressCallback' = None, progress_callback: 'hints.ProgressCallback' = None,
reply_to: 'hints.MessageIDLike' = None, reply_to: 'hints.MessageIDLike' = None,
@ -269,31 +270,41 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods):
# First check if the user passed an iterable, in which case # 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 as an album if all are photo files.
if utils.is_list_like(file): if utils.is_list_like(file):
image_captions = []
document_captions = []
if utils.is_list_like(caption):
captions = caption
else:
captions = [caption]
# TODO Fix progress_callback # TODO Fix progress_callback
images = [] images = []
if force_document: if force_document:
documents = file documents = file
else: else:
documents = [] documents = []
for x in file: for doc, cap in itertools.zip_longest(file, captions):
if utils.is_image(x): if utils.is_image(doc):
images.append(x) images.append(doc)
image_captions.append(cap)
else: else:
documents.append(x) documents.append(doc)
document_captions.append(cap)
result = [] result = []
while images: while images:
result += await self._send_album( result += await self._send_album(
entity, images[:10], caption=caption, entity, images[:10], caption=image_captions[:10],
progress_callback=progress_callback, reply_to=reply_to, progress_callback=progress_callback, reply_to=reply_to,
parse_mode=parse_mode, silent=silent parse_mode=parse_mode, silent=silent
) )
images = images[10:] images = images[10:]
image_captions = image_captions[10:]
for x in documents: for doc, cap in zip(documents, captions):
result.append(await self.send_file( result.append(await self.send_file(
entity, x, allow_cache=allow_cache, entity, doc, allow_cache=allow_cache,
caption=caption, force_document=force_document, caption=cap, force_document=force_document,
progress_callback=progress_callback, reply_to=reply_to, progress_callback=progress_callback, reply_to=reply_to,
attributes=attributes, thumb=thumb, voice_note=voice_note, attributes=attributes, thumb=thumb, voice_note=voice_note,
video_note=video_note, buttons=buttons, silent=silent, video_note=video_note, buttons=buttons, silent=silent,