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