1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Message support contact.

This commit is contained in:
eternnoir 2015-07-02 11:19:38 +08:00
parent 23574dee59
commit 1fdf8bd570
2 changed files with 25 additions and 1 deletions

View File

@ -22,11 +22,13 @@ ForceReply
import json import json
class Jsonable: class Jsonable:
""" """
Subclasses of this class are guaranteed to be able to be converted to JSON format. Subclasses of this class are guaranteed to be able to be converted to JSON format.
All subclasses of this class must override to_json. All subclasses of this class must override to_json.
""" """
def to_json(self): def to_json(self):
""" """
Returns a JSON string representation of this class. Returns a JSON string representation of this class.
@ -36,6 +38,7 @@ class Jsonable:
""" """
raise NotImplementedError raise NotImplementedError
class User: class User:
@classmethod @classmethod
def de_json(cls, json_string): def de_json(cls, json_string):
@ -101,6 +104,9 @@ class Message:
if 'location' in obj: if 'location' in obj:
opts['location'] = Location.de_json(json.dumps(obj['location'])) opts['location'] = Location.de_json(json.dumps(obj['location']))
content_type = 'location' content_type = 'location'
if 'contact' in obj:
opts['contact'] = Contact.de_json(json.dumps(obj['contact']))
content_type = 'contact'
return Message(message_id, from_user, date, chat, content_type, opts) return Message(message_id, from_user, date, chat, content_type, opts)
@classmethod @classmethod
@ -248,6 +254,18 @@ class Video:
class Contact: class Contact:
@classmethod
def de_json(cls, json_string):
obj = json.loads(json_string)
phone_number = obj['phone_number']
first_name = obj['first_name']
last_name = None
if 'last_name' in obj:
last_name = obj['last_name']
if 'user_id' in obj:
user_id = obj['user_id']
return Contact(phone_number, first_name, last_name, user_id)
def __init__(self, phone_number, first_name, last_name=None, user_id=None): def __init__(self, phone_number, first_name, last_name=None, user_id=None):
self.phone_number = phone_number self.phone_number = phone_number
self.first_name = first_name self.first_name = first_name

View File

@ -87,3 +87,9 @@ def test_json_UserProfilePhotos():
upp = types.UserProfilePhotos.de_json(json_string) upp = types.UserProfilePhotos.de_json(json_string)
assert upp.photos[0][0].width == 160 assert upp.photos[0][0].width == 160
assert upp.photos[0][-1].height == 800 assert upp.photos[0][-1].height == 800
def test_json_contact():
json_string = r'{"phone_number":"00011111111","first_name":"dd","last_name":"ddl","user_id":8633}'
contact = types.Contact.de_json(json_string)
assert contact.first_name == 'dd'
assert contact.last_name == 'ddl'