1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #445 from heyyyoyy/update_send_media_group

Added support for local files in the sendMediaGroup method
This commit is contained in:
FrankWang 2018-02-01 19:24:50 +08:00 committed by GitHub
commit 55302cb972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 5 deletions

View File

@ -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): def send_media_group(token, chat_id, media, disable_notification=None, reply_to_message_id=None):
method_url = r'sendMediaGroup' 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} payload = {'chat_id': chat_id, 'media': media_json}
if disable_notification: if disable_notification:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if reply_to_message_id: if reply_to_message_id:
payload['reply_to_message_id'] = 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, 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 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 _no_encode(func):
def wrapper(key, val): def wrapper(key, val):
if key == 'filename': if key == 'filename':

View File

@ -1984,7 +1984,8 @@ class InputMediaPhoto(JsonSerializable):
return json.dumps(self.to_dic()) return json.dumps(self.to_dic())
def to_dic(self): 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: if self.caption:
ret['caption'] = self.caption ret['caption'] = self.caption
return ret return ret
@ -2003,7 +2004,8 @@ class InputMediaVideo(JsonSerializable):
return json.dumps(self.to_dic()) return json.dumps(self.to_dic())
def to_dic(self): 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: if self.caption:
ret['caption'] = self.caption ret['caption'] = self.caption
if self.width: if self.width:

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import random
import string
import threading import threading
import traceback import traceback
import re import re
@ -254,3 +255,7 @@ def per_thread(key, construct_value):
value = construct_value() value = construct_value()
setattr(thread_local, key, value) setattr(thread_local, key, value)
return value return value
def generate_random_token():
return ''.join(random.sample(string.ascii_letters, 16))