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

Merge branch 'change20150815' into develop

This commit is contained in:
eternnoir 2015-08-19 18:28:07 +08:00
commit 7dc1c5095a
8 changed files with 128 additions and 13 deletions

View File

@ -231,18 +231,33 @@ 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
:param chat_id:
:param data:
:param reply_to_message_id:
Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format.
:param chat_id:Unique identifier for the message recipient
:param audio:Audio file to send.
:param duration:Duration of the audio in seconds
:param performer:Performer
:param title:Track name
:param reply_to_message_id:If the message is a reply, ID of the original messag
:param reply_markup:
:return: API reply.
:return: Message
"""
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):
"""
Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message.
:param chat_id:Unique identifier for the message recipient.
:param voice:
:param duration:Duration of sent audio in seconds
:param reply_to_message_id:
:param reply_markup:
:return: Message
"""
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):
"""

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')
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':

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.
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'
@ -215,13 +219,42 @@ class Audio(JsonDeserializable):
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']
return Audio(file_id, duration, mime_type, 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)
file_id = obj['file_id']
duration = obj['duration']
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']
return Voice(file_id, duration, mime_type, file_size)
def __init__(self, file_id, duration, mime_type=None, file_size=None):
self.file_id = file_id

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
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():
text = 'CI Test Message'
tb = telebot.TeleBot(TOKEN)
@ -180,3 +196,5 @@ def test_is_string_string():
def test_not_string():
i1 = 10
assert not apihelper.is_string(i1)
test_send_voice()

View File

@ -39,10 +39,12 @@ def test_json_Document():
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)
assert msg.audio.duration == 3
assert msg.audio.duration == 1
assert msg.content_type == 'audio'
assert msg.audio.performer == 'eternnoir'
assert msg.audio.title == 'pyTelegram'
def test_json_Message_Sticker():
@ -52,6 +54,7 @@ def test_json_Message_Sticker():
assert msg.sticker.thumb.height == 60
assert msg.content_type == 'sticker'
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}}'
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.content_type == 'sticker'
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}}'
msg = types.Message.de_json(json_string)
@ -102,3 +106,11 @@ def test_json_contact():
assert contact.first_name == 'dd'
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
test_json_voice()