Add to_json method to classes

This commit is contained in:
Leandro Toledo 2015-07-09 11:40:44 -03:00
parent 73cbd11307
commit b90b608fb1
16 changed files with 240 additions and 8 deletions

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class Audio(object):
def __init__(self,
file_id,
@ -18,3 +21,15 @@ class Audio(object):
duration=data.get('duration', None),
mime_type=data.get('mime_type', None),
file_size=data.get('file_size', None))
def to_json(self):
json_data = {'file_id': self.file_id,
'duration': self.duration}
if self.mime_type:
json_data['mime_type'] = self.mime_type
if self.file_size:
json_data['file_size'] = self.file_size
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -197,7 +197,10 @@ class Bot(object):
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
data['reply_markup'] = reply_markup
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
@ -243,7 +246,10 @@ class Bot(object):
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
data['reply_markup'] = reply_markup
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
@ -286,7 +292,10 @@ class Bot(object):
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
data['reply_markup'] = reply_markup
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
@ -329,7 +338,10 @@ class Bot(object):
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
data['reply_markup'] = reply_markup
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
@ -373,7 +385,10 @@ class Bot(object):
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
data['reply_markup'] = reply_markup
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
@ -418,7 +433,10 @@ class Bot(object):
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
data['reply_markup'] = reply_markup
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class Contact(object):
def __init__(self,
phone_number,
@ -18,3 +21,15 @@ class Contact(object):
first_name=data.get('first_name', None),
last_name=data.get('last_name', None),
user_id=data.get('user_id', None))
def to_json(self):
json_data = {'phone_number': self.phone_number,
'first_name': self.first_name}
if self.last_name:
json_data['last_name'] = self.last_name
if self.user_id:
json_data['user_id'] = self.user_id
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class Document(object):
def __init__(self,
file_id,
@ -27,3 +30,17 @@ class Document(object):
file_name=data.get('file_name', None),
mime_type=data.get('mime_type', None),
file_size=data.get('file_size', None))
def to_json(self):
json_data = {'file_id': self.file_id,
'thumb': self.thumb.to_json()}
if self.file_name:
json_data['file_name'] = self.file_name
if self.mime_type:
json_data['mime_type'] = self.mime_type
if self.file_size:
json_data['file_size'] = self.file_size
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -22,3 +22,6 @@ class ForceReply(ReplyMarkup):
if self.selective:
json_data['selective'] = self.selective
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class GroupChat(object):
def __init__(self,
id,
@ -12,3 +15,11 @@ class GroupChat(object):
def de_json(data):
return GroupChat(id=data.get('id', None),
title=data.get('title', None))
def to_json(self):
json_data = {'id': self.id,
'title': self.title}
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class Location(object):
def __init__(self,
longitude,
@ -12,3 +15,11 @@ class Location(object):
def de_json(data):
return Location(longitude=data.get('longitude', None),
latitude=data.get('latitude', None))
def to_json(self):
json_data = {'longitude': self.longitude,
'latitude': self.latitude}
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class Message(object):
def __init__(self,
message_id,
@ -52,7 +55,7 @@ class Message(object):
@staticmethod
def de_json(data):
if 'from' in data: # from is a reserved word, use user_from instead.
if 'from' in data: # from is a reserved word, use from_user instead.
from telegram import User
from_user = User.de_json(data['from'])
else:
@ -154,3 +157,47 @@ class Message(object):
new_chat_photo=data.get('new_chat_photo', None),
delete_chat_photo=data.get('delete_chat_photo', None),
group_chat_created=data.get('group_chat_created', None))
def to_json(self):
json_data = {'message_id': self.message_id,
'from': self.from_user.to_json(),
'date': self.date,
'chat': self.chat.to_json()}
if self.forward_from:
json_data['forward_from'] = self.forward_from
if self.forward_date:
json_data['forward_date'] = self.forward_date
if self.reply_to_message:
json_data['reply_to_message'] = self.reply_to_message
if self.text:
json_data['text'] = self.text
if self.audio:
json_data['audio'] = self.audio.to_json()
if self.document:
json_data['document'] = self.document.to_json()
if self.photo:
json_data['photo'] = self.photo.to_json()
if self.sticker:
json_data['sticker'] = self.sticker.to_json()
if self.video:
json_data['video'] = self.video.to_json()
if self.contact:
json_data['contact'] = self.contact.to_json()
if self.location:
json_data['location'] = self.location.to_json()
if self.new_chat_participant:
json_data['new_chat_participant'] = self.new_chat_participant
if self.left_chat_participant:
json_data['left_chat_participant'] = self.left_chat_participant
if self.new_chat_title:
json_data['new_chat_title'] = self.new_chat_title
if self.new_chat_photo:
json_data['new_chat_photo'] = self.new_chat_photo
if self.delete_chat_photo:
json_data['delete_chat_photo'] = self.delete_chat_photo
if self.group_chat_created:
json_data['group_chat_created'] = self.group_chat_created
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class PhotoSize(object):
def __init__(self,
file_id,
@ -18,3 +21,14 @@ class PhotoSize(object):
width=data.get('width', None),
height=data.get('height', None),
file_size=data.get('file_size', None))
def to_json(self):
json_data = {'file_id': self.file_id,
'width': self.width,
'height': self.height}
if self.file_size:
json_data['file_size'] = self.file_size
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -4,3 +4,6 @@
class ReplyMarkup(object):
def to_json(self):
raise NotImplementedError
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class Sticker(object):
def __init__(self,
file_id,
@ -27,3 +30,15 @@ class Sticker(object):
height=data.get('height', None),
thumb=thumb,
file_size=data.get('file_size', None))
def to_json(self):
json_data = {'file_id': self.file_id,
'width': self.width,
'height': self.height,
'thumb': self.thumb.to_json()}
if self.file_size:
json_data['file_size'] = self.file_size
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class Update(object):
def __init__(self,
update_id,
@ -18,3 +21,12 @@ class Update(object):
return Update(update_id=data.get('update_id', None),
message=message)
def to_json(self):
json_data = {'update_id': self.update_id}
if self.message:
json_data['message'] = self.message.to_json()
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class User(object):
def __init__(self,
id,
@ -18,3 +21,15 @@ class User(object):
first_name=data.get('first_name', None),
last_name=data.get('last_name', None),
username=data.get('username', None))
def to_json(self):
json_data = {'id': self.id,
'first_name': self.first_name}
if self.last_name:
json_data['last_name'] = self.last_name
if self.username:
json_data['username'] = self.username
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class UserProfilePhotos(object):
def __init__(self,
total_count,
@ -20,3 +23,16 @@ class UserProfilePhotos(object):
return UserProfilePhotos(total_count=data.get('total_count', None),
photos=photos)
def to_json(self):
json_data = {}
if self.total_count:
json_data['total_count'] = self.total_count
if self.photos:
json_data['photos'] = []
for photo in self.photos:
json_data['photos'].append([x.to_json() for x in photo])
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,6 +1,9 @@
#!/usr/bin/env python
import json
class Video(object):
def __init__(self,
file_id,
@ -36,3 +39,20 @@ class Video(object):
mime_type=data.get('mime_type', None),
file_size=data.get('file_size', None),
caption=data.get('caption', None))
def to_json(self):
json_data = {'file_id': self.file_id,
'width': self.width,
'height': self.height,
'duration': self.duration,
'thumb': self.thumb.to_json()}
if self.mime_type:
json_data['mime_type'] = self.mime_type
if self.file_size:
json_data['file_size'] = self.file_size
if self.caption:
json_data['caption'] = self.caption
return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -33,7 +33,7 @@ class BotTest(unittest.TestCase):
'''Test the telegram.Bot getUpdates method'''
print 'Testing getUpdates'
updates = self._bot.getUpdates()
self.assertEqual(129566524, updates[0].update_id)
self.assertEqual(129566562, updates[0].update_id)
def testForwardMessage(self):
'''Test the telegram.Bot forwardMessage method'''