From 1fdf8bd570d622e1e58414651916b4c7c7df7fc6 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Thu, 2 Jul 2015 11:19:38 +0800 Subject: [PATCH 1/4] Message support contact. --- telebot/types.py | 18 ++++++++++++++++++ tests/test_types.py | 8 +++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/telebot/types.py b/telebot/types.py index 59727cf..c02cb15 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -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 diff --git a/tests/test_types.py b/tests/test_types.py index 1728db6..358b35d 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -86,4 +86,10 @@ def test_json_UserProfilePhotos(): json_string = r'{"total_count":1,"photos":[[{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAATZH_SpyZjzIwdVAAIC","file_size":6150,"width":160,"height":160},{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAATOiTNi_YoJMghVAAIC","file_size":13363,"width":320,"height":320},{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAAQW4DyFv0-lhglVAAIC","file_size":28347,"width":640,"height":640},{"file_id":"AgADAgADqacxG6wpRwABvEB6fpeIcKS4HAIkAAT50RvJCg0GQApVAAIC","file_size":33953,"width":800,"height":800}]]}' upp = types.UserProfilePhotos.de_json(json_string) assert upp.photos[0][0].width == 160 - assert upp.photos[0][-1].height == 800 \ No newline at end of file + 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' \ No newline at end of file From 1e221433f0d3e81ef184a32d39208985c6673f99 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Thu, 2 Jul 2015 11:30:45 +0800 Subject: [PATCH 2/4] Update Version 0.1.9 Change log: - Support python3 - ReplyKeyboardHide - Message support contact --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index aa4c8e4..e11942b 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', From 77d699bc3aed13acc70cc3d4a2f0a4b9d4dfdbc0 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Thu, 2 Jul 2015 11:51:50 +0800 Subject: [PATCH 3/4] Add python 3 to setup.py. --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index e11942b..6bc3dbb 100644 --- a/setup.py +++ b/setup.py @@ -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)', ] From b17416e7186a6341ed2f2e8dbb8af4cdc2b71ae2 Mon Sep 17 00:00:00 2001 From: FrankWang Date: Thu, 2 Jul 2015 11:53:11 +0800 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)