diff --git a/telebot/__init__.py b/telebot/__init__.py index 8cb2f49..d140ccf 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -268,17 +268,19 @@ class TeleBot: return types.Message.de_json( apihelper.send_data(self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup)) - def send_video(self, chat_id, data, 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): """ Use this method to send video files, Telegram clients support mp4 videos. - :param chat_id: - :param data: + :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 duration: Integer : Duration of sent video in seconds + :param caption: String : Video caption (may also be used when resending videos by file_id). :param reply_to_message_id: :param reply_markup: - :return: API reply. + :return: """ return types.Message.de_json( - apihelper.send_data(self.token, chat_id, data, 'video', reply_to_message_id, reply_markup)) + apihelper.send_video(self.token, chat_id, data, duration, caption, reply_to_message_id, reply_markup)) def send_location(self, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None): """ diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 655b881..afdc63b 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -120,6 +120,25 @@ def send_chat_action(token, chat_id, action): return _make_request(token, method_url, params=payload) +def send_video(token, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None): + method_url = r'sendVideo' + payload = {'chat_id': chat_id} + files = None + if not is_string(data): + files = {'video': data} + else: + payload['video'] = data + if duration: + payload['duration'] = duration + if caption: + payload['caption'] = caption + 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} @@ -142,8 +161,6 @@ def get_method_by_type(data_type): return 'sendDocument' if data_type == 'sticker': return 'sendSticker' - if data_type == 'video': - return 'sendVideo' def _convert_markup(markup): @@ -155,6 +172,7 @@ def _convert_markup(markup): def is_string(var): return isinstance(var, string_types) + def is_command(text): """ Checks if `text` is a command. Telegram chat commands start with the '/' character. @@ -163,6 +181,7 @@ def is_command(text): """ return text.startswith('/') + def extract_command(text): """ Extracts the command from `text` (minus the '/') if `text` is a command (see is_command). @@ -191,6 +210,7 @@ def split_string(text, chars_per_string): """ return [text[i:i + chars_per_string] for i in range(0, len(text), chars_per_string)] + class ApiException(Exception): """ This class represents an Exception thrown when a call to the Telegram API fails. diff --git a/tests/test_data/test_video.mp4 b/tests/test_data/test_video.mp4 new file mode 100644 index 0000000..f01fe18 Binary files /dev/null and b/tests/test_data/test_video.mp4 differ diff --git a/tests/test_telebot.py b/tests/test_telebot.py index ce8298c..5256f5d 100644 --- a/tests/test_telebot.py +++ b/tests/test_telebot.py @@ -77,6 +77,20 @@ def test_send_file(): assert ret_msg.message_id +def test_send_video(): + file_data = open('./test_data/test_video.mp4', 'rb') + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_video(CHAT_ID, file_data) + assert ret_msg.message_id + + +def test_send_video_more_params(): + file_data = open('./test_data/test_video.mp4', 'rb') + tb = telebot.TeleBot(TOKEN) + ret_msg = tb.send_video(CHAT_ID, file_data, 1) + assert ret_msg.message_id + + def test_send_file_exception(): tb = telebot.TeleBot(TOKEN) try: @@ -149,7 +163,7 @@ def test_send_location(): def create_text_message(text): params = {'text': text} - chat = types.User(11,'test') + chat = types.User(11, 'test') return types.Message(1, None, None, chat, 'text', params)