From b90b608fb1153fdef8763f4e9a7ead7fe71b87d3 Mon Sep 17 00:00:00 2001 From: Leandro Toledo Date: Thu, 9 Jul 2015 11:40:44 -0300 Subject: [PATCH] Add to_json method to classes --- telegram/audio.py | 15 +++++++++++ telegram/bot.py | 30 ++++++++++++++++----- telegram/contact.py | 15 +++++++++++ telegram/document.py | 17 ++++++++++++ telegram/forcereply.py | 3 +++ telegram/groupchat.py | 11 ++++++++ telegram/location.py | 11 ++++++++ telegram/message.py | 49 ++++++++++++++++++++++++++++++++++- telegram/photosize.py | 14 ++++++++++ telegram/replymarkup.py | 3 +++ telegram/sticker.py | 15 +++++++++++ telegram/update.py | 12 +++++++++ telegram/user.py | 15 +++++++++++ telegram/userprofilephotos.py | 16 ++++++++++++ telegram/video.py | 20 ++++++++++++++ tests/test_bot.py | 2 +- 16 files changed, 240 insertions(+), 8 deletions(-) diff --git a/telegram/audio.py b/telegram/audio.py index 38dbf0648..6595a099e 100644 --- a/telegram/audio.py +++ b/telegram/audio.py @@ -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() diff --git a/telegram/bot.py b/telegram/bot.py index 913aaa392..971e99bab 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -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) diff --git a/telegram/contact.py b/telegram/contact.py index b99d96ed2..fdde8230d 100644 --- a/telegram/contact.py +++ b/telegram/contact.py @@ -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() diff --git a/telegram/document.py b/telegram/document.py index 6d8f34246..f058a3e9c 100644 --- a/telegram/document.py +++ b/telegram/document.py @@ -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() diff --git a/telegram/forcereply.py b/telegram/forcereply.py index b0ff287c8..6cb5a149d 100644 --- a/telegram/forcereply.py +++ b/telegram/forcereply.py @@ -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() diff --git a/telegram/groupchat.py b/telegram/groupchat.py index f469ea320..7408aebfa 100644 --- a/telegram/groupchat.py +++ b/telegram/groupchat.py @@ -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() diff --git a/telegram/location.py b/telegram/location.py index 040e7b9c3..5148101a2 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -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() diff --git a/telegram/message.py b/telegram/message.py index 40428b6a1..8c5f74daf 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -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() diff --git a/telegram/photosize.py b/telegram/photosize.py index 8db31b04f..174f9d650 100644 --- a/telegram/photosize.py +++ b/telegram/photosize.py @@ -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() diff --git a/telegram/replymarkup.py b/telegram/replymarkup.py index 9d65d80d3..b582a5e66 100644 --- a/telegram/replymarkup.py +++ b/telegram/replymarkup.py @@ -4,3 +4,6 @@ class ReplyMarkup(object): def to_json(self): raise NotImplementedError + + def __str__(self): + return self.to_json() diff --git a/telegram/sticker.py b/telegram/sticker.py index 4f169f51b..84464bf17 100644 --- a/telegram/sticker.py +++ b/telegram/sticker.py @@ -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() diff --git a/telegram/update.py b/telegram/update.py index 7e63a77aa..2c7cfb675 100644 --- a/telegram/update.py +++ b/telegram/update.py @@ -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() diff --git a/telegram/user.py b/telegram/user.py index e463101f5..50f3e98ef 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -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() diff --git a/telegram/userprofilephotos.py b/telegram/userprofilephotos.py index 30ae3218c..d304ff11a 100644 --- a/telegram/userprofilephotos.py +++ b/telegram/userprofilephotos.py @@ -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() diff --git a/telegram/video.py b/telegram/video.py index a08d09fca..fd589e2d5 100644 --- a/telegram/video.py +++ b/telegram/video.py @@ -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() diff --git a/tests/test_bot.py b/tests/test_bot.py index ed865370b..01cb4b7aa 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -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'''