Adding PhotoSize and Audio models

This commit is contained in:
Leandro Toledo 2015-07-07 20:52:57 -03:00
parent 0b0bc3d550
commit c4cfcaecec
5 changed files with 62 additions and 8 deletions

View file

@ -11,8 +11,8 @@ from user import User
from message import Message
from update import Update
from groupchat import GroupChat
# from photosize import PhotoSize
# from audio import Audio
from photosize import PhotoSize
from audio import Audio
# from document import Document
# from sticket import Sticker
# from video import Video

View file

@ -0,0 +1,21 @@
#!/usr/bin/env python
class Audio(object):
def __init__(self, **kwargs):
param_defaults = {
'file_id': None,
'duration': None,
'mime_type': None,
'file_size': None
}
for (param, default) in param_defaults.iteritems():
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
return Audio(file_id=data.get('file_id', None),
duration=data.get('duration', None),
mime_type=data.get('mime_type', None),
file_size=data.get('file_size', None))

View file

@ -61,6 +61,18 @@ class Message(object):
else:
reply_to_message = None
if 'audio' in data:
from telegram import Audio
audio = Audio.newFromJsonDict(data['audio'])
else:
audio = None
if 'photo' in data:
from telegram import PhotoSize
photo = [PhotoSize.newFromJsonDict(x) for x in data['photo']]
else:
photo = None
if 'new_chat_participant' in data:
from telegram import User
new_chat_participant = User.newFromJsonDict(
@ -85,9 +97,9 @@ class Message(object):
forward_date=data.get('forward_date', None),
reply_to_message=reply_to_message,
text=data.get('text', None),
audio=data.get('audio', None),
audio=audio,
document=data.get('document', None),
photo=data.get('photo', None),
photo=photo,
sticker=data.get('sticker', None),
video=data.get('video', None),
contact=data.get('contact', None),

View file

@ -0,0 +1,21 @@
#!/usr/bin/env python
class PhotoSize(object):
def __init__(self, **kwargs):
param_defaults = {
'file_id': None,
'width': None,
'height': None,
'file_size': None
}
for (param, default) in param_defaults.iteritems():
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
return PhotoSize(file_id=data.get('file_id', None),
width=data.get('width', None),
height=data.get('height', None),
file_size=data.get('file_size', None))

View file

@ -49,25 +49,25 @@ class BotTest(unittest.TestCase):
print 'Testing sendPhoto - File'
message = self._bot.sendPhoto(photo=open('tests/telegram.png', 'rb'),
chat_id=12173560)
self.assertEqual(12948, message.photo[2].get('file_size'))
self.assertEqual(12948, message.photo[2].file_size)
def testResendPhoto(self):
'''Test the telegram.Bot sendPhoto method'''
print 'Testing sendPhoto - Resend'
message = self._bot.sendPhoto(photo=str('AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI'),
chat_id=12173560)
self.assertEqual(u'AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI', message.photo[2].get('file_id'))
self.assertEqual(u'AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI', message.photo[2].file_id)
def testSendAudio(self):
'''Test the telegram.Bot sendAudio method'''
print 'Testing sendAudio - File'
message = self._bot.sendAudio(audio=open('tests/telegram.ogg', 'rb'),
chat_id=12173560)
self.assertEqual(9199, message.audio.get('file_size'))
self.assertEqual(9199, message.audio.file_size)
def testResendAudio(self):
'''Test the telegram.Bot sendAudio method'''
print 'Testing sendAudio - Resent'
message = self._bot.sendAudio(audio=str('AwADAQADIQEAAvjAuQABSAXg_GhkhZcC'),
chat_id=12173560)
self.assertEqual(u'AwADAQADIQEAAvjAuQABSAXg_GhkhZcC', message.audio.get('file_id'))
self.assertEqual(u'AwADAQADIQEAAvjAuQABSAXg_GhkhZcC', message.audio.file_id)