Skip to content

Commit

Permalink
Apply new Telegram Bot API changes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrotoledo committed Aug 11, 2015
1 parent 163b27b commit ecdf32b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
12 changes: 12 additions & 0 deletions telegram/bot.py
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion telegram/document.py
Expand Up @@ -23,7 +23,7 @@
class Document(TelegramObject):
def __init__(self,
file_id,
thumb,
thumb=None,
file_name=None,
mime_type=None,
file_size=None):
Expand Down
12 changes: 6 additions & 6 deletions telegram/message.py
Expand Up @@ -35,6 +35,7 @@ def __init__(self,
photo=None,
sticker=None,
video=None,
caption=None,
contact=None,
location=None,
new_chat_participant=None,
Expand All @@ -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
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion telegram/sticker.py
Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions telegram/video.py
Expand Up @@ -26,18 +26,16 @@ 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
self.duration = duration
self.thumb = thumb
self.mime_type = mime_type
self.file_size = file_size
self.caption = caption

@staticmethod
def de_json(data):
Expand All @@ -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,
Expand All @@ -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
6 changes: 5 additions & 1 deletion tests/test_bot.py
Expand Up @@ -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'''
Expand Down Expand Up @@ -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'''
Expand Down Expand Up @@ -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)

0 comments on commit ecdf32b

Please sign in to comment.