@@ -29,6 +29,7 @@ class JsonSerializable:
2929 Subclasses of this class are guaranteed to be able to be converted to JSON format.
3030 All subclasses of this class must override to_json.
3131 """
32+
3233 def to_json (self ):
3334 """
3435 Returns a JSON string representation of this class.
@@ -44,6 +45,7 @@ class JsonDeserializable:
4445 Subclasses of this class are guaranteed to be able to be created from a json-style dict or json formatted string.
4546 All subclasses of this class must override de_json.
4647 """
48+
4749 @classmethod
4850 def de_json (cls , json_type ):
4951 """
@@ -79,6 +81,7 @@ def __str__(self):
7981
8082 return unicode (d )
8183
84+
8285class Update (JsonDeserializable ):
8386 @classmethod
8487 def de_json (cls , json_type ):
@@ -91,6 +94,7 @@ def __init__(self, update_id, message):
9194 self .update_id = update_id
9295 self .message = message
9396
97+
9498class User (JsonDeserializable ):
9599 @classmethod
96100 def de_json (cls , json_string ):
@@ -125,13 +129,44 @@ def __init__(self, id, title):
125129 self .title = title
126130
127131
132+ class Chat (JsonDeserializable ):
133+ @classmethod
134+ def de_json (cls , json_string ):
135+ obj = cls .check_json (json_string )
136+ id = obj ['id' ]
137+ type = obj ['type' ]
138+ title = None
139+ username = None
140+ first_name = None
141+ last_name = None
142+ if 'title' in obj :
143+ title = obj ['title' ]
144+ if 'username' in obj :
145+ username = obj ['username' ]
146+ if 'first_name' in obj :
147+ first_name = obj ['first_name' ]
148+ if 'last_name' in obj :
149+ last_name = obj ['last_name' ]
150+ return Chat (id , type , title , username , first_name , last_name )
151+
152+ def __init__ (self , id , type , title = None , username = None , first_name = None , last_name = None ):
153+ self .type = type
154+ self .last_name = last_name
155+ self .first_name = first_name
156+ self .username = username
157+ self .id = id
158+ self .title = title
159+
160+
128161class Message (JsonDeserializable ):
129162 @classmethod
130163 def de_json (cls , json_string ):
131164 obj = cls .check_json (json_string )
132165 message_id = obj ['message_id' ]
133- from_user = User .de_json (obj ['from' ])
134- chat = Message .parse_chat (obj ['chat' ])
166+ from_user = None
167+ if 'from' in obj :
168+ from_user = User .de_json (obj ['from' ])
169+ chat = Chat .de_json (obj ['chat' ])
135170 date = obj ['date' ]
136171 content_type = None
137172 opts = {}
@@ -409,8 +444,8 @@ def __init__(self, total_count, photos):
409444 self .total_count = total_count
410445 self .photos = photos
411446
412- class File (JsonDeserializable ):
413447
448+ class File (JsonDeserializable ):
414449 @classmethod
415450 def de_json (cls , json_type ):
416451 obj = cls .check_json (json_type )
0 commit comments