Skip to content

Commit 0c50850

Browse files
authored
Fix Setting Thumbs When Uploading A Single File (python-telegram-bot#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
1 parent 1fdaaac commit 0c50850

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ The following wonderful people contributed directly or indirectly to this projec
8484
- `Oleg Sushchenko <https://github.com/feuillemorte>`_
8585
- `Or Bin <https://github.com/OrBin>`_
8686
- `overquota <https://github.com/overquota>`_
87+
- `Paradox <https://github.com/paradox70>`_
8788
- `Patrick Hofmann <https://github.com/PH89>`_
8889
- `Paul Larsen <https://github.com/PaulSonOfLars>`_
8990
- `Pieter Schutz <https://github.com/eldinnie>`_

telegram/utils/request.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
raise
5959

6060
# pylint: disable=C0412
61-
from telegram import InputFile, InputMedia, TelegramError
61+
from telegram import InputFile, TelegramError
6262
from telegram.error import (
6363
BadRequest,
6464
ChatMigrated,
@@ -325,13 +325,9 @@ def post(self, url: str, data: JSONDict, timeout: float = None) -> Union[JSONDic
325325
# Urllib3 doesn't like floats it seems
326326
data[key] = str(val)
327327
elif key == 'media':
328-
# One media or multiple
329-
if isinstance(val, InputMedia):
330-
# Attach and set val to attached name
331-
data[key] = val.to_json()
332-
if isinstance(val.media, InputFile): # type: ignore
333-
data[val.media.attach] = val.media.field_tuple # type: ignore
334-
else:
328+
files = True
329+
# List of media
330+
if isinstance(val, list):
335331
# Attach and set val to attached name for all
336332
media = []
337333
for med in val:
@@ -343,7 +339,16 @@ def post(self, url: str, data: JSONDict, timeout: float = None) -> Union[JSONDic
343339
if "thumb" in media_dict:
344340
data[med.thumb.attach] = med.thumb.field_tuple
345341
data[key] = json.dumps(media)
346-
files = True
342+
# Single media
343+
else:
344+
# Attach and set val to attached name
345+
media_dict = val.to_dict()
346+
if isinstance(val.media, InputFile):
347+
data[val.media.attach] = val.media.field_tuple
348+
# if the file has a thumb, we also need to attach it to the data
349+
if "thumb" in media_dict:
350+
data[val.thumb.attach] = val.thumb.field_tuple
351+
data[key] = json.dumps(media_dict)
347352
elif isinstance(val, list):
348353
# In case we're sending files, we need to json-dump lists manually
349354
# As we can't know if that's the case, we just json-dump here

tests/test_inputmedia.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,21 @@ def test_edit_message_media_new_file(self, bot, chat_id, media_group, thumb_file
591591
)
592592
assert isinstance(new_message, Message)
593593

594+
def test_edit_message_media_with_thumb(
595+
self, bot, chat_id, video_file, photo_file, monkeypatch # noqa: F811
596+
):
597+
def test(*args, **kwargs):
598+
data = kwargs['fields']
599+
video_check = data[input_video.media.attach] == input_video.media.field_tuple
600+
thumb_check = data[input_video.thumb.attach] == input_video.thumb.field_tuple
601+
result = video_check and thumb_check
602+
raise Exception(f"Test was {'successful' if result else 'failing'}")
603+
604+
monkeypatch.setattr('telegram.utils.request.Request._request_wrapper', test)
605+
input_video = InputMediaVideo(video_file, thumb=photo_file)
606+
with pytest.raises(Exception, match='Test was successful'):
607+
bot.edit_message_media(chat_id=chat_id, message_id=123, media=input_video)
608+
594609
@flaky(3, 1)
595610
@pytest.mark.parametrize(
596611
'default_bot', [{'parse_mode': ParseMode.HTML}], indirect=True, ids=['HTML-Bot']

0 commit comments

Comments
 (0)