From 759474924d0e671797565d2541ae3a18ca833cb2 Mon Sep 17 00:00:00 2001 From: pieter Date: Tue, 30 Jun 2015 16:40:44 +0200 Subject: [PATCH 1/6] Implemented getUserProfilePhotos (Though I do not see the use of it currently, because bots cannot download photos with the API) --- telebot/__init__.py | 20 +++++++++++++++----- telebot/apihelper.py | 12 ++++++++++++ telebot/types.py | 7 +++++++ tests/test_types.py | 6 ++++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index ca6de4c..8faed74 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -60,7 +60,7 @@ class TeleBot: def polling(self, interval=3): """ Always get updates. - :param interval: iterval secs. + :param interval: interval secs. :return: """ self.interval = interval @@ -91,10 +91,19 @@ class TeleBot: def get_me(self): result = apihelper.get_me(self.token) - if result['ok'] is not True: - raise Exception('getMe Error.' + json.dumps(result)) - u = types.User.de_json(json.dumps(result['result'])) - return u + return types.User.de_json(json.dumps(result['result'])) + + def get_user_profile_photos(self, user_id, offset=None, limit=None): + """ + Retrieves the user profile photos of the person with 'user_id' + See https://core.telegram.org/bots/api#getuserprofilephotos + :param user_id: + :param offset: + :param limit: + :return: + """ + result = apihelper.get_user_profile_photos(self.token, user_id, offset, limit) + return types.UserProfilePhotos.de_json(json.dumps(result['result'])) def send_message(self, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None): """ @@ -199,3 +208,4 @@ class TeleBot: :return: """ return apihelper.send_chat_action(self.token, chat_id, action) + diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 72476ed..2b8d5af 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -49,6 +49,18 @@ def get_updates(token, offset=None): req = requests.get(request_url) return check_result(method_url, req) +def get_user_profile_photos(token, user_id, offset=None, limit=None): + api_url = telebot.API_URL + method_url = r'getUserProfilePhotos' + request_url = api_url + 'bot' + token + '/' + method_url + payload = {'user_id': user_id} + if offset: + payload['offset'] = offset + if limit: + payload['limit'] = limit + req = requests.get(request_url, params=payload) + return check_result(method_url, req) + def forward_message(token, chat_id, from_chat_id, message_id): api_url = telebot.API_URL diff --git a/telebot/types.py b/telebot/types.py index efba253..120a7e9 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -256,6 +256,13 @@ class Location: class UserProfilePhotos: + @classmethod + def de_json(cls, json_string): + obj = json.loads(json_string) + total_count = obj['total_count'] + photos = [[PhotoSize.de_json(json.dumps(y)) for y in x] for x in obj['photos']] + return UserProfilePhotos(total_count, photos) + def __init__(self, total_count, photos): self.total_count = total_count self.photos = photos diff --git a/tests/test_types.py b/tests/test_types.py index e548145..1728db6 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -81,3 +81,9 @@ def test_json_Message_Location(): msg = types.Message.de_json(json_string) assert msg.location.latitude == 26.090577 assert msg.content_type == 'location' + +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 From 924099ddfa9950fdc1abb70627f38e1741c0d8fc Mon Sep 17 00:00:00 2001 From: FrankWang Date: Wed, 1 Jul 2015 09:55:25 +0800 Subject: [PATCH 2/6] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index bc0f95d..a18f505 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,9 @@ tb.send_chat_action(chat_id, action_string) # ReplyKeyboardMarkup. # Use ReplyKeyboardMarkup class. # Thanks pevdh. + +from telebot import types + markup = types.ReplyKeyboardMarkup() markup.add('a', 'v', 'd') tb.send_message(chat_id, message, None, None, markup) From b444565b7b51264706a8d131925a7b1d0d7fb93b Mon Sep 17 00:00:00 2001 From: pieter Date: Wed, 1 Jul 2015 18:17:33 +0200 Subject: [PATCH 3/6] Implemented #17 (with some small adjustments) and ForceReply Changed apihelper#convert_markup Constructed the Jsonable abstract class. All subclasses must override Jsonable#to_json. Made ReplyKeyboardHide, ReplyKeyboardMarkup and ForceReply a subclass of Jsonable to make things less complicated in convert_markup. --- telebot/apihelper.py | 5 ++--- telebot/types.py | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 2b8d5af..1dbe6ed 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -146,10 +146,9 @@ def check_result(func_name, result): return result_json def convert_markup(markup): - if isinstance(markup, types.ReplyKeyboardMarkup): + if not isinstance(markup, types.Jsonable): return markup.to_json() - else: - return markup + return markup class ApiError(Exception): def __init__(self, message, result): diff --git a/telebot/types.py b/telebot/types.py index 120a7e9..59727cf 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -22,6 +22,19 @@ 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. + + This function must be overridden by subclasses. + :return: a JSON formatted string. + """ + raise NotImplementedError class User: @classmethod @@ -268,7 +281,29 @@ class UserProfilePhotos: self.photos = photos -class ReplyKeyboardMarkup: +class ForceReply(Jsonable): + def __init__(self, selective=None): + self.selective = selective + + def to_json(self): + json_dict = {'force_reply': True} + if self.selective: + json_dict['selective'] = True + return json.dumps(json_dict) + + +class ReplyKeyboardHide(Jsonable): + def __init__(self, selective=None): + self.selective = selective + + def to_json(self): + json_dict = {'hide_keyboard': True} + if self.selective: + json_dict['selective'] = True + return json.dumps(json_dict) + + +class ReplyKeyboardMarkup(Jsonable): def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3): self.resize_keyboard = resize_keyboard self.one_time_keyboard = one_time_keyboard From 006838887275ba0c7968a1b4b180cd57e1c26106 Mon Sep 17 00:00:00 2001 From: pieter Date: Wed, 1 Jul 2015 18:56:21 +0200 Subject: [PATCH 4/6] Implemented #17 (with some small adjustments) and ForceReply Changed apihelper#convert_markup Constructed the Jsonable abstract class. All subclasses must override Jsonable#to_json. Made ReplyKeyboardHide, ReplyKeyboardMarkup and ForceReply a subclass of Jsonable to make things less complicated in convert_markup. --- telebot/apihelper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 10e88f9..ab59b2a 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -147,7 +147,7 @@ def check_result(func_name, result): def convert_markup(markup): - if not isinstance(markup, types.Jsonable): + if isinstance(markup, types.Jsonable): return markup.to_json() return markup From 949dfc0fb04fcb9d1c168bc2205b01075b7891cd Mon Sep 17 00:00:00 2001 From: pieter Date: Wed, 1 Jul 2015 19:12:12 +0200 Subject: [PATCH 5/6] Fix unnecessary Thread creation when no new messages arrived. --- telebot/__init__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index e2caf40..31bff89 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -44,13 +44,15 @@ class TeleBot: def get_update(self): result = apihelper.get_updates(self.token, offset=(self.last_update_id + 1)) updates = result['result'] - notify_updates = [] + new_messages = [] for update in updates: if update['update_id'] > self.last_update_id: self.last_update_id = update['update_id'] msg = types.Message.de_json(json.dumps(update['message'])) - notify_updates.append(msg) - self.__notify_update(notify_updates) + new_messages.append(msg) + + if len(new_messages) > 0: + self.__notify_update(new_messages) def __notify_update(self, new_messages): for listener in self.update_listener: From b4dc441ffd5336883a188dc1150895112e90948a Mon Sep 17 00:00:00 2001 From: pieter Date: Wed, 1 Jul 2015 19:44:49 +0200 Subject: [PATCH 6/6] Update TODO --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6583f5d..23092c3 100644 --- a/README.md +++ b/README.md @@ -164,5 +164,5 @@ def listener1(*messages): - [x] sendVideo - [x] sendLocation - [x] sendChatAction -- [ ] getUserProfilePhotos +- [x] getUserProfilePhotos - [ ] getUpdat(contact and chat message not yet)