mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
New audio,voice supported.
This commit is contained in:
@ -231,7 +231,7 @@ class TeleBot:
|
||||
return types.Message.de_json(
|
||||
apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup))
|
||||
|
||||
def send_audio(self, chat_id, data, reply_to_message_id=None, reply_markup=None):
|
||||
def send_audio(self, chat_id, audio, duration=None, performer=None, title=None, reply_to_message_id=None, reply_markup=None):
|
||||
"""
|
||||
Use this method to send audio files, if you want Telegram clients to display the file as a playable
|
||||
voice message. For this to work, your audio must be in an .ogg file encoded with OPUS
|
||||
@ -242,7 +242,11 @@ class TeleBot:
|
||||
:return: API reply.
|
||||
"""
|
||||
return types.Message.de_json(
|
||||
apihelper.send_data(self.token, chat_id, data, 'audio', reply_to_message_id, reply_markup))
|
||||
apihelper.send_audio(self.token, chat_id, audio,duration,performer,title, reply_to_message_id, reply_markup))
|
||||
|
||||
def send_voice(self, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None):
|
||||
return types.Message.de_json(
|
||||
apihelper.send_voice(self.token, chat_id, voice, duration, reply_to_message_id,reply_markup))
|
||||
|
||||
def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=None):
|
||||
"""
|
||||
|
@ -139,6 +139,45 @@ def send_video(token, chat_id, data, duration=None, caption=None, reply_to_messa
|
||||
return _make_request(token, method_url, params=payload, files=files, method='post')
|
||||
|
||||
|
||||
def send_voice(token, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None):
|
||||
method_url = r'sendVoice'
|
||||
payload = {'chat_id': chat_id}
|
||||
files = None
|
||||
if not is_string(voice):
|
||||
files = {'voice': voice}
|
||||
else:
|
||||
payload['voice'] = voice
|
||||
if duration:
|
||||
payload['duration'] = duration
|
||||
if reply_to_message_id:
|
||||
payload['reply_to_message_id'] = reply_to_message_id
|
||||
if reply_markup:
|
||||
payload['reply_markup'] = _convert_markup(reply_markup)
|
||||
return _make_request(token, method_url, params=payload, files=files, method='post')
|
||||
|
||||
|
||||
def send_audio(token, chat_id, audio, duration=None, performer=None, title=None, reply_to_message_id=None,
|
||||
reply_markup=None):
|
||||
method_url = r'sendAudio'
|
||||
payload = {'chat_id': chat_id}
|
||||
files = None
|
||||
if not is_string(audio):
|
||||
files = {'audio': audio}
|
||||
else:
|
||||
payload['audio'] = audio
|
||||
if duration:
|
||||
payload['duration'] = duration
|
||||
if performer:
|
||||
payload['performer'] = performer
|
||||
if title:
|
||||
payload['title'] = title
|
||||
if reply_to_message_id:
|
||||
payload['reply_to_message_id'] = reply_to_message_id
|
||||
if reply_markup:
|
||||
payload['reply_markup'] = _convert_markup(reply_markup)
|
||||
return _make_request(token, method_url, params=payload, files=files, method='post')
|
||||
|
||||
|
||||
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None):
|
||||
method_url = get_method_by_type(data_type)
|
||||
payload = {'chat_id': chat_id}
|
||||
@ -155,8 +194,6 @@ def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_m
|
||||
|
||||
|
||||
def get_method_by_type(data_type):
|
||||
if data_type == 'audio':
|
||||
return 'sendAudio'
|
||||
if data_type == 'document':
|
||||
return 'sendDocument'
|
||||
if data_type == 'sticker':
|
||||
|
@ -44,6 +44,7 @@ class JsonDeserializable:
|
||||
Subclasses of this class are guaranteed to be able to be created from a json-style dict or json formatted string.
|
||||
All subclasses of this class must override de_json.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def de_json(cls, json_type):
|
||||
"""
|
||||
@ -126,6 +127,9 @@ class Message(JsonDeserializable):
|
||||
if 'audio' in obj:
|
||||
opts['audio'] = Audio.de_json(obj['audio'])
|
||||
content_type = 'audio'
|
||||
if 'voice' in obj:
|
||||
opts['voice'] = Audio.de_json(obj['voice'])
|
||||
content_type = 'voice'
|
||||
if 'document' in obj:
|
||||
opts['document'] = Document.de_json(obj['document'])
|
||||
content_type = 'document'
|
||||
@ -210,6 +214,35 @@ class PhotoSize(JsonDeserializable):
|
||||
|
||||
|
||||
class Audio(JsonDeserializable):
|
||||
@classmethod
|
||||
def de_json(cls, json_string):
|
||||
obj = cls.check_json(json_string)
|
||||
file_id = obj['file_id']
|
||||
duration = obj['duration']
|
||||
performer = None
|
||||
title = None
|
||||
mime_type = None
|
||||
file_size = None
|
||||
if 'mime_type' in obj:
|
||||
mime_type = obj['mime_type']
|
||||
if 'file_size' in obj:
|
||||
file_size = obj['file_size']
|
||||
if 'performer' in obj:
|
||||
performer = obj['performer']
|
||||
if 'title' in obj:
|
||||
title = obj['title']
|
||||
return Audio(file_id, duration, performer, title, mime_type, file_size)
|
||||
|
||||
def __init__(self, file_id, duration, performer=None, title=None, mime_type=None, file_size=None):
|
||||
self.file_id = file_id
|
||||
self.duration = duration
|
||||
self.performer = performer
|
||||
self.title = title
|
||||
self.mime_type = mime_type
|
||||
self.file_size = file_size
|
||||
|
||||
|
||||
class Voice(JsonDeserializable):
|
||||
@classmethod
|
||||
def de_json(cls, json_string):
|
||||
obj = cls.check_json(json_string)
|
||||
|
Reference in New Issue
Block a user