From ecdf32b5f6683bbee5e45c354c64818dd4b356d4 Mon Sep 17 00:00:00 2001 From: Leandro Toledo Date: Tue, 11 Aug 2015 17:32:06 -0300 Subject: [PATCH] Apply new Telegram Bot API changes #32 --- telegram/bot.py | 12 ++++++++++++ telegram/document.py | 2 +- telegram/message.py | 12 ++++++------ telegram/sticker.py | 2 +- telegram/video.py | 11 +++-------- tests/test_bot.py | 6 +++++- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index 8972e06a245..6c222a099b7 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -370,6 +370,8 @@ def sendSticker(self, def sendVideo(self, chat_id, video, + duration=None, + caption=None, reply_to_message_id=None, reply_markup=None): """Use this method to send video files, Telegram clients support mp4 @@ -382,6 +384,11 @@ def sendVideo(self, Video to send. You can either pass a file_id as String to resend a video that is already on the Telegram servers, or upload a new video file using multipart/form-data. + duration: + Duration of sent video in seconds. [Optional] + caption: + Video caption (may also be used when resending videos by file_id). + [Optional] reply_to_message_id: If the message is a reply, ID of the original message. [Optional] reply_markup: @@ -398,6 +405,11 @@ def sendVideo(self, data = {'chat_id': chat_id, 'video': video} + if duration: + data['duration'] = duration + if caption: + data['caption'] = caption + return url, data @log diff --git a/telegram/document.py b/telegram/document.py index 7ee842f605f..23921c799a9 100644 --- a/telegram/document.py +++ b/telegram/document.py @@ -23,7 +23,7 @@ class Document(TelegramObject): def __init__(self, file_id, - thumb, + thumb=None, file_name=None, mime_type=None, file_size=None): diff --git a/telegram/message.py b/telegram/message.py index 40cb2cdb20c..33762c931ee 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -35,6 +35,7 @@ def __init__(self, photo=None, sticker=None, video=None, + caption=None, contact=None, location=None, new_chat_participant=None, @@ -56,6 +57,7 @@ def __init__(self, self.photo = photo self.sticker = sticker self.video = video + self.caption = caption self.contact = contact self.location = location self.new_chat_participant = new_chat_participant @@ -98,11 +100,6 @@ def de_json(data): else: reply_to_message = None - if 'text' in data: - text = data['text'] - else: - text = '' - if 'audio' in data: from telegram import Audio audio = Audio.de_json(data['audio']) @@ -164,12 +161,13 @@ def de_json(data): forward_from=forward_from, forward_date=data.get('forward_date', None), reply_to_message=reply_to_message, - text=text, + text=data.get('text', ''), audio=audio, document=document, photo=photo, sticker=sticker, video=video, + caption=data.get('caption', ''), contact=contact, location=location, new_chat_participant=new_chat_participant, @@ -202,6 +200,8 @@ def to_dict(self): data['sticker'] = self.sticker.to_dict() if self.video: data['video'] = self.video.to_dict() + if self.caption: + data['caption'] = self.caption if self.contact: data['contact'] = self.contact.to_dict() if self.location: diff --git a/telegram/sticker.py b/telegram/sticker.py index bdf9f6da8b3..eae52d25f53 100644 --- a/telegram/sticker.py +++ b/telegram/sticker.py @@ -25,7 +25,7 @@ def __init__(self, file_id, width, height, - thumb, + thumb=None, file_size=None): self.file_id = file_id self.width = width diff --git a/telegram/video.py b/telegram/video.py index 376da25ba0f..a75c4a93162 100644 --- a/telegram/video.py +++ b/telegram/video.py @@ -26,10 +26,9 @@ def __init__(self, width, height, duration, - thumb, + thumb=None, mime_type=None, - file_size=None, - caption=None): + file_size=None): self.file_id = file_id self.width = width self.height = height @@ -37,7 +36,6 @@ def __init__(self, self.thumb = thumb self.mime_type = mime_type self.file_size = file_size - self.caption = caption @staticmethod def de_json(data): @@ -53,8 +51,7 @@ def de_json(data): duration=data.get('duration', None), thumb=thumb, mime_type=data.get('mime_type', None), - file_size=data.get('file_size', None), - caption=data.get('caption', None)) + file_size=data.get('file_size', None)) def to_dict(self): data = {'file_id': self.file_id, @@ -67,6 +64,4 @@ def to_dict(self): data['mime_type'] = self.mime_type if self.file_size: data['file_size'] = self.file_size - if self.caption: - data['caption'] = self.caption return data diff --git a/tests/test_bot.py b/tests/test_bot.py index 016a7a53b46..b5627b03986 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -64,8 +64,10 @@ def testSendPhoto(self): '''Test the telegram.Bot sendPhoto method''' print('Testing sendPhoto - File') message = self._bot.sendPhoto(photo=open('tests/telegram.png', 'rb'), + caption='testSendPhoto', chat_id=12173560) self.assertEqual(1451, message.photo[0].file_size) + self.assertEqual('testSendPhoto', message.caption) def testResendPhoto(self): '''Test the telegram.Bot sendPhoto method''' @@ -113,8 +115,10 @@ def testSendVideo(self): '''Test the telegram.Bot sendVideo method''' print('Testing sendVideo - File') message = self._bot.sendVideo(video=open('tests/telegram.mp4', 'rb'), + caption='testSendVideo', chat_id=12173560) self.assertEqual(326534, message.video.file_size) + self.assertEqual('testSendVideo', message.caption) def testResendVideo(self): '''Test the telegram.Bot sendVideo method''' @@ -149,4 +153,4 @@ def testGetUserProfilePhotos(self): '''Test the telegram.Bot getUserProfilePhotos method''' print('Testing getUserProfilePhotos') upf = self._bot.getUserProfilePhotos(user_id=12173560) - self.assertEqual(8314, upf.photos[0][0].file_size) + self.assertEqual(6547, upf.photos[0][0].file_size)