Adding Document and minor fixes

This commit is contained in:
Leandro Toledo 2015-07-07 21:12:51 -03:00
parent c4cfcaecec
commit 368fe234f3
6 changed files with 103 additions and 7 deletions

View file

@ -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

View file

@ -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))

View file

@ -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(

View file

@ -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))

View file

@ -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),

View file

@ -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)