diff --git a/telegram/__init__.py b/telegram/__init__.py index 90b280105..82e543c73 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -15,7 +15,7 @@ from photosize import PhotoSize from audio import Audio from document import Document from sticker import Sticker -# from video import Video +from video import Video # from contact import Contact # from location import Location # from inputfile import InputFile diff --git a/telegram/bot.py b/telegram/bot.py index 1b0657c65..03780c089 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -239,6 +239,24 @@ class Bot(object): sticker, reply_to_message_id=None, reply_markup=None): + """Use this method to send .webp stickers. + + Args: + chat_id: + Unique identifier for the message recipient — User or GroupChat id. + sticker: + Sticker to send. You can either pass a file_id as String to resend + a sticker that is already on the Telegram servers, or upload a new + sticker using multipart/form-data. + reply_to_message_id: + If the message is a reply, ID of the original message. [Optional] + reply_markup: + Additional interface options. A JSON-serialized object for a + custom reply keyboard, instructions to hide keyboard or to force a + reply from the user. [Optional] + Returns: + A telegram.Message instance representing the message posted. + """ url = '%s/sendSticker' % (self.base_url) @@ -255,9 +273,46 @@ class Bot(object): return Message.newFromJsonDict(data) - def sendVideo(self): + def sendVideo(self, + chat_id, + video, + reply_to_message_id=None, + reply_markup=None): + """Use this method to send video files, Telegram clients support mp4 + videos (other formats may be sent as telegram.Document). + + Args: + chat_id: + Unique identifier for the message recipient — User or GroupChat id. + video: + 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. + reply_to_message_id: + If the message is a reply, ID of the original message. [Optional] + reply_markup: + Additional interface options. A JSON-serialized object for a + custom reply keyboard, instructions to hide keyboard or to force a + reply from the user. [Optional] + Returns: + A telegram.Message instance representing the message posted. + """ + url = '%s/sendVideo' % (self.base_url) + data = {'chat_id': chat_id, + 'video': video} + + if reply_to_message_id: + data['reply_to_message_id'] = reply_to_message_id + if reply_markup: + data['reply_markup'] = reply_markup + + json_data = self._requestUrl(url, 'POST', data=data) + data = self._parseAndCheckTelegram(json_data.content) + + return Message.newFromJsonDict(data) + def sendLocation(self): url = '%s/sendLocation' % (self.base_url) diff --git a/telegram/groupchat.py b/telegram/groupchat.py index d1a1a258a..295e68932 100644 --- a/telegram/groupchat.py +++ b/telegram/groupchat.py @@ -11,6 +11,10 @@ class GroupChat(object): for (param, default) in param_defaults.iteritems(): setattr(self, param, kwargs.get(param, default)) + @property + def chat_id(self): + return self.id + @staticmethod def newFromJsonDict(data): return GroupChat(id=data.get('id', None), diff --git a/telegram/message.py b/telegram/message.py index 3c21419e8..1573df6e3 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -85,6 +85,12 @@ class Message(object): else: sticker = None + if 'video' in data: + from telegram import Video + video = Video.newFromJsonDict(data['video']) + else: + video = None + if 'new_chat_participant' in data: from telegram import User new_chat_participant = User.newFromJsonDict( @@ -113,7 +119,7 @@ class Message(object): document=document, photo=photo, sticker=sticker, - video=data.get('video', None), + video=video, contact=data.get('contact', None), location=data.get('location', None), new_chat_participant=new_chat_participant, diff --git a/telegram/user.py b/telegram/user.py index 4864a3ac1..389e4259a 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -5,7 +5,6 @@ class User(object): def __init__(self, **kwargs): param_defaults = { 'id': None, - # 'chat_id' TODO 'first_name': None, 'last_name': None, 'username': None @@ -14,6 +13,10 @@ class User(object): for (param, default) in param_defaults.iteritems(): setattr(self, param, kwargs.get(param, default)) + @property + def chat_id(self): + return self.id + @staticmethod def newFromJsonDict(data): return User(id=data.get('id', None), diff --git a/telegram/video.py b/telegram/video.py index e69de29bb..6204cdb49 100644 --- a/telegram/video.py +++ b/telegram/video.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + + +class Video(object): + def __init__(self, **kwargs): + param_defaults = { + 'file_id': None, + 'width': None, + 'height': None, + 'duration': None, + 'thumb': None, + 'mime_type': None, + 'file_size': None, + 'caption': None + } + + for (param, default) in param_defaults.iteritems(): + setattr(self, param, kwargs.get(param, default)) + + @staticmethod + def newFromJsonDict(data): + if 'thumb' in data: + from telegram import PhotoSize + thumb = PhotoSize.newFromJsonDict(data['thumb']) + else: + thumb = None + + return Video(file_id=data.get('file_id', None), + width=data.get('width', None), + height=data.get('height', None), + 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)) diff --git a/tests/test_bot.py b/tests/test_bot.py index bc22ef501..d38949648 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -33,7 +33,7 @@ class BotTest(unittest.TestCase): '''Test the telegram.Bot getUpdates method''' print 'Testing getUpdates' updates = self._bot.getUpdates() - self.assertEqual(129566481, updates[0].update_id) + self.assertEqual(129566520, updates[0].update_id) def testForwardMessage(self): '''Test the telegram.Bot forwardMessage method''' @@ -86,9 +86,16 @@ class BotTest(unittest.TestCase): chat_id=12173560) self.assertEqual(u'BQADAQADHAADNTwtBxZxUGKyxYbYAg', message.document.file_id) - def testSendSticker(self): + def testResendSticker(self): '''Test the telegram.Bot sendSticker method''' - print 'Testing sendSticket' + print 'Testing sendSticker - Resend' message = self._bot.sendSticker(sticker=str('BQADAQADHAADyIsGAAFZfq1bphjqlgI'), chat_id=12173560) self.assertEqual(39518, message.sticker.file_size) + + def testResendVideo(self): + '''Test the telegram.Bot sendVideo method''' + print 'Testing sendVideo - Resend' + message = self._bot.sendVideo(video=str('BAADAQADIgEAAvjAuQABOuTB937fPTgC'), + chat_id=12173560) + self.assertEqual(4, message.video.duration)