From 0c5085022ceaa854baf267b9dd82e0010ec7e4c8 Mon Sep 17 00:00:00 2001 From: Mehdi Date: Wed, 11 Aug 2021 08:34:47 +0200 Subject: [PATCH] Fix Setting Thumbs When Uploading A Single File (#2583) * Update request.py If the media has a thumb, we also need to attach it to the data. * Add test * Editing syntax * Debug test * update request.py * Update test_inputmedia.py * Update test_inputmedia.py * Update test_inputmedia.py Fix test. * Update AUTHORS.rst Adding my name! * Update AUTHORS.rst --- AUTHORS.rst | 1 + telegram/utils/request.py | 23 ++++++++++++++--------- tests/test_inputmedia.py | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index bc59f500c..9879a302b 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -84,6 +84,7 @@ The following wonderful people contributed directly or indirectly to this projec - `Oleg Sushchenko `_ - `Or Bin `_ - `overquota `_ +- `Paradox `_ - `Patrick Hofmann `_ - `Paul Larsen `_ - `Pieter Schutz `_ diff --git a/telegram/utils/request.py b/telegram/utils/request.py index f2c35bfdf..7362be590 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -58,7 +58,7 @@ except ImportError: # pragma: no cover raise # pylint: disable=C0412 -from telegram import InputFile, InputMedia, TelegramError +from telegram import InputFile, TelegramError from telegram.error import ( BadRequest, ChatMigrated, @@ -325,13 +325,9 @@ class Request: # Urllib3 doesn't like floats it seems data[key] = str(val) elif key == 'media': - # One media or multiple - if isinstance(val, InputMedia): - # Attach and set val to attached name - data[key] = val.to_json() - if isinstance(val.media, InputFile): # type: ignore - data[val.media.attach] = val.media.field_tuple # type: ignore - else: + files = True + # List of media + if isinstance(val, list): # Attach and set val to attached name for all media = [] for med in val: @@ -343,7 +339,16 @@ class Request: if "thumb" in media_dict: data[med.thumb.attach] = med.thumb.field_tuple data[key] = json.dumps(media) - files = True + # Single media + else: + # Attach and set val to attached name + media_dict = val.to_dict() + if isinstance(val.media, InputFile): + data[val.media.attach] = val.media.field_tuple + # if the file has a thumb, we also need to attach it to the data + if "thumb" in media_dict: + data[val.thumb.attach] = val.thumb.field_tuple + data[key] = json.dumps(media_dict) elif isinstance(val, list): # In case we're sending files, we need to json-dump lists manually # As we can't know if that's the case, we just json-dump here diff --git a/tests/test_inputmedia.py b/tests/test_inputmedia.py index 5f0d10b65..a23d96987 100644 --- a/tests/test_inputmedia.py +++ b/tests/test_inputmedia.py @@ -591,6 +591,21 @@ class TestSendMediaGroup: ) assert isinstance(new_message, Message) + def test_edit_message_media_with_thumb( + self, bot, chat_id, video_file, photo_file, monkeypatch # noqa: F811 + ): + def test(*args, **kwargs): + data = kwargs['fields'] + video_check = data[input_video.media.attach] == input_video.media.field_tuple + thumb_check = data[input_video.thumb.attach] == input_video.thumb.field_tuple + result = video_check and thumb_check + raise Exception(f"Test was {'successful' if result else 'failing'}") + + monkeypatch.setattr('telegram.utils.request.Request._request_wrapper', test) + input_video = InputMediaVideo(video_file, thumb=photo_file) + with pytest.raises(Exception, match='Test was successful'): + bot.edit_message_media(chat_id=chat_id, message_id=123, media=input_video) + @flaky(3, 1) @pytest.mark.parametrize( 'default_bot', [{'parse_mode': ParseMode.HTML}], indirect=True, ids=['HTML-Bot']