Skip to content

Commit 8ac6e66

Browse files
committed
new: InputMediaAnimation, InputMediaAudio, InputMediaDocument, editMessageMedia
Added support for editing the media content of messages: added the method editMessageMedia and new types InputMediaAnimation, InputMediaAudio, and InputMediaDocument.
1 parent 41f7c07 commit 8ac6e66

File tree

4 files changed

+128
-23
lines changed

4 files changed

+128
-23
lines changed

telebot/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,12 @@ def edit_message_text(self, text, chat_id=None, message_id=None, inline_message_
999999
return result
10001000
return types.Message.de_json(result)
10011001

1002+
def edit_message_media(self, media, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None):
1003+
result = apihelper.edit_message_media(self.token, media, chat_id, message_id, inline_message_id, reply_markup)
1004+
if type(result) == bool: # if edit inline message return is bool not Message.
1005+
return result
1006+
return types.Message.de_json(result)
1007+
10021008
def edit_message_reply_markup(self, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None):
10031009
result = apihelper.edit_message_reply_markup(self.token, chat_id, message_id, inline_message_id, reply_markup)
10041010
if type(result) == bool:
@@ -1677,6 +1683,10 @@ def unpin_chat_message(self, *args):
16771683
def edit_message_text(self, *args, **kwargs):
16781684
return TeleBot.edit_message_text(self, *args, **kwargs)
16791685

1686+
@util.async_dec()
1687+
def edit_message_media(self, *args, **kwargs):
1688+
return TeleBot.edit_message_media(self, *args, **kwargs)
1689+
16801690
@util.async_dec()
16811691
def edit_message_reply_markup(self, *args, **kwargs):
16821692
return TeleBot.edit_message_reply_markup(self, *args, **kwargs)

telebot/apihelper.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
from telebot.types import InputMedia
23

34
try:
45
import ujson as json
@@ -264,7 +265,7 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re
264265

265266
def send_media_group(token, chat_id, media, disable_notification=None, reply_to_message_id=None):
266267
method_url = r'sendMediaGroup'
267-
media_json, files = _convert_input_media(media)
268+
media_json, files = _convert_input_media_array(media)
268269
payload = {'chat_id': chat_id, 'media': media_json}
269270
if disable_notification:
270271
payload['disable_notification'] = disable_notification
@@ -638,6 +639,21 @@ def edit_message_caption(token, caption, chat_id=None, message_id=None, inline_m
638639
return _make_request(token, method_url, params=payload)
639640

640641

642+
def edit_message_media(token, media, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None):
643+
method_url = r'editMessageMedia'
644+
media_json, file = _convert_input_media(media)
645+
payload = {'media': media_json}
646+
if chat_id:
647+
payload['chat_id'] = chat_id
648+
if message_id:
649+
payload['message_id'] = message_id
650+
if inline_message_id:
651+
payload['inline_message_id'] = inline_message_id
652+
if reply_markup:
653+
payload['reply_markup'] = _convert_markup(reply_markup)
654+
return _make_request(token, method_url, params=payload, files=file)
655+
656+
641657
def edit_message_reply_markup(token, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None):
642658
method_url = r'editMessageReplyMarkup'
643659
payload = {}
@@ -937,11 +953,17 @@ def _convert_markup(markup):
937953
return markup
938954

939955

940-
def _convert_input_media(array):
956+
def _convert_input_media(media):
957+
if isinstance(media, InputMedia):
958+
return media._convert_input_media()
959+
return None, None
960+
961+
962+
def _convert_input_media_array(array):
941963
media = []
942964
files = {}
943965
for input_media in array:
944-
if isinstance(input_media, types.JsonSerializable):
966+
if isinstance(input_media, InputMedia):
945967
media_dict = input_media.to_dic()
946968
if media_dict['media'].startswith('attach://'):
947969
key = media_dict['media'].replace('attach://', '')

telebot/types.py

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,48 +2073,56 @@ def to_dic(self):
20732073

20742074
# InputMedia
20752075

2076-
class InputMediaPhoto(JsonSerializable):
2077-
def __init__(self, media, caption=None, parse_mode=None):
2078-
self.type = "photo"
2076+
class InputMedia(JsonSerializable):
2077+
def __init__(self, type, media, caption=None, parse_mode=None):
2078+
self.type = type
20792079
self.media = media
20802080
self.caption = caption
20812081
self.parse_mode = parse_mode
20822082

2083+
self._media_dic = 'attach://' + util.generate_random_token() if not util.is_string(self.media) else self.media
2084+
20832085
def to_json(self):
20842086
return json.dumps(self.to_dic())
20852087

20862088
def to_dic(self):
2087-
ret = {'type': self.type, 'media': 'attach://' + util.generate_random_token()
2088-
if not util.is_string(self.media) else self.media}
2089+
ret = {'type': self.type, 'media': self._media_dic}
20892090
if self.caption:
20902091
ret['caption'] = self.caption
20912092
if self.parse_mode:
20922093
ret['parse_mode'] = self.parse_mode
20932094
return ret
20942095

2096+
def _convert_input_media(self):
2097+
if util.is_string(self.media):
2098+
return self.to_json(), None
2099+
2100+
return self.to_json(), {self._media_dic: self.media}
2101+
2102+
2103+
class InputMediaPhoto(InputMedia):
2104+
def __init__(self, media, caption=None, parse_mode=None):
2105+
super(InputMedia).__init__(type="photo", media=media, caption=caption, parse_mode=parse_mode)
2106+
2107+
def to_dic(self):
2108+
ret = super(InputMedia).to_dic()
2109+
return ret
2110+
20952111

2096-
class InputMediaVideo(JsonSerializable):
2097-
def __init__(self, media, caption=None, parse_mode=None, width=None, height=None, duration=None,
2112+
class InputMediaVideo(InputMedia):
2113+
def __init__(self, media, thumb=None, caption=None, parse_mode=None, width=None, height=None, duration=None,
20982114
supports_streaming=None):
2099-
self.type = "video"
2100-
self.media = media
2101-
self.caption = caption
2102-
self.parse_mode = parse_mode
2115+
super(InputMedia).__init__(type="video", media=media, caption=caption, parse_mode=parse_mode)
2116+
self.thumb = thumb
21032117
self.width = width
21042118
self.height = height
21052119
self.duration = duration
21062120
self.supports_streaming = supports_streaming
21072121

2108-
def to_json(self):
2109-
return json.dumps(self.to_dic())
2110-
21112122
def to_dic(self):
2112-
ret = {'type': self.type, 'media': 'attach://' + util.generate_random_token()
2113-
if not util.is_string(self.media) else self.media}
2114-
if self.caption:
2115-
ret['caption'] = self.caption
2116-
if self.parse_mode:
2117-
ret['parse_mode'] = self.parse_mode
2123+
ret = super(InputMedia).to_dic()
2124+
if self.thumb:
2125+
ret['thumb'] = self.thumb
21182126
if self.width:
21192127
ret['width'] = self.width
21202128
if self.height:
@@ -2124,3 +2132,57 @@ def to_dic(self):
21242132
if self.supports_streaming:
21252133
ret['supports_streaming'] = self.supports_streaming
21262134
return ret
2135+
2136+
2137+
class InputMediaAnimation(InputMedia):
2138+
def __init__(self, media, thumb=None, caption=None, parse_mode=None, width=None, height=None, duration=None):
2139+
super(InputMedia).__init__(type="animation", media=media, caption=caption, parse_mode=parse_mode)
2140+
self.thumb = thumb
2141+
self.width = width
2142+
self.height = height
2143+
self.duration = duration
2144+
2145+
def to_dic(self):
2146+
ret = super(InputMedia).to_dic()
2147+
if self.thumb:
2148+
ret['thumb'] = self.thumb
2149+
if self.width:
2150+
ret['width'] = self.width
2151+
if self.height:
2152+
ret['height'] = self.height
2153+
if self.duration:
2154+
ret['duration'] = self.duration
2155+
return ret
2156+
2157+
2158+
class InputMediaAudio(InputMedia):
2159+
def __init__(self, media, thumb=None, caption=None, parse_mode=None, duration=None, performer=None, title=None):
2160+
super(InputMedia).__init__(type="audio", media=media, caption=caption, parse_mode=parse_mode)
2161+
self.thumb = thumb
2162+
self.duration = duration
2163+
self.performer = performer
2164+
self.title = title
2165+
2166+
def to_dic(self):
2167+
ret = super(InputMedia).to_dic()
2168+
if self.thumb:
2169+
ret['thumb'] = self.thumb
2170+
if self.duration:
2171+
ret['duration'] = self.duration
2172+
if self.performer:
2173+
ret['performer'] = self.performer
2174+
if self.title:
2175+
ret['title'] = self.title
2176+
return ret
2177+
2178+
2179+
class InputMediaDocument(InputMedia):
2180+
def __init__(self, media, thumb=None, caption=None, parse_mode=None):
2181+
super(InputMedia).__init__(type="document", media=media, caption=caption, parse_mode=parse_mode)
2182+
self.thumb = thumb
2183+
2184+
def to_dic(self):
2185+
ret = super(InputMedia).to_dic()
2186+
if self.thumb:
2187+
ret['thumb'] = self.thumb
2188+
return ret

tests/test_telebot.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ def test_edit_message_caption(self):
361361
new_msg = tb.edit_message_caption(caption='Edit test', chat_id=CHAT_ID, message_id=msg.message_id)
362362
assert new_msg.caption == 'Edit test'
363363

364+
def test_edit_message_media(self):
365+
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
366+
file_data_2 = open('../examples/detailed_example/rooster.jpg', 'rb')
367+
tb = telebot.TeleBot(TOKEN)
368+
msg = tb.send_photo(CHAT_ID, file_data)
369+
new_msg = tb.edit_message_media(chat_id=CHAT_ID, message_id=msg.message_id, media=file_data_2)
370+
assert type(new_msg) != bool
371+
372+
new_msg = tb.edit_message_media(chat_id=CHAT_ID, message_id=msg.message_id, media=msg.photo[0].file_id)
373+
assert type(new_msg) != bool
374+
364375
def test_get_chat(self):
365376
tb = telebot.TeleBot(TOKEN)
366377
ch = tb.get_chat(GROUP_ID)

0 commit comments

Comments
 (0)