diff --git a/telegram/__init__.py b/telegram/__init__.py index bde7294f5..5f510f125 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -18,17 +18,18 @@ from .contact import Contact from .location import Location from .chataction import ChatAction from .userprofilephotos import UserProfilePhotos +from .replymarkup import ReplyMarkup from .replykeyboardmarkup import ReplyKeyboardMarkup from .replykeyboardhide import ReplyKeyboardHide from .forcereply import ForceReply -from .replymarkup import ReplyMarkup from .inputfile import InputFile from .error import TelegramError from .emoji import Emoji +from .base import TelegramObject from .bot import Bot __all__ = ['Bot', 'Emoji', 'TelegramError', 'InputFile', 'ReplyMarkup', 'ForceReply', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup', 'UserProfilePhotos', 'ChatAction', 'Location', 'Contact', 'Video', 'Sticker', 'Document', 'Audio', 'PhotoSize', 'GroupChat', - 'Update', 'Message', 'User'] + 'Update', 'Message', 'User', 'TelegramObject'] diff --git a/telegram/audio.py b/telegram/audio.py index 17ba6c184..926051d92 100644 --- a/telegram/audio.py +++ b/telegram/audio.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class Audio(Base): +class Audio(TelegramObject): def __init__(self, file_id, duration, @@ -23,11 +22,11 @@ class Audio(Base): 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} + def to_data(self): + data = {'file_id': self.file_id, + 'duration': self.duration} if self.mime_type: - json_data['mime_type'] = self.mime_type + data['mime_type'] = self.mime_type if self.file_size: - json_data['file_size'] = self.file_size - return json.dumps(json_data) + data['file_size'] = self.file_size + return data diff --git a/telegram/telegram_boject_base.py b/telegram/base.py similarity index 51% rename from telegram/telegram_boject_base.py rename to telegram/base.py index 67f788dae..cc7ac6d35 100644 --- a/telegram/telegram_boject_base.py +++ b/telegram/base.py @@ -1,26 +1,28 @@ +#!/usr/bin/env python + + +import json from abc import ABCMeta, abstractmethod -from .error import TelegramError -class Base(object): - +class TelegramObject(object): """Base class for most telegram object""" __metaclass__ = ABCMeta def __str__(self): - return self.to_json() + return self.to_data() def __getitem__(self, item): - try: - return self.__dict__[item] - except KeyError as e: - raise TelegramError(str(e)) + return self.__dict__[item] @staticmethod def de_json(data): - pass + raise NotImplementedError + + def to_json(self): + return json.dumps(self.to_data()) @abstractmethod - def to_json(self): - pass + def to_data(self): + return diff --git a/telegram/contact.py b/telegram/contact.py index 045b291c3..5dc39c130 100644 --- a/telegram/contact.py +++ b/telegram/contact.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class Contact(Base): +class Contact(TelegramObject): def __init__(self, phone_number, first_name, @@ -23,11 +22,11 @@ class Contact(Base): 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} + def to_data(self): + data = {'phone_number': self.phone_number, + 'first_name': self.first_name} if self.last_name: - json_data['last_name'] = self.last_name + data['last_name'] = self.last_name if self.user_id: - json_data['user_id'] = self.user_id - return json.dumps(json_data) + data['user_id'] = self.user_id + return data diff --git a/telegram/document.py b/telegram/document.py index 6e9eb2827..64a8decd3 100644 --- a/telegram/document.py +++ b/telegram/document.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class Document(Base): +class Document(TelegramObject): def __init__(self, file_id, thumb, @@ -32,13 +31,13 @@ class Document(Base): 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()} + def to_data(self): + data = {'file_id': self.file_id, + 'thumb': self.thumb.to_data()} if self.file_name: - json_data['file_name'] = self.file_name + data['file_name'] = self.file_name if self.mime_type: - json_data['mime_type'] = self.mime_type + data['mime_type'] = self.mime_type if self.file_size: - json_data['file_size'] = self.file_size - return json.dumps(json_data) + data['file_size'] = self.file_size + return data diff --git a/telegram/forcereply.py b/telegram/forcereply.py index 28be1fd43..8f2dcf78b 100644 --- a/telegram/forcereply.py +++ b/telegram/forcereply.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .replymarkup import ReplyMarkup +from .base import TelegramObject -class ForceReply(ReplyMarkup): +class ForceReply(TelegramObject): def __init__(self, force_reply=True, selective=None): @@ -17,8 +16,8 @@ class ForceReply(ReplyMarkup): return ForceReply(force_reply=data.get('force_reply', None), selective=data.get('selective', None)) - def to_json(self): - json_data = {'force_reply': self.force_reply} + def to_data(self): + data = {'force_reply': self.force_reply} if self.selective: - json_data['selective'] = self.selective - return json.dumps(json_data) + data['selective'] = self.selective + return data diff --git a/telegram/groupchat.py b/telegram/groupchat.py index 1f28c9c36..e60f67115 100644 --- a/telegram/groupchat.py +++ b/telegram/groupchat.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class GroupChat(Base): +class GroupChat(TelegramObject): def __init__(self, id, title): @@ -17,7 +16,7 @@ class GroupChat(Base): 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 to_data(self): + data = {'id': self.id, + 'title': self.title} + return data diff --git a/telegram/location.py b/telegram/location.py index 4a1942b69..4e05b9e00 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class Location(Base): +class Location(TelegramObject): def __init__(self, longitude, latitude): @@ -17,7 +16,7 @@ class Location(Base): 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 to_data(self): + data = {'longitude': self.longitude, + 'latitude': self.latitude} + return data diff --git a/telegram/message.py b/telegram/message.py index ccc26f14e..4f22275b0 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class Message(Base): +class Message(TelegramObject): def __init__(self, message_id, from_user, @@ -164,43 +163,43 @@ class Message(Base): 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()} + def to_data(self): + data = {'message_id': self.message_id, + 'from': self.from_user.to_data(), + 'date': self.date, + 'chat': self.chat.to_data()} if self.forward_from: - json_data['forward_from'] = self.forward_from + data['forward_from'] = self.forward_from if self.forward_date: - json_data['forward_date'] = self.forward_date + data['forward_date'] = self.forward_date if self.reply_to_message: - json_data['reply_to_message'] = self.reply_to_message + data['reply_to_message'] = self.reply_to_message if self.text: - json_data['text'] = self.text + data['text'] = self.text if self.audio: - json_data['audio'] = self.audio.to_json() + data['audio'] = self.audio.to_data() if self.document: - json_data['document'] = self.document.to_json() + data['document'] = self.document.to_data() if self.photo: - json_data['photo'] = self.photo.to_json() + data['photo'] = self.photo.to_data() if self.sticker: - json_data['sticker'] = self.sticker.to_json() + data['sticker'] = self.sticker.to_data() if self.video: - json_data['video'] = self.video.to_json() + data['video'] = self.video.to_data() if self.contact: - json_data['contact'] = self.contact.to_json() + data['contact'] = self.contact.to_data() if self.location: - json_data['location'] = self.location.to_json() + data['location'] = self.location.to_data() if self.new_chat_participant: - json_data['new_chat_participant'] = self.new_chat_participant + data['new_chat_participant'] = self.new_chat_participant if self.left_chat_participant: - json_data['left_chat_participant'] = self.left_chat_participant + data['left_chat_participant'] = self.left_chat_participant if self.new_chat_title: - json_data['new_chat_title'] = self.new_chat_title + data['new_chat_title'] = self.new_chat_title if self.new_chat_photo: - json_data['new_chat_photo'] = self.new_chat_photo + data['new_chat_photo'] = self.new_chat_photo if self.delete_chat_photo: - json_data['delete_chat_photo'] = self.delete_chat_photo + 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) + data['group_chat_created'] = self.group_chat_created + return data diff --git a/telegram/photosize.py b/telegram/photosize.py index 6e4db482c..82030926c 100644 --- a/telegram/photosize.py +++ b/telegram/photosize.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class PhotoSize(Base): +class PhotoSize(TelegramObject): def __init__(self, file_id, width, @@ -23,10 +22,10 @@ class PhotoSize(Base): 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} + def to_data(self): + 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) + data['file_size'] = self.file_size + return data diff --git a/telegram/replykeyboardhide.py b/telegram/replykeyboardhide.py index bba71149b..53d8c58b9 100644 --- a/telegram/replykeyboardhide.py +++ b/telegram/replykeyboardhide.py @@ -1,7 +1,6 @@ #!/usr/bin/env python -import json from .replymarkup import ReplyMarkup @@ -17,8 +16,8 @@ class ReplyKeyboardHide(ReplyMarkup): return ReplyKeyboardHide(hide_keyboard=data.get('hide_keyboard', None), selective=data.get('selective', None)) - def to_json(self): - json_data = {'hide_keyboard': self.hide_keyboard} + def to_data(self): + data = {'hide_keyboard': self.hide_keyboard} if self.selective: - json_data['selective'] = self.selective - return json.dumps(json_data) + data['selective'] = self.selective + return data diff --git a/telegram/replykeyboardmarkup.py b/telegram/replykeyboardmarkup.py index e3c273c00..df52e2407 100644 --- a/telegram/replykeyboardmarkup.py +++ b/telegram/replykeyboardmarkup.py @@ -1,7 +1,6 @@ #!/usr/bin/env python -import json from .replymarkup import ReplyMarkup @@ -27,12 +26,12 @@ class ReplyKeyboardMarkup(ReplyMarkup): ), selective=data.get('selective', None)) - def to_json(self): - json_data = {'keyboard': self.keyboard} + def to_data(self): + data = {'keyboard': self.keyboard} if self.resize_keyboard: - json_data['resize_keyboard'] = self.resize_keyboard + data['resize_keyboard'] = self.resize_keyboard if self.one_time_keyboard: - json_data['one_time_keyboard'] = self.one_time_keyboard + data['one_time_keyboard'] = self.one_time_keyboard if self.selective: - json_data['selective'] = self.selective - return json.dumps(json_data) + data['selective'] = self.selective + return data diff --git a/telegram/replymarkup.py b/telegram/replymarkup.py index 86b4166d4..7d4d998f6 100644 --- a/telegram/replymarkup.py +++ b/telegram/replymarkup.py @@ -1,7 +1,8 @@ #!/usr/bin/env python -from .telegram_boject_base import Base -class ReplyMarkup(Base): - def to_json(self): - raise NotImplementedError +from .base import TelegramObject + + +class ReplyMarkup(TelegramObject): + pass diff --git a/telegram/sticker.py b/telegram/sticker.py index 42c4b08fd..46ef1af4c 100644 --- a/telegram/sticker.py +++ b/telegram/sticker.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class Sticker(Base): +class Sticker(TelegramObject): def __init__(self, file_id, width, @@ -32,11 +31,11 @@ class Sticker(Base): 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()} + def to_data(self): + data = {'file_id': self.file_id, + 'width': self.width, + 'height': self.height, + 'thumb': self.thumb.to_data()} if self.file_size: - json_data['file_size'] = self.file_size - return json.dumps(json_data) + data['file_size'] = self.file_size + return data diff --git a/telegram/update.py b/telegram/update.py index df3fd0666..9030fafbf 100644 --- a/telegram/update.py +++ b/telegram/update.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class Update(Base): +class Update(TelegramObject): def __init__(self, update_id, message=None): @@ -23,8 +22,8 @@ class Update(Base): return Update(update_id=data.get('update_id', None), message=message) - def to_json(self): - json_data = {'update_id': self.update_id} + def to_data(self): + data = {'update_id': self.update_id} if self.message: - json_data['message'] = self.message.to_json() - return json.dumps(json_data) + data['message'] = self.message.to_data() + return data diff --git a/telegram/user.py b/telegram/user.py index 356c46c95..d9dd67701 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class User(Base): +class User(TelegramObject): def __init__(self, id, first_name, @@ -31,11 +30,11 @@ class User(Base): 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} + def to_data(self): + data = {'id': self.id, + 'first_name': self.first_name} if self.last_name: - json_data['last_name'] = self.last_name + data['last_name'] = self.last_name if self.username: - json_data['username'] = self.username - return json.dumps(json_data) + data['username'] = self.username + return data diff --git a/telegram/userprofilephotos.py b/telegram/userprofilephotos.py index b6e1eddfe..64395ff34 100644 --- a/telegram/userprofilephotos.py +++ b/telegram/userprofilephotos.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class UserProfilePhotos(Base): +class UserProfilePhotos(TelegramObject): def __init__(self, total_count, photos): @@ -25,12 +24,12 @@ class UserProfilePhotos(Base): return UserProfilePhotos(total_count=data.get('total_count', None), photos=photos) - def to_json(self): - json_data = {} + def to_data(self): + data = {} if self.total_count: - json_data['total_count'] = self.total_count + data['total_count'] = self.total_count if self.photos: - json_data['photos'] = [] + data['photos'] = [] for photo in self.photos: - json_data['photos'].append([x.to_json() for x in photo]) - return json.dumps(json_data) + data['photos'].append([x.to_data() for x in photo]) + return data diff --git a/telegram/video.py b/telegram/video.py index 093f15b5c..e5fac4f6e 100644 --- a/telegram/video.py +++ b/telegram/video.py @@ -1,11 +1,10 @@ #!/usr/bin/env python -import json -from .telegram_boject_base import Base +from .base import TelegramObject -class Video(Base): +class Video(TelegramObject): def __init__(self, file_id, width, @@ -41,16 +40,16 @@ class Video(Base): 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()} + def to_data(self): + data = {'file_id': self.file_id, + 'width': self.width, + 'height': self.height, + 'duration': self.duration, + 'thumb': self.thumb.to_data()} if self.mime_type: - json_data['mime_type'] = self.mime_type + data['mime_type'] = self.mime_type if self.file_size: - json_data['file_size'] = self.file_size + data['file_size'] = self.file_size if self.caption: - json_data['caption'] = self.caption - return json.dumps(json_data) + data['caption'] = self.caption + return data