Adding sendVideo and minor fixes

This commit is contained in:
Leandro Toledo 2015-07-08 09:17:18 -03:00
parent c3ea91e68c
commit 5963649c9a
7 changed files with 117 additions and 7 deletions

View file

@ -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

View file

@ -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)

View file

@ -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),

View file

@ -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,

View file

@ -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),

View file

@ -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))

View file

@ -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)