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

Bot Api 3.6

This commit is contained in:
heyyyoyy 2018-02-14 20:27:55 +00:00
parent 55302cb972
commit 2e199a5684
4 changed files with 97 additions and 17 deletions

View File

@ -486,24 +486,25 @@ class TeleBot:
return apihelper.delete_message(self.token, chat_id, message_id) return apihelper.delete_message(self.token, chat_id, message_id)
def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None, def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None,
disable_notification=None): parse_mode=None, disable_notification=None):
""" """
Use this method to send photos. Use this method to send photos.
:param disable_notification: :param disable_notification:
:param chat_id: :param chat_id:
:param photo: :param photo:
:param caption: :param caption:
:param parse_mode
:param reply_to_message_id: :param reply_to_message_id:
:param reply_markup: :param reply_markup:
:return: API reply. :return: API reply.
""" """
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,
disable_notification)) parse_mode, disable_notification))
def send_audio(self, chat_id, audio, caption=None, duration=None, performer=None, title=None, def send_audio(self, chat_id, audio, caption=None, duration=None, performer=None, title=None,
reply_to_message_id=None, reply_to_message_id=None,
reply_markup=None, disable_notification=None, timeout=None): reply_markup=None, parse_mode=None, disable_notification=None, timeout=None):
""" """
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. 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 chat_id:Unique identifier for the message recipient
@ -511,16 +512,17 @@ class TeleBot:
:param duration:Duration of the audio in seconds :param duration:Duration of the audio in seconds
:param performer:Performer :param performer:Performer
:param title:Track name :param title:Track name
:param parse_mode
:param reply_to_message_id:If the message is a reply, ID of the original message :param reply_to_message_id:If the message is a reply, ID of the original message
:param reply_markup: :param reply_markup:
:return: Message :return: Message
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_audio(self.token, chat_id, audio, caption, duration, performer, title, reply_to_message_id, apihelper.send_audio(self.token, chat_id, audio, caption, duration, performer, title, reply_to_message_id,
reply_markup, disable_notification, timeout)) reply_markup, parse_mode, disable_notification, timeout))
def send_voice(self, chat_id, voice, caption=None, duration=None, reply_to_message_id=None, reply_markup=None, def send_voice(self, chat_id, voice, caption=None, duration=None, reply_to_message_id=None, reply_markup=None,
disable_notification=None, timeout=None): parse_mode=None, disable_notification=None, timeout=None):
""" """
Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. 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 chat_id:Unique identifier for the message recipient.
@ -528,25 +530,27 @@ class TeleBot:
:param duration:Duration of sent audio in seconds :param duration:Duration of sent audio in seconds
:param reply_to_message_id: :param reply_to_message_id:
:param reply_markup: :param reply_markup:
:param parse_mode
:return: Message :return: Message
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_voice(self.token, chat_id, voice, caption, duration, reply_to_message_id, reply_markup, apihelper.send_voice(self.token, chat_id, voice, caption, duration, reply_to_message_id, reply_markup,
disable_notification, timeout)) parse_mode, disable_notification, timeout))
def send_document(self, chat_id, data, reply_to_message_id=None, caption=None, reply_markup=None, def send_document(self, chat_id, data, reply_to_message_id=None, caption=None, reply_markup=None,
disable_notification=None, timeout=None): parse_mode=None, disable_notification=None, timeout=None):
""" """
Use this method to send general files. Use this method to send general files.
:param chat_id: :param chat_id:
:param data: :param data:
:param reply_to_message_id: :param reply_to_message_id:
:param reply_markup: :param reply_markup:
:param parse_mode
:return: API reply. :return: API reply.
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup, apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup,
disable_notification, timeout, caption=caption)) parse_mode, disable_notification, timeout, caption=caption))
def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None, def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None,
timeout=None): timeout=None):
@ -563,20 +567,22 @@ class TeleBot:
disable_notification, timeout)) disable_notification, timeout))
def send_video(self, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None, def send_video(self, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None,
disable_notification=None, timeout=None): parse_mode=None, supports_streaming=None, disable_notification=None, timeout=None):
""" """
Use this method to send video files, Telegram clients support mp4 videos. Use this method to send video files, Telegram clients support mp4 videos.
:param chat_id: Integer : Unique identifier for the message recipient User or GroupChat id :param chat_id: Integer : Unique identifier for the message recipient User or GroupChat id
:param data: InputFile or String : Video to send. You can either pass a file_id as String to resend a video that is already on the Telegram server :param data: InputFile or String : Video to send. You can either pass a file_id as String to resend a video that is already on the Telegram server
:param duration: Integer : Duration of sent video in seconds :param duration: Integer : Duration of sent video in seconds
:param caption: String : Video caption (may also be used when resending videos by file_id). :param caption: String : Video caption (may also be used when resending videos by file_id).
:param parse_mode:
:param supports_streaming:
:param reply_to_message_id: :param reply_to_message_id:
:param reply_markup: :param reply_markup:
:return: :return:
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_video(self.token, chat_id, data, duration, caption, reply_to_message_id, reply_markup, apihelper.send_video(self.token, chat_id, data, duration, caption, reply_to_message_id, reply_markup,
disable_notification, timeout)) parse_mode, supports_streaming, disable_notification, timeout))
def send_video_note(self, chat_id, data, duration=None, length=None, reply_to_message_id=None, reply_markup=None, def send_video_note(self, chat_id, data, duration=None, length=None, reply_to_message_id=None, reply_markup=None,
disable_notification=None, timeout=None): disable_notification=None, timeout=None):

View File

@ -236,7 +236,7 @@ def forward_message(token, chat_id, from_chat_id, message_id, disable_notificati
def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None, def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None,
disable_notification=None): parse_mode=None, disable_notification=None):
method_url = r'sendPhoto' method_url = r'sendPhoto'
payload = {'chat_id': chat_id} payload = {'chat_id': chat_id}
files = None files = None
@ -250,6 +250,8 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode:
payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
return _make_request(token, method_url, params=payload, files=files, method='post') return _make_request(token, method_url, params=payload, files=files, method='post')
@ -349,7 +351,7 @@ def send_chat_action(token, chat_id, action):
def send_video(token, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None, def send_video(token, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None,
disable_notification=None, timeout=None): parse_mode=None, supports_streaming=None, disable_notification=None, timeout=None):
method_url = r'sendVideo' method_url = r'sendVideo'
payload = {'chat_id': chat_id} payload = {'chat_id': chat_id}
files = None files = None
@ -365,6 +367,10 @@ def send_video(token, chat_id, data, duration=None, caption=None, reply_to_messa
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode:
payload['parse_mode'] = parse_mode
if supports_streaming:
payload['supports_streaming'] = supports_streaming
if disable_notification: if disable_notification:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
@ -373,7 +379,7 @@ def send_video(token, chat_id, data, duration=None, caption=None, reply_to_messa
def send_voice(token, chat_id, voice, caption=None, duration=None, reply_to_message_id=None, reply_markup=None, def send_voice(token, chat_id, voice, caption=None, duration=None, reply_to_message_id=None, reply_markup=None,
disable_notification=None, timeout=None): parse_mode=None, disable_notification=None, timeout=None):
method_url = r'sendVoice' method_url = r'sendVoice'
payload = {'chat_id': chat_id} payload = {'chat_id': chat_id}
files = None files = None
@ -389,6 +395,8 @@ def send_voice(token, chat_id, voice, caption=None, duration=None, reply_to_mess
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode:
payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
@ -423,7 +431,7 @@ def send_video_note(token, chat_id, data, duration=None, length=None, reply_to_m
def send_audio(token, chat_id, audio, caption=None, duration=None, performer=None, title=None, reply_to_message_id=None, def send_audio(token, chat_id, audio, caption=None, duration=None, performer=None, title=None, reply_to_message_id=None,
reply_markup=None, disable_notification=None, timeout=None): reply_markup=None, parse_mode=None, disable_notification=None, timeout=None):
method_url = r'sendAudio' method_url = r'sendAudio'
payload = {'chat_id': chat_id} payload = {'chat_id': chat_id}
files = None files = None
@ -443,6 +451,8 @@ def send_audio(token, chat_id, audio, caption=None, duration=None, performer=Non
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode:
payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
@ -451,7 +461,7 @@ def send_audio(token, chat_id, audio, caption=None, duration=None, performer=Non
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None, disable_notification=None, def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None, disable_notification=None,
timeout=None, caption=None): timeout=None, parse_mode=None, caption=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}
files = None files = None
@ -463,6 +473,8 @@ def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_m
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode and data_type == 'document':
payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:

View File

@ -366,6 +366,9 @@ class Message(JsonDeserializable):
if 'successful_payment' in obj: if 'successful_payment' in obj:
opts['successful_payment'] = SuccessfulPayment.de_json(obj['successful_payment']) opts['successful_payment'] = SuccessfulPayment.de_json(obj['successful_payment'])
content_type = 'successful_payment' content_type = 'successful_payment'
if 'connected_website' in obj:
opts['connected_website'] = obj['connected_website']
content_type = 'connected_website'
return cls(message_id, from_user, date, chat, content_type, opts) return cls(message_id, from_user, date, chat, content_type, opts)
@classmethod @classmethod
@ -430,6 +433,7 @@ class Message(JsonDeserializable):
self.pinned_message = None self.pinned_message = None
self.invoice = None self.invoice = None
self.successful_payment = None self.successful_payment = None
self.connected_website = None
for key in options: for key in options:
setattr(self, key, options[key]) setattr(self, key, options[key])
@ -1975,10 +1979,11 @@ class MaskPosition(JsonDeserializable, JsonSerializable):
# InputMedia # InputMedia
class InputMediaPhoto(JsonSerializable): class InputMediaPhoto(JsonSerializable):
def __init__(self, media, caption=None): def __init__(self, media, caption=None, parse_mode=None):
self.type = "photo" self.type = "photo"
self.media = media self.media = media
self.caption = caption self.caption = caption
self.parse_mode = parse_mode
def to_json(self): def to_json(self):
return json.dumps(self.to_dic()) return json.dumps(self.to_dic())
@ -1988,17 +1993,22 @@ class InputMediaPhoto(JsonSerializable):
if not util.is_string(self.media) else self.media} 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.parse_mode:
ret['parse_mode'] = self.parse_mode
return ret return ret
class InputMediaVideo(JsonSerializable): class InputMediaVideo(JsonSerializable):
def __init__(self, media, caption=None, width=None, height=None, duration=None): def __init__(self, media, caption=None, parse_mode=None, width=None, height=None, duration=None,
supports_streaming=None):
self.type = "video" self.type = "video"
self.media = media self.media = media
self.caption = caption self.caption = caption
self.parse_mode = parse_mode
self.width = width self.width = width
self.height = height self.height = height
self.duration = duration self.duration = duration
self.supports_streaming = supports_streaming
def to_json(self): def to_json(self):
return json.dumps(self.to_dic()) return json.dumps(self.to_dic())
@ -2008,10 +2018,14 @@ class InputMediaVideo(JsonSerializable):
if not util.is_string(self.media) else self.media} 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.parse_mode:
ret['parse_mode'] = self.parse_mode
if self.width: if self.width:
ret['width'] = self.width ret['width'] = self.width
if self.height: if self.height:
ret['height'] = self.height ret['height'] = self.height
if self.duration: if self.duration:
ret['duration'] = self.duration ret['duration'] = self.duration
if self.supports_streaming:
ret['supports_streaming'] = self.supports_streaming
return ret return ret

View File

@ -420,3 +420,51 @@ class TestTeleBot:
assert len(result) == 2 assert len(result) == 2
assert result[0].media_group_id is not None assert result[0].media_group_id is not None
assert result[0].media_group_id == result[1].media_group_id assert result[0].media_group_id == result[1].media_group_id
def test_send_media_group_local_files(self):
photo = open('../examples/detailed_example/kitten.jpg', 'rb')
video = open('./test_data/test_video.mp4', 'rb')
tb = telebot.TeleBot(TOKEN)
medias = [types.InputMediaPhoto(photo, "View"),
types.InputMediaVideo(video)]
result = tb.send_media_group(CHAT_ID, medias)
assert len(result) == 2
assert result[0].media_group_id is not None
assert result[1].media_group_id is not None
def test_send_photo_formating_caption(self):
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
tb = telebot.TeleBot(TOKEN)
ret_msg = tb.send_photo(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
assert ret_msg.caption_entities[0].type == 'italic'
def test_send_video_formatting_caption(self):
file_data = open('./test_data/test_video.mp4', 'rb')
tb = telebot.TeleBot(TOKEN)
ret_msg = tb.send_video(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
assert ret_msg.caption_entities[0].type == 'italic'
def test_send_audio_formatting_caption(self):
file_data = open('./test_data/record.mp3', 'rb')
tb = telebot.TeleBot(TOKEN)
ret_msg = tb.send_audio(CHAT_ID, file_data, caption='<b>bold</b>', parse_mode='HTML')
assert ret_msg.caption_entities[0].type == 'bold'
def test_send_voice_formatting_caprion(self):
file_data = open('./test_data/record.ogg', 'rb')
tb = telebot.TeleBot(TOKEN)
ret_msg = tb.send_voice(CHAT_ID, file_data, caption='<b>bold</b>', parse_mode='HTML')
assert ret_msg.caption_entities[0].type == 'bold'
assert ret_msg.voice.mime_type == 'audio/ogg'
def test_send_media_group_formatting_caption(self):
tb = telebot.TeleBot(TOKEN)
img1 = 'https://i.imgur.com/CjXjcnU.png'
img2 = 'https://i.imgur.com/CjXjcnU.png'
medias = [types.InputMediaPhoto(img1, "*View*", parse_mode='Markdown'),
types.InputMediaPhoto(img2, "_Dog_", parse_mode='Markdown')]
result = tb.send_media_group(CHAT_ID, medias)
assert len(result) == 2
assert result[0].media_group_id is not None
assert result[0].caption_entities[0].type == 'bold'
assert result[1].caption_entities[0].type == 'italic'