diff --git a/telegram/__init__.py b/telegram/__init__.py index e810021b1..411e0d1a4 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -20,7 +20,7 @@ from video import Video from location import Location from chataction import ChatAction # from inputfile import InputFile -# from userprofilephotos import UserProfilePhotos +from userprofilephotos import UserProfilePhotos # from replykeyboardmarkup import ReplyKeyboardMarkup # from replykeyboardhide import ReplyKeyboardHide # from forcereply import ForceReply diff --git a/telegram/bot.py b/telegram/bot.py index 7d4efc354..9d3213675 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -1,12 +1,12 @@ #!/usr/bin/env python # encoding: utf-8 -"""A library that provides a Python interface to the Telegram Bots API""" +"""A library that provides a Python interface to the Telegram Bot API""" import json import requests -from telegram import (User, Message, Update) +from telegram import (User, Message, Update, UserProfilePhotos) class Bot(object): @@ -385,9 +385,25 @@ class Bot(object): self._requestUrl(url, 'POST', data=data) - def getUserProfilePhotos(self): + def getUserProfilePhotos(self, + user_id, + offset=None, + limit=100): + url = '%s/getUserProfilePhotos' % (self.base_url) + data = {'user_id': user_id} + + if offset: + data['offset'] = offset + if limit: + data['limit'] = limit + + json_data = self._requestUrl(url, 'POST', data=data) + data = self._parseAndCheckTelegram(json_data.content) + + return UserProfilePhotos.newFromJsonDict(data) + def getUpdates(self, offset=None, limit=100, diff --git a/telegram/userprofilephotos.py b/telegram/userprofilephotos.py index e69de29bb..1fc23de4d 100644 --- a/telegram/userprofilephotos.py +++ b/telegram/userprofilephotos.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + + +class UserProfilePhotos(object): + def __init__(self, **kwargs): + param_defaults = { + 'total_count': None, + 'photos': None + } + + for (param, default) in param_defaults.iteritems(): + setattr(self, param, kwargs.get(param, default)) + + @staticmethod + def newFromJsonDict(data): + if 'photos' in data: + from telegram import PhotoSize + photos = [] + for photo in data['photos']: + photos.append([PhotoSize.newFromJsonDict(x) for x in photo]) + else: + photos= None + + return UserProfilePhotos(total_count=data.get('total_count', None), + photos=photos) diff --git a/tests/test_bot.py b/tests/test_bot.py index c84392402..5ef95a438 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -115,3 +115,9 @@ class BotTest(unittest.TestCase): message = self._bot.sendChatAction(action=telegram.ChatAction.TYPING, chat_id=12173560) # TODO: return json + + def testGetUserProfilePhotos(self): + '''Test the telegram.Bot getUserProfilePhotos method''' + print 'Testing getUserProfilePhotos' + upf = self._bot.getUserProfilePhotos(user_id=12173560) + self.assertEqual(8314, upf.photos[0][0].file_size)