From 635fae04e6e4fb0edc3f32c3ad5a1baabe067ccf Mon Sep 17 00:00:00 2001 From: Leandro Toledo Date: Tue, 7 Jul 2015 23:52:12 -0300 Subject: [PATCH] Adding Sticker and its tests --- telegram/__init__.py | 2 +- telegram/bot.py | 20 +++++++++++++++++++- telegram/message.py | 8 +++++++- telegram/sticker.py | 29 +++++++++++++++++++++++++++++ telegram/user.py | 1 + tests/test_bot.py | 7 +++++++ 6 files changed, 64 insertions(+), 3 deletions(-) diff --git a/telegram/__init__.py b/telegram/__init__.py index b34470706..90b280105 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -14,7 +14,7 @@ from groupchat import GroupChat from photosize import PhotoSize from audio import Audio from document import Document -# from sticket import Sticker +from sticker import Sticker # from video import Video # from contact import Contact # from location import Location diff --git a/telegram/bot.py b/telegram/bot.py index 9acc2b500..1b0657c65 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -234,9 +234,27 @@ class Bot(object): return Message.newFromJsonDict(data) - def sendSticker(self): + def sendSticker(self, + chat_id, + sticker, + reply_to_message_id=None, + reply_markup=None): + url = '%s/sendSticker' % (self.base_url) + data = {'chat_id': chat_id, + 'sticker': sticker} + + if reply_to_message_id: + data['reply_to_message_id'] = reply_to_message_id + if reply_markup: + data['reply_markup'] = reply_markup + + json_data = self._requestUrl(url, 'POST', data=data) + data = self._parseAndCheckTelegram(json_data.content) + + return Message.newFromJsonDict(data) + def sendVideo(self): url = '%s/sendVideo' % (self.base_url) diff --git a/telegram/message.py b/telegram/message.py index 7debe53e8..3c21419e8 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -79,6 +79,12 @@ class Message(object): else: photo = None + if 'sticker' in data: + from telegram import Sticker + sticker = Sticker.newFromJsonDict(data['sticker']) + else: + sticker = None + if 'new_chat_participant' in data: from telegram import User new_chat_participant = User.newFromJsonDict( @@ -106,7 +112,7 @@ class Message(object): audio=audio, document=document, photo=photo, - sticker=data.get('sticker', None), + sticker=sticker, video=data.get('video', None), contact=data.get('contact', None), location=data.get('location', None), diff --git a/telegram/sticker.py b/telegram/sticker.py index e69de29bb..f26ab74f2 100644 --- a/telegram/sticker.py +++ b/telegram/sticker.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + + +class Sticker(object): + def __init__(self, **kwargs): + param_defaults = { + 'file_id': None, + 'width': None, + 'height': None, + 'thumb': None, + 'file_size': None + } + + for (param, default) in param_defaults.iteritems(): + setattr(self, param, kwargs.get(param, default)) + + @staticmethod + def newFromJsonDict(data): + if 'thumb' in data: + from telegram import PhotoSize + thumb = PhotoSize.newFromJsonDict(data['thumb']) + else: + thumb = None + + return Sticker(file_id=data.get('file_id', None), + width=data.get('width', None), + height=data.get('height', None), + thumb=thumb, + file_size=data.get('file_size', None)) diff --git a/telegram/user.py b/telegram/user.py index e3e18f257..4864a3ac1 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -5,6 +5,7 @@ class User(object): def __init__(self, **kwargs): param_defaults = { 'id': None, + # 'chat_id' TODO 'first_name': None, 'last_name': None, 'username': None diff --git a/tests/test_bot.py b/tests/test_bot.py index 745edfd05..bc22ef501 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -85,3 +85,10 @@ class BotTest(unittest.TestCase): message = self._bot.sendDocument(document=str('BQADAQADHAADNTwtBxZxUGKyxYbYAg'), chat_id=12173560) self.assertEqual(u'BQADAQADHAADNTwtBxZxUGKyxYbYAg', message.document.file_id) + + def testSendSticker(self): + '''Test the telegram.Bot sendSticker method''' + print 'Testing sendSticket' + message = self._bot.sendSticker(sticker=str('BQADAQADHAADyIsGAAFZfq1bphjqlgI'), + chat_id=12173560) + self.assertEqual(39518, message.sticker.file_size)