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
class Jsonable:
"""
Subclasses of this class are guaranteed to be able to be converted to JSON format.
All subclasses of this class must override to_json.
"""
def to_json(self):
"""
Returns a JSON string representation of this class.
@ -36,6 +38,7 @@ class Jsonable:
"""
raise NotImplementedError
class User:
@classmethod
def de_json(cls, json_string):
@ -101,6 +104,9 @@ class Message:
if 'location' in obj:
opts['location'] = Location.de_json(json.dumps(obj['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)
@classmethod
@ -248,6 +254,18 @@ class Video:
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):
self.phone_number = phone_number
self.first_name = first_name

View File

@ -87,3 +87,9 @@ def test_json_UserProfilePhotos():
upp = types.UserProfilePhotos.de_json(json_string)
assert upp.photos[0][0].width == 160
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'