Skip to content

Commit c1fbed2

Browse files
committed
Make edit_message_media work with new files
1 parent bc4ded8 commit c1fbed2

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

telegram/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@
9191
from .webhookinfo import WebhookInfo
9292
from .games.gamehighscore import GameHighScore
9393
from .update import Update
94+
from .files.inputmedia import (InputMedia, InputMediaVideo, InputMediaPhoto, InputMediaAnimation,
95+
InputMediaAudio, InputMediaDocument)
9496
from .bot import Bot
9597
from .constants import (MAX_MESSAGE_LENGTH, MAX_CAPTION_LENGTH, SUPPORTED_WEBHOOK_PORTS,
9698
MAX_FILESIZE_DOWNLOAD, MAX_FILESIZE_UPLOAD,
9799
MAX_MESSAGES_PER_SECOND_PER_CHAT, MAX_MESSAGES_PER_SECOND,
98100
MAX_MESSAGES_PER_MINUTE_PER_GROUP)
99-
from .files.inputmedia import (InputMedia, InputMediaVideo, InputMediaPhoto, InputMediaAnimation,
100-
InputMediaAudio, InputMediaDocument)
101101
from .version import __version__ # flake8: noqa
102102

103103
__author__ = 'devs@python-telegram-bot.org'

telegram/bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ def edit_message_media(self,
18051805
Identifier of the sent message.
18061806
inline_message_id (:obj:`str`, optional): Required if chat_id and message_id are not
18071807
specified. Identifier of the inline message.
1808-
media (:class:`telegram.InputMedia`): A JSON-serialized object for a new media content
1808+
media (:class:`telegram.InputMedia`): An object for a new media content
18091809
of the message.
18101810
reply_markup (:class:`telegram.ReplyMarkup`, optional): Additional interface options. A
18111811
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
@@ -1823,7 +1823,7 @@ def edit_message_media(self,
18231823

18241824
url = '{0}/editMessageMedia'.format(self.base_url)
18251825

1826-
data = {'media': media.to_dict()}
1826+
data = {'media': media}
18271827

18281828
if chat_id:
18291829
data['chat_id'] = chat_id

telegram/utils/request.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"how to properly install.")
4242
raise
4343

44-
from telegram import (InputFile, TelegramError)
44+
from telegram import (InputFile, TelegramError, InputMedia)
4545
from telegram.error import (Unauthorized, NetworkError, TimedOut, BadRequest, ChatMigrated,
4646
RetryAfter, InvalidToken)
4747

@@ -276,22 +276,31 @@ def post(self, url, data, timeout=None):
276276
files = False
277277

278278
for key, val in data.copy().items():
279-
if key == 'media':
280-
media = []
281-
for m in val:
282-
media.append(m.to_dict())
283-
if isinstance(m.media, InputFile):
284-
data[m.media.attach] = m.media.field_tuple
285-
data[key] = json.dumps(media)
286-
files = True
287-
288279
if isinstance(val, InputFile):
280+
# Convert the InputFile to urllib3 field format
289281
data[key] = val.field_tuple
290282
files = True
291-
292-
if isinstance(val, (float, int)):
283+
elif isinstance(val, (float, int)):
284+
# Urllib3 doesn't like floats it seems
293285
data[key] = str(val)
286+
elif key == 'media':
287+
# One media or multiple
288+
if isinstance(val, InputMedia):
289+
# Attach and set val to attached name
290+
data[key] = val.to_json()
291+
if isinstance(val.media, InputFile):
292+
data[val.media.attach] = val.media.field_tuple
293+
else:
294+
# Attach and set val to attached name for all
295+
media = []
296+
for m in val:
297+
media.append(m.to_dict())
298+
if isinstance(m.media, InputFile):
299+
data[m.media.attach] = m.media.field_tuple
300+
data[key] = json.dumps(media)
301+
files = True
294302

303+
# Use multipart upload if we're uploading files, otherwise use JSON
295304
if files:
296305
result = self._request_wrapper('POST', url, fields=data, **urlopen_kwargs)
297306
else:

tests/test_inputmedia.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def input_media_video(class_thumb_file):
4949
def input_media_photo(class_thumb_file):
5050
return InputMediaPhoto(media=TestInputMediaPhoto.media,
5151
caption=TestInputMediaPhoto.caption,
52-
thumb=class_thumb_file,
5352
parse_mode=TestInputMediaPhoto.parse_mode)
5453

5554

@@ -144,7 +143,6 @@ def test_expected_values(self, input_media_photo):
144143
assert input_media_photo.media == self.media
145144
assert input_media_photo.caption == self.caption
146145
assert input_media_photo.parse_mode == self.parse_mode
147-
assert isinstance(input_media_photo.thumb, InputFile)
148146

149147
def test_to_dict(self, input_media_photo):
150148
input_media_photo_dict = input_media_photo.to_dict()
@@ -337,3 +335,13 @@ def test_edit_message_media(self, bot, chat_id, media_group):
337335
mid = messages[-1].message_id
338336
new_message = bot.edit_message_media(chat_id=cid, message_id=mid, media=media_group[0])
339337
assert isinstance(new_message, Message)
338+
339+
@flaky(3, 1)
340+
@pytest.mark.timeout(10)
341+
def test_edit_message_media_new_file(self, bot, chat_id, media_group, thumb_file):
342+
messages = bot.send_media_group(chat_id, media_group)
343+
cid = messages[-1].chat.id
344+
mid = messages[-1].message_id
345+
new_message = bot.edit_message_media(chat_id=cid, message_id=mid,
346+
media=InputMediaPhoto(thumb_file))
347+
assert isinstance(new_message, Message)

0 commit comments

Comments
 (0)