Skip to content

Commit 55302cb

Browse files
authored
Merge pull request eternnoir#445 from heyyyoyy/update_send_media_group
Added support for local files in the sendMediaGroup method
2 parents f47653d + 2637e29 commit 55302cb

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

telebot/apihelper.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,14 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re
257257

258258
def send_media_group(token, chat_id, media, disable_notification=None, reply_to_message_id=None):
259259
method_url = r'sendMediaGroup'
260-
media_json = _convert_list_json_serializable(media)
260+
media_json, files = _convert_input_media(media)
261261
payload = {'chat_id': chat_id, 'media': media_json}
262262
if disable_notification:
263263
payload['disable_notification'] = disable_notification
264264
if reply_to_message_id:
265265
payload['reply_to_message_id'] = reply_to_message_id
266-
return _make_request(token, method_url, params=payload)
266+
return _make_request(token, method_url, params=payload, method='post' if files else 'get',
267+
files=files if files else None)
267268

268269

269270
def send_location(token, chat_id, latitude, longitude, live_period=None, reply_to_message_id=None, reply_markup=None,
@@ -916,6 +917,19 @@ def _convert_markup(markup):
916917
return markup
917918

918919

920+
def _convert_input_media(array):
921+
media = []
922+
files = {}
923+
for input_media in array:
924+
if isinstance(input_media, types.JsonSerializable):
925+
media_dict = input_media.to_dic()
926+
if media_dict['media'].startswith('attach://'):
927+
key = media_dict['media'].replace('attach://', '')
928+
files[key] = input_media.media
929+
media.append(media_dict)
930+
return json.dumps(media), files
931+
932+
919933
def _no_encode(func):
920934
def wrapper(key, val):
921935
if key == 'filename':

telebot/types.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,8 @@ def to_json(self):
19841984
return json.dumps(self.to_dic())
19851985

19861986
def to_dic(self):
1987-
ret = {'type': self.type, 'media': self.media}
1987+
ret = {'type': self.type, 'media': 'attach://' + util.generate_random_token()
1988+
if not util.is_string(self.media) else self.media}
19881989
if self.caption:
19891990
ret['caption'] = self.caption
19901991
return ret
@@ -2003,7 +2004,8 @@ def to_json(self):
20032004
return json.dumps(self.to_dic())
20042005

20052006
def to_dic(self):
2006-
ret = {'type': self.type, 'media': self.media}
2007+
ret = {'type': self.type, 'media': 'attach://' + util.generate_random_token()
2008+
if not util.is_string(self.media) else self.media}
20072009
if self.caption:
20082010
ret['caption'] = self.caption
20092011
if self.width:

telebot/util.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
2+
import random
3+
import string
34
import threading
45
import traceback
56
import re
@@ -254,3 +255,7 @@ def per_thread(key, construct_value):
254255
value = construct_value()
255256
setattr(thread_local, key, value)
256257
return value
258+
259+
260+
def generate_random_token():
261+
return ''.join(random.sample(string.ascii_letters, 16))

0 commit comments

Comments
 (0)