From 2637e29dbe8aca5b391eb0ce40fe6a6130fd09fe Mon Sep 17 00:00:00 2001 From: heyyyoyy Date: Mon, 15 Jan 2018 16:08:50 +0300 Subject: [PATCH] Updated sendMediaGroup method --- telebot/apihelper.py | 18 ++++++++++++++++-- telebot/types.py | 6 ++++-- telebot/util.py | 7 ++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index a12d38d..24c6c83 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -257,13 +257,14 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re def send_media_group(token, chat_id, media, disable_notification=None, reply_to_message_id=None): method_url = r'sendMediaGroup' - media_json = _convert_list_json_serializable(media) + media_json, files = _convert_input_media(media) payload = {'chat_id': chat_id, 'media': media_json} if disable_notification: payload['disable_notification'] = disable_notification if reply_to_message_id: payload['reply_to_message_id'] = reply_to_message_id - return _make_request(token, method_url, params=payload) + return _make_request(token, method_url, params=payload, method='post' if files else 'get', + files=files if files else None) 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): return markup +def _convert_input_media(array): + media = [] + files = {} + for input_media in array: + if isinstance(input_media, types.JsonSerializable): + media_dict = input_media.to_dic() + if media_dict['media'].startswith('attach://'): + key = media_dict['media'].replace('attach://', '') + files[key] = input_media.media + media.append(media_dict) + return json.dumps(media), files + + def _no_encode(func): def wrapper(key, val): if key == 'filename': diff --git a/telebot/types.py b/telebot/types.py index 1621b1e..5ea9b89 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -1981,7 +1981,8 @@ class InputMediaPhoto(JsonSerializable): return json.dumps(self.to_dic()) def to_dic(self): - ret = {'type': self.type, 'media': self.media} + ret = {'type': self.type, 'media': 'attach://' + util.generate_random_token() + if not util.is_string(self.media) else self.media} if self.caption: ret['caption'] = self.caption return ret @@ -2000,7 +2001,8 @@ class InputMediaVideo(JsonSerializable): return json.dumps(self.to_dic()) def to_dic(self): - ret = {'type': self.type, 'media': self.media} + ret = {'type': self.type, 'media': 'attach://' + util.generate_random_token() + if not util.is_string(self.media) else self.media} if self.caption: ret['caption'] = self.caption if self.width: diff --git a/telebot/util.py b/telebot/util.py index ab7650d..ca080cc 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- - +import random +import string import threading import traceback import re @@ -254,3 +255,7 @@ def per_thread(key, construct_value): value = construct_value() setattr(thread_local, key, value) return value + + +def generate_random_token(): + return ''.join(random.sample(string.ascii_letters, 16)) \ No newline at end of file