1
0
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:
eternnoir 2015-08-19 18:08:01 +08:00
parent cb4a58a1e8
commit c3300af656
8 changed files with 110 additions and 6 deletions

View File

@ -231,7 +231,7 @@ class TeleBot:
return types.Message.de_json( return types.Message.de_json(
apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup)) 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 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 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: API reply.
""" """
return types.Message.de_json( 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): def send_document(self, chat_id, data, reply_to_message_id=None, reply_markup=None):
""" """

View File

@ -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') 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): 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) method_url = get_method_by_type(data_type)
payload = {'chat_id': chat_id} 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): def get_method_by_type(data_type):
if data_type == 'audio':
return 'sendAudio'
if data_type == 'document': if data_type == 'document':
return 'sendDocument' return 'sendDocument'
if data_type == 'sticker': if data_type == 'sticker':

View File

@ -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. 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. All subclasses of this class must override de_json.
""" """
@classmethod @classmethod
def de_json(cls, json_type): def de_json(cls, json_type):
""" """
@ -126,6 +127,9 @@ class Message(JsonDeserializable):
if 'audio' in obj: if 'audio' in obj:
opts['audio'] = Audio.de_json(obj['audio']) opts['audio'] = Audio.de_json(obj['audio'])
content_type = 'audio' content_type = 'audio'
if 'voice' in obj:
opts['voice'] = Audio.de_json(obj['voice'])
content_type = 'voice'
if 'document' in obj: if 'document' in obj:
opts['document'] = Document.de_json(obj['document']) opts['document'] = Document.de_json(obj['document'])
content_type = 'document' content_type = 'document'
@ -210,6 +214,35 @@ class PhotoSize(JsonDeserializable):
class Audio(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 @classmethod
def de_json(cls, json_string): def de_json(cls, json_string):
obj = cls.check_json(json_string) obj = cls.check_json(json_string)

BIN
tests/test_data/record.mp3 Normal file

Binary file not shown.

BIN
tests/test_data/record.ogg Normal file

Binary file not shown.

BIN
tests/test_data/record.wav Normal file

Binary file not shown.

View File

@ -115,6 +115,22 @@ def test_send_photo():
assert ret_msg.message_id assert ret_msg.message_id
def test_send_audio():
file_data = open('./test_data/record.mp3', 'rb')
tb = telebot.TeleBot(TOKEN)
ret_msg = tb.send_audio(CHAT_ID, file_data, 1, 'eternnoir', 'pyTelegram')
assert ret_msg.content_type == 'audio'
assert ret_msg.audio.performer == 'eternnoir'
assert ret_msg.audio.title == 'pyTelegram'
def test_send_voice():
file_data = open('./test_data/record.ogg', 'rb')
tb = telebot.TeleBot(TOKEN)
ret_msg = tb.send_voice(CHAT_ID, file_data)
assert ret_msg.voice.mime_type == 'audio/ogg'
def test_send_message(): def test_send_message():
text = 'CI Test Message' text = 'CI Test Message'
tb = telebot.TeleBot(TOKEN) tb = telebot.TeleBot(TOKEN)
@ -180,3 +196,7 @@ def test_is_string_string():
def test_not_string(): def test_not_string():
i1 = 10 i1 = 10
assert not apihelper.is_string(i1) assert not apihelper.is_string(i1)
test_send_voice()
test_send_audio()

View File

@ -39,10 +39,12 @@ def test_json_Document():
def test_json_Message_Audio(): def test_json_Message_Audio():
json_string = r'{"message_id":100,"from":{"id":10734,"first_name":"dd","last_name":"dd","username":"dd"},"chat":{"id":10734,"first_name":"dd","last_name":"dd","username":"dd"},"date":1435481343,"audio":{"duration":3,"mime_type":"audio\/ogg","file_id":"ddg","file_size":8249}}' json_string = r'{"message_id":131,"from":{"id":12775,"first_name":"dd","username":"dd"},"chat":{"id":10834,"first_name":"dd","last_name":"dd","username":"dd"},"date":1439978364,"audio":{"duration":1,"mime_type":"audio\/mpeg","title":"pyTelegram","performer":"eternnoir","file_id":"BQADBQADDH1JaB8-1KyWUss2-Ag","file_size":20096}}'
msg = types.Message.de_json(json_string) msg = types.Message.de_json(json_string)
assert msg.audio.duration == 3 assert msg.audio.duration == 1
assert msg.content_type == 'audio' assert msg.content_type == 'audio'
assert msg.audio.performer == 'eternnoir'
assert msg.audio.title == 'pyTelegram'
def test_json_Message_Sticker(): def test_json_Message_Sticker():
@ -52,6 +54,7 @@ def test_json_Message_Sticker():
assert msg.sticker.thumb.height == 60 assert msg.sticker.thumb.height == 60
assert msg.content_type == 'sticker' assert msg.content_type == 'sticker'
def test_json_Message_Sticker_without_thumb(): def test_json_Message_Sticker_without_thumb():
json_string = r'{"message_id":98,"from":{"id":10734,"first_name":"Fd","last_name":"Wd","username":"dd"},"chat":{"id":10734,"first_name":"Fd","last_name":"Wd","username":"dd"},"date":1435479551,"sticker":{"width":550,"height":368,"file_id":"BQADBQADNAIAAsYifgYdGJOa6bGAsQI","file_size":30320}}' json_string = r'{"message_id":98,"from":{"id":10734,"first_name":"Fd","last_name":"Wd","username":"dd"},"chat":{"id":10734,"first_name":"Fd","last_name":"Wd","username":"dd"},"date":1435479551,"sticker":{"width":550,"height":368,"file_id":"BQADBQADNAIAAsYifgYdGJOa6bGAsQI","file_size":30320}}'
msg = types.Message.de_json(json_string) msg = types.Message.de_json(json_string)
@ -59,6 +62,7 @@ def test_json_Message_Sticker_without_thumb():
assert msg.sticker.thumb == None assert msg.sticker.thumb == None
assert msg.content_type == 'sticker' assert msg.content_type == 'sticker'
def test_json_Message_Document(): def test_json_Message_Document():
json_string = r'{"message_id":97,"from":{"id":10734,"first_name":"Fd","last_name":"Wd","username":"dd"},"chat":{"id":10,"first_name":"Fd","last_name":"Wd","username":"dd"},"date":1435478744,"document":{"file_name":"Text File","thumb":{},"file_id":"BQADBQADMwIAAsYifgZ_CEh0u682xwI","file_size":446}}' json_string = r'{"message_id":97,"from":{"id":10734,"first_name":"Fd","last_name":"Wd","username":"dd"},"chat":{"id":10,"first_name":"Fd","last_name":"Wd","username":"dd"},"date":1435478744,"document":{"file_name":"Text File","thumb":{},"file_id":"BQADBQADMwIAAsYifgZ_CEh0u682xwI","file_size":446}}'
msg = types.Message.de_json(json_string) msg = types.Message.de_json(json_string)
@ -102,3 +106,9 @@ def test_json_contact():
assert contact.first_name == 'dd' assert contact.first_name == 'dd'
assert contact.last_name == 'ddl' assert contact.last_name == 'ddl'
def test_json_voice():
json_string = r'{"duration": 0,"mime_type": "audio/ogg","file_id": "AwcccccccDH1JaB7w_gyFjYQxVAg","file_size": 10481}'
voice = types.Voice.de_json(json_string)
assert voice.duration == 0;
assert voice.file_size == 10481