diff --git a/README.md b/README.md index 23092c3..043fa9a 100644 --- a/README.md +++ b/README.md @@ -165,4 +165,4 @@ def listener1(*messages): - [x] sendLocation - [x] sendChatAction - [x] getUserProfilePhotos -- [ ] getUpdat(contact and chat message not yet) +- [ ] getUpdat(chat message not yet) diff --git a/setup.py b/setup.py index aa4c8e4..6bc3dbb 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup(name='pyTelegramBotAPI', - version='0.1.8', + version='0.1.9', description='Python Telegram bot api. ', author='eternnoir', author_email='eternnoir@gmail.com', @@ -16,6 +16,7 @@ setup(name='pyTelegramBotAPI', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', 'Environment :: Console', 'License :: OSI Approved :: GNU General Public License v2 (GPLv2)', ] diff --git a/telebot/types.py b/telebot/types.py index f1cdf74..e254fe4 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -28,6 +28,7 @@ class JsonSerializable: 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. @@ -134,6 +135,9 @@ class Message(JsonDeserializable): if 'location' in obj: opts['location'] = Location.de_json(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 @@ -280,6 +284,18 @@ class Video(JsonDeserializable): 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 diff --git a/tests/test_types.py b/tests/test_types.py index 8eb4688..e43c68b 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -88,3 +88,10 @@ 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'