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, def sendVideo(self,
chat_id, chat_id,
video, video,
duration=None,
caption=None,
reply_to_message_id=None, reply_to_message_id=None,
reply_markup=None): reply_markup=None):
"""Use this method to send video files, Telegram clients support mp4 """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 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 that is already on the Telegram servers, or upload a new
video file using multipart/form-data. 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: reply_to_message_id:
If the message is a reply, ID of the original message. [Optional] If the message is a reply, ID of the original message. [Optional]
reply_markup: reply_markup:
@ -398,6 +405,11 @@ class Bot(TelegramObject):
data = {'chat_id': chat_id, data = {'chat_id': chat_id,
'video': video} 'video': video}
if duration:
data['duration'] = duration
if caption:
data['caption'] = caption
return url, data return url, data
@log @log

View file

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

View file

@ -35,6 +35,7 @@ class Message(TelegramObject):
photo=None, photo=None,
sticker=None, sticker=None,
video=None, video=None,
caption=None,
contact=None, contact=None,
location=None, location=None,
new_chat_participant=None, new_chat_participant=None,
@ -56,6 +57,7 @@ class Message(TelegramObject):
self.photo = photo self.photo = photo
self.sticker = sticker self.sticker = sticker
self.video = video self.video = video
self.caption = caption
self.contact = contact self.contact = contact
self.location = location self.location = location
self.new_chat_participant = new_chat_participant self.new_chat_participant = new_chat_participant
@ -98,11 +100,6 @@ class Message(TelegramObject):
else: else:
reply_to_message = None reply_to_message = None
if 'text' in data:
text = data['text']
else:
text = ''
if 'audio' in data: if 'audio' in data:
from telegram import Audio from telegram import Audio
audio = Audio.de_json(data['audio']) audio = Audio.de_json(data['audio'])
@ -164,12 +161,13 @@ class Message(TelegramObject):
forward_from=forward_from, forward_from=forward_from,
forward_date=data.get('forward_date', None), forward_date=data.get('forward_date', None),
reply_to_message=reply_to_message, reply_to_message=reply_to_message,
text=text, text=data.get('text', ''),
audio=audio, audio=audio,
document=document, document=document,
photo=photo, photo=photo,
sticker=sticker, sticker=sticker,
video=video, video=video,
caption=data.get('caption', ''),
contact=contact, contact=contact,
location=location, location=location,
new_chat_participant=new_chat_participant, new_chat_participant=new_chat_participant,
@ -202,6 +200,8 @@ class Message(TelegramObject):
data['sticker'] = self.sticker.to_dict() data['sticker'] = self.sticker.to_dict()
if self.video: if self.video:
data['video'] = self.video.to_dict() data['video'] = self.video.to_dict()
if self.caption:
data['caption'] = self.caption
if self.contact: if self.contact:
data['contact'] = self.contact.to_dict() data['contact'] = self.contact.to_dict()
if self.location: if self.location:

View file

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

View file

@ -26,10 +26,9 @@ class Video(TelegramObject):
width, width,
height, height,
duration, duration,
thumb, thumb=None,
mime_type=None, mime_type=None,
file_size=None, file_size=None):
caption=None):
self.file_id = file_id self.file_id = file_id
self.width = width self.width = width
self.height = height self.height = height
@ -37,7 +36,6 @@ class Video(TelegramObject):
self.thumb = thumb self.thumb = thumb
self.mime_type = mime_type self.mime_type = mime_type
self.file_size = file_size self.file_size = file_size
self.caption = caption
@staticmethod @staticmethod
def de_json(data): def de_json(data):
@ -53,8 +51,7 @@ class Video(TelegramObject):
duration=data.get('duration', None), duration=data.get('duration', None),
thumb=thumb, thumb=thumb,
mime_type=data.get('mime_type', None), mime_type=data.get('mime_type', None),
file_size=data.get('file_size', None), file_size=data.get('file_size', None))
caption=data.get('caption', None))
def to_dict(self): def to_dict(self):
data = {'file_id': self.file_id, data = {'file_id': self.file_id,
@ -67,6 +64,4 @@ class Video(TelegramObject):
data['mime_type'] = self.mime_type data['mime_type'] = self.mime_type
if self.file_size: if self.file_size:
data['file_size'] = self.file_size data['file_size'] = self.file_size
if self.caption:
data['caption'] = self.caption
return data return data

View file

@ -64,8 +64,10 @@ class BotTest(unittest.TestCase):
'''Test the telegram.Bot sendPhoto method''' '''Test the telegram.Bot sendPhoto method'''
print('Testing sendPhoto - File') print('Testing sendPhoto - File')
message = self._bot.sendPhoto(photo=open('tests/telegram.png', 'rb'), message = self._bot.sendPhoto(photo=open('tests/telegram.png', 'rb'),
caption='testSendPhoto',
chat_id=12173560) chat_id=12173560)
self.assertEqual(1451, message.photo[0].file_size) self.assertEqual(1451, message.photo[0].file_size)
self.assertEqual('testSendPhoto', message.caption)
def testResendPhoto(self): def testResendPhoto(self):
'''Test the telegram.Bot sendPhoto method''' '''Test the telegram.Bot sendPhoto method'''
@ -113,8 +115,10 @@ class BotTest(unittest.TestCase):
'''Test the telegram.Bot sendVideo method''' '''Test the telegram.Bot sendVideo method'''
print('Testing sendVideo - File') print('Testing sendVideo - File')
message = self._bot.sendVideo(video=open('tests/telegram.mp4', 'rb'), message = self._bot.sendVideo(video=open('tests/telegram.mp4', 'rb'),
caption='testSendVideo',
chat_id=12173560) chat_id=12173560)
self.assertEqual(326534, message.video.file_size) self.assertEqual(326534, message.video.file_size)
self.assertEqual('testSendVideo', message.caption)
def testResendVideo(self): def testResendVideo(self):
'''Test the telegram.Bot sendVideo method''' '''Test the telegram.Bot sendVideo method'''
@ -149,4 +153,4 @@ class BotTest(unittest.TestCase):
'''Test the telegram.Bot getUserProfilePhotos method''' '''Test the telegram.Bot getUserProfilePhotos method'''
print('Testing getUserProfilePhotos') print('Testing getUserProfilePhotos')
upf = self._bot.getUserProfilePhotos(user_id=12173560) 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)