From 368fe234f3c570f5f11df53cb72a4db12c493adc Mon Sep 17 00:00:00 2001 From: Leandro Toledo Date: Tue, 7 Jul 2015 21:12:51 -0300 Subject: [PATCH] Adding Document and minor fixes --- telegram/__init__.py | 2 +- telegram/audio.py | 6 +++--- telegram/bot.py | 49 +++++++++++++++++++++++++++++++++++++++++++- telegram/document.py | 29 ++++++++++++++++++++++++++ telegram/message.py | 8 +++++++- tests/test_bot.py | 16 ++++++++++++++- 6 files changed, 103 insertions(+), 7 deletions(-) diff --git a/telegram/__init__.py b/telegram/__init__.py index 128164d1b..b34470706 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -13,7 +13,7 @@ from update import Update from groupchat import GroupChat from photosize import PhotoSize from audio import Audio -# from document import Document +from document import Document # from sticket import Sticker # from video import Video # from contact import Contact diff --git a/telegram/audio.py b/telegram/audio.py index f803104d1..d8d2390a5 100644 --- a/telegram/audio.py +++ b/telegram/audio.py @@ -16,6 +16,6 @@ class Audio(object): @staticmethod def newFromJsonDict(data): return Audio(file_id=data.get('file_id', None), - duration=data.get('duration', None), - mime_type=data.get('mime_type', None), - file_size=data.get('file_size', None)) + duration=data.get('duration', None), + mime_type=data.get('mime_type', None), + file_size=data.get('file_size', None)) diff --git a/telegram/bot.py b/telegram/bot.py index 46b8c7241..9acc2b500 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -195,9 +195,45 @@ class Bot(object): return Message.newFromJsonDict(data) - def sendDocument(self): + def sendDocument(self, + chat_id, + document, + reply_to_message_id=None, + reply_markup=None): + """Use this method to send general files. + + Args: + chat_id: + Unique identifier for the message recipient — User or GroupChat id. + document: + File to send. You can either pass a file_id as String to resend a + file that is already on the Telegram servers, or upload a new file + using multipart/form-data. + reply_to_message_id: + If the message is a reply, ID of the original message. [Optional] + reply_markup: + Additional interface options. A JSON-serialized object for a + custom reply keyboard, instructions to hide keyboard or to force a + reply from the user. [Optional] + Returns: + A telegram.Message instance representing the message posted. + """ + url = '%s/sendDocument' % (self.base_url) + data = {'chat_id': chat_id, + 'document': document} + + 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 sendSticker(self): url = '%s/sendSticker' % (self.base_url) @@ -276,6 +312,17 @@ class Bot(object): ) except requests.RequestException as e: pass + if 'document' in data and isinstance(data['document'], file): + try: + document = data.pop('document') + + return requests.post( + url, + data=data, + files={'document': document} + ) + except requests.RequestException as e: + pass else: try: return requests.post( diff --git a/telegram/document.py b/telegram/document.py index e69de29bb..1bdba84df 100644 --- a/telegram/document.py +++ b/telegram/document.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + + +class Document(object): + def __init__(self, **kwargs): + param_defaults = { + 'file_id': None, + 'thumb': None, + 'file_name': None, + 'mime_type': 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 Document(file_id=data.get('file_id', None), + thumb=thumb, + file_name=data.get('file_name', None), + mime_type=data.get('mime_type', None), + file_size=data.get('file_size', None)) diff --git a/telegram/message.py b/telegram/message.py index 0f007c4d1..7debe53e8 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -67,6 +67,12 @@ class Message(object): else: audio = None + if 'document' in data: + from telegram import Document + document = Document.newFromJsonDict(data['document']) + else: + document = None + if 'photo' in data: from telegram import PhotoSize photo = [PhotoSize.newFromJsonDict(x) for x in data['photo']] @@ -98,7 +104,7 @@ class Message(object): reply_to_message=reply_to_message, text=data.get('text', None), audio=audio, - document=data.get('document', None), + document=document, photo=photo, sticker=data.get('sticker', None), video=data.get('video', None), diff --git a/tests/test_bot.py b/tests/test_bot.py index c29b3834b..745edfd05 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -67,7 +67,21 @@ class BotTest(unittest.TestCase): def testResendAudio(self): '''Test the telegram.Bot sendAudio method''' - print 'Testing sendAudio - Resent' + print 'Testing sendAudio - Resend' message = self._bot.sendAudio(audio=str('AwADAQADIQEAAvjAuQABSAXg_GhkhZcC'), chat_id=12173560) self.assertEqual(u'AwADAQADIQEAAvjAuQABSAXg_GhkhZcC', message.audio.file_id) + + def testSendDocument(self): + '''Test the telegram.Bot sendDocument method''' + print 'Testing sendDocument - File' + message = self._bot.sendDocument(document=open('tests/telegram.png', 'rb'), + chat_id=12173560) + self.assertEqual(12948, message.document.file_size) + + def testResendDocument(self): + '''Test the telegram.Bot sendDocument method''' + print 'Testing sendDocument - Resend' + message = self._bot.sendDocument(document=str('BQADAQADHAADNTwtBxZxUGKyxYbYAg'), + chat_id=12173560) + self.assertEqual(u'BQADAQADHAADNTwtBxZxUGKyxYbYAg', message.document.file_id)