base class for telegram class

This commit is contained in:
JASON0916 2015-07-17 22:53:54 +08:00
parent 5460671384
commit ce852de9a8
15 changed files with 65 additions and 68 deletions

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class Audio(object): class Audio(Base):
def __init__(self, def __init__(self,
file_id, file_id,
duration, duration,
@ -30,6 +31,3 @@ class Audio(object):
if self.file_size: if self.file_size:
json_data['file_size'] = self.file_size json_data['file_size'] = self.file_size
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class Contact(object): class Contact(Base):
def __init__(self, def __init__(self,
phone_number, phone_number,
first_name, first_name,
@ -29,7 +30,4 @@ class Contact(object):
json_data['last_name'] = self.last_name json_data['last_name'] = self.last_name
if self.user_id: if self.user_id:
json_data['user_id'] = self.user_id json_data['user_id'] = self.user_id
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class Document(object): class Document(Base):
def __init__(self, def __init__(self,
file_id, file_id,
thumb, thumb,
@ -40,7 +41,4 @@ class Document(object):
json_data['mime_type'] = self.mime_type json_data['mime_type'] = self.mime_type
if self.file_size: if self.file_size:
json_data['file_size'] = self.file_size json_data['file_size'] = self.file_size
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -21,7 +21,4 @@ class ForceReply(ReplyMarkup):
json_data = {'force_reply': self.force_reply} json_data = {'force_reply': self.force_reply}
if self.selective: if self.selective:
json_data['selective'] = self.selective json_data['selective'] = self.selective
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class GroupChat(object): class GroupChat(Base):
def __init__(self, def __init__(self,
id, id,
title): title):
@ -19,7 +20,4 @@ class GroupChat(object):
def to_json(self): def to_json(self):
json_data = {'id': self.id, json_data = {'id': self.id,
'title': self.title} 'title': self.title}
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class Location(object): class Location(Base):
def __init__(self, def __init__(self,
longitude, longitude,
latitude): latitude):
@ -19,7 +20,4 @@ class Location(object):
def to_json(self): def to_json(self):
json_data = {'longitude': self.longitude, json_data = {'longitude': self.longitude,
'latitude': self.latitude} 'latitude': self.latitude}
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class Message(object): class Message(Base):
def __init__(self, def __init__(self,
message_id, message_id,
from_user, from_user,
@ -202,7 +203,4 @@ class Message(object):
json_data['delete_chat_photo'] = self.delete_chat_photo json_data['delete_chat_photo'] = self.delete_chat_photo
if self.group_chat_created: if self.group_chat_created:
json_data['group_chat_created'] = self.group_chat_created json_data['group_chat_created'] = self.group_chat_created
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class PhotoSize(object): class PhotoSize(Base):
def __init__(self, def __init__(self,
file_id, file_id,
width, width,
@ -28,7 +29,4 @@ class PhotoSize(object):
'height': self.height} 'height': self.height}
if self.file_size: if self.file_size:
json_data['file_size'] = self.file_size json_data['file_size'] = self.file_size
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -1,9 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from .telegram_boject_base import Base
class ReplyMarkup(object): class ReplyMarkup(Base):
def to_json(self): def to_json(self):
raise NotImplementedError raise NotImplementedError
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class Sticker(object): class Sticker(Base):
def __init__(self, def __init__(self,
file_id, file_id,
width, width,
@ -38,7 +39,4 @@ class Sticker(object):
'thumb': self.thumb.to_json()} 'thumb': self.thumb.to_json()}
if self.file_size: if self.file_size:
json_data['file_size'] = self.file_size json_data['file_size'] = self.file_size
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -0,0 +1,26 @@
from abc import ABCMeta, abstractmethod
from telegram import TelegramError
class Base(object):
"""Base class for most telegram object"""
__metaclass__ = ABCMeta
def __str__(self):
return self.to_json()
def __getitem__(self, item):
try:
return self.__dict__[item]
except KeyError as e:
raise TelegramError(str(e))
@staticmethod
def de_json(data):
pass
@abstractmethod
def to_json(self):
pass

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class Update(object): class Update(Base):
def __init__(self, def __init__(self,
update_id, update_id,
message=None): message=None):
@ -26,7 +27,4 @@ class Update(object):
json_data = {'update_id': self.update_id} json_data = {'update_id': self.update_id}
if self.message: if self.message:
json_data['message'] = self.message.to_json() json_data['message'] = self.message.to_json()
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class User(object): class User(Base):
def __init__(self, def __init__(self,
id, id,
first_name, first_name,
@ -37,7 +38,4 @@ class User(object):
json_data['last_name'] = self.last_name json_data['last_name'] = self.last_name
if self.username: if self.username:
json_data['username'] = self.username json_data['username'] = self.username
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class UserProfilePhotos(object): class UserProfilePhotos(Base):
def __init__(self, def __init__(self,
total_count, total_count,
photos): photos):
@ -32,7 +33,4 @@ class UserProfilePhotos(object):
json_data['photos'] = [] json_data['photos'] = []
for photo in self.photos: for photo in self.photos:
json_data['photos'].append([x.to_json() for x in photo]) json_data['photos'].append([x.to_json() for x in photo])
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()

View file

@ -2,9 +2,10 @@
import json import json
from .telegram_boject_base import Base
class Video(object): class Video(Base):
def __init__(self, def __init__(self,
file_id, file_id,
width, width,
@ -52,7 +53,4 @@ class Video(object):
json_data['file_size'] = self.file_size json_data['file_size'] = self.file_size
if self.caption: if self.caption:
json_data['caption'] = self.caption json_data['caption'] = self.caption
return json.dumps(json_data) return json.dumps(json_data)
def __str__(self):
return self.to_json()