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
This commit is contained in:
Mehdi 2021-08-11 08:34:47 +02:00 committed by GitHub
parent 1fdaaac809
commit 0c5085022c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 9 deletions

View file

@ -84,6 +84,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Oleg Sushchenko <https://github.com/feuillemorte>`_
- `Or Bin <https://github.com/OrBin>`_
- `overquota <https://github.com/overquota>`_
- `Paradox <https://github.com/paradox70>`_
- `Patrick Hofmann <https://github.com/PH89>`_
- `Paul Larsen <https://github.com/PaulSonOfLars>`_
- `Pieter Schutz <https://github.com/eldinnie>`_

View file

@ -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

View file

@ -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']