From 8e36c0002bae6035103b2c4ae0cfc62139cf486e Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 25 Feb 2019 18:23:39 +0100 Subject: [PATCH] Fix sending albums with bot file IDs --- telethon/client/uploads.py | 19 +++++++++++-------- telethon/utils.py | 6 +++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index c95377f3..cbe3e8da 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -293,22 +293,25 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): # Need to upload the media first, but only if they're not cached yet media = [] for file in files: - # fh will either be InputPhoto or a modified InputFile - fh = await self.upload_file(file, use_cache=types.InputPhoto) - if not isinstance(fh, types.InputPhoto): + # Albums want :tl:`InputMedia` which, in theory, includes + # :tl:`InputMediaUploadedPhoto`. However using that will + # 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) + if isinstance(fm, types.InputMediaUploadedPhoto): r = await self(functions.messages.UploadMediaRequest( - entity, media=types.InputMediaUploadedPhoto(fh) + entity, media=fm )) - input_photo = utils.get_input_photo(r.photo) - self.session.cache_file(fh.md5, fh.size, input_photo) - fh = input_photo + fm = utils.get_input_photo(r.photo) + self.session.cache_file(fh.md5, fh.size, fm) + fm = types.InputMediaPhoto(fm) if captions: caption, msg_entities = captions.pop() else: caption, msg_entities = '', None media.append(types.InputSingleMedia( - types.InputMediaPhoto(fh), + fm, message=caption, entities=msg_entities )) diff --git a/telethon/utils.py b/telethon/utils.py index b0a5b1e5..305e3e39 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -662,7 +662,11 @@ def is_image(file): """ Returns ``True`` if the file extension looks like an image file to Telegram. """ - return re.match(r'\.(png|jpe?g)', _get_extension(file), re.IGNORECASE) + match = re.match(r'\.(png|jpe?g)', _get_extension(file), re.IGNORECASE) + if match: + return True + else: + return isinstance(resolve_bot_file_id(file), types.Photo) def is_gif(file):