mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-13 11:18:20 +01:00
Adding PhotoSize and Audio models
This commit is contained in:
parent
0b0bc3d550
commit
c4cfcaecec
5 changed files with 62 additions and 8 deletions
|
@ -11,8 +11,8 @@ from user import User
|
||||||
from message import Message
|
from message import Message
|
||||||
from update import Update
|
from update import Update
|
||||||
from groupchat import GroupChat
|
from groupchat import GroupChat
|
||||||
# from photosize import PhotoSize
|
from photosize import PhotoSize
|
||||||
# from audio import Audio
|
from audio import Audio
|
||||||
# from document import Document
|
# from document import Document
|
||||||
# from sticket import Sticker
|
# from sticket import Sticker
|
||||||
# from video import Video
|
# from video import Video
|
||||||
|
|
|
@ -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))
|
|
@ -61,6 +61,18 @@ class Message(object):
|
||||||
else:
|
else:
|
||||||
reply_to_message = None
|
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:
|
if 'new_chat_participant' in data:
|
||||||
from telegram import User
|
from telegram import User
|
||||||
new_chat_participant = User.newFromJsonDict(
|
new_chat_participant = User.newFromJsonDict(
|
||||||
|
@ -85,9 +97,9 @@ class Message(object):
|
||||||
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=data.get('text', None),
|
text=data.get('text', None),
|
||||||
audio=data.get('audio', None),
|
audio=audio,
|
||||||
document=data.get('document', None),
|
document=data.get('document', None),
|
||||||
photo=data.get('photo', None),
|
photo=photo,
|
||||||
sticker=data.get('sticker', None),
|
sticker=data.get('sticker', None),
|
||||||
video=data.get('video', None),
|
video=data.get('video', None),
|
||||||
contact=data.get('contact', None),
|
contact=data.get('contact', None),
|
||||||
|
|
|
@ -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))
|
|
@ -49,25 +49,25 @@ class BotTest(unittest.TestCase):
|
||||||
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'),
|
||||||
chat_id=12173560)
|
chat_id=12173560)
|
||||||
self.assertEqual(12948, message.photo[2].get('file_size'))
|
self.assertEqual(12948, message.photo[2].file_size)
|
||||||
|
|
||||||
def testResendPhoto(self):
|
def testResendPhoto(self):
|
||||||
'''Test the telegram.Bot sendPhoto method'''
|
'''Test the telegram.Bot sendPhoto method'''
|
||||||
print 'Testing sendPhoto - Resend'
|
print 'Testing sendPhoto - Resend'
|
||||||
message = self._bot.sendPhoto(photo=str('AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI'),
|
message = self._bot.sendPhoto(photo=str('AgAD_v___6-nMRs1PC0HuqtHTCQ9qx0AFAI'),
|
||||||
chat_id=12173560)
|
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):
|
def testSendAudio(self):
|
||||||
'''Test the telegram.Bot sendAudio method'''
|
'''Test the telegram.Bot sendAudio method'''
|
||||||
print 'Testing sendAudio - File'
|
print 'Testing sendAudio - File'
|
||||||
message = self._bot.sendAudio(audio=open('tests/telegram.ogg', 'rb'),
|
message = self._bot.sendAudio(audio=open('tests/telegram.ogg', 'rb'),
|
||||||
chat_id=12173560)
|
chat_id=12173560)
|
||||||
self.assertEqual(9199, message.audio.get('file_size'))
|
self.assertEqual(9199, message.audio.file_size)
|
||||||
|
|
||||||
def testResendAudio(self):
|
def testResendAudio(self):
|
||||||
'''Test the telegram.Bot sendAudio method'''
|
'''Test the telegram.Bot sendAudio method'''
|
||||||
print 'Testing sendAudio - Resent'
|
print 'Testing sendAudio - Resent'
|
||||||
message = self._bot.sendAudio(audio=str('AwADAQADIQEAAvjAuQABSAXg_GhkhZcC'),
|
message = self._bot.sendAudio(audio=str('AwADAQADIQEAAvjAuQABSAXg_GhkhZcC'),
|
||||||
chat_id=12173560)
|
chat_id=12173560)
|
||||||
self.assertEqual(u'AwADAQADIQEAAvjAuQABSAXg_GhkhZcC', message.audio.get('file_id'))
|
self.assertEqual(u'AwADAQADIQEAAvjAuQABSAXg_GhkhZcC', message.audio.file_id)
|
||||||
|
|
Loading…
Add table
Reference in a new issue