Apply new Telegram Bot API changes #32

This commit is contained in:
Leandro Toledo 2015-08-11 17:32:06 -03:00
parent 163b27b7dd
commit ecdf32b5f6
6 changed files with 28 additions and 17 deletions

View file

@ -370,6 +370,8 @@ class Bot(TelegramObject):
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 @@ class Bot(TelegramObject):
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 @@ class Bot(TelegramObject):
data = {'chat_id': chat_id,
'video': video}
if duration:
data['duration'] = duration
if caption:
data['caption'] = caption
return url, data
@log

View file

@ -23,7 +23,7 @@ from telegram import TelegramObject
class Document(TelegramObject):
def __init__(self,
file_id,
thumb,
thumb=None,
file_name=None,
mime_type=None,
file_size=None):

View file

@ -35,6 +35,7 @@ class Message(TelegramObject):
photo=None,
sticker=None,
video=None,
caption=None,
contact=None,
location=None,
new_chat_participant=None,
@ -56,6 +57,7 @@ class Message(TelegramObject):
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 @@ class Message(TelegramObject):
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 @@ class Message(TelegramObject):
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 @@ class Message(TelegramObject):
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:

View file

@ -25,7 +25,7 @@ class Sticker(TelegramObject):
file_id,
width,
height,
thumb,
thumb=None,
file_size=None):
self.file_id = file_id
self.width = width

View file

@ -26,10 +26,9 @@ class Video(TelegramObject):
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 @@ class Video(TelegramObject):
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 @@ class Video(TelegramObject):
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 @@ class Video(TelegramObject):
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

View file

@ -64,8 +64,10 @@ class BotTest(unittest.TestCase):
'''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 @@ class BotTest(unittest.TestCase):
'''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 @@ class BotTest(unittest.TestCase):
'''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)