Adding URL support to send files

This commit is contained in:
Leandro Toledo 2015-07-10 19:46:15 -03:00
parent 39ee67b95c
commit 8ab910bd62
3 changed files with 17 additions and 11 deletions

View file

@ -602,10 +602,10 @@ class Bot(object):
"""
if method == 'POST':
if 'audio' in data and isinstance(data['audio'], file) or \
'document' in data and isinstance(data['document'], file) or \
'photo' in data and isinstance(data['photo'], file) or \
'video' in data and isinstance(data['video'], file):
if 'audio' in data and (isinstance(data['audio'], file) or 'http' in data['audio']) or \
'document' in data and (isinstance(data['document'], file) or 'http' in data['document']) or \
'photo' in data and (isinstance(data['photo'], file) or 'http' in data['photo']) or \
'video' in data and (isinstance(data['video'], file) or 'http' in data['video']):
try:
data = InputFile(data)
request = urllib2.Request(

View file

@ -4,6 +4,7 @@
import mimetools
import mimetypes
import os
import urllib2
class InputFile(object):
@ -12,21 +13,26 @@ class InputFile(object):
self.data = data
self.boundary = mimetools.choose_boundary()
if 'audio' in data and isinstance(data['audio'], file):
if 'audio' in data:
self.input_name = 'audio'
self.input_file = data.pop('audio')
if 'document' in data and isinstance(data['document'], file):
if 'document' in data:
self.input_name = 'document'
self.input_file = data.pop('document')
if 'photo' in data and isinstance(data['photo'], file):
if 'photo' in data:
self.input_name = 'photo'
self.input_file = data.pop('photo')
if 'video' in data and isinstance(data['video'], file):
if 'video' in data:
self.input_name = 'video'
self.input_file = data.pop('video')
self.input_file_content = self.input_file.read()
self.filename = os.path.basename(self.input_file.name)
if isinstance(self.input_file, file):
self.filename = os.path.basename(self.input_file.name)
self.input_file_content = self.input_file.read()
if 'http' in self.input_file:
self.filename = os.path.basename(self.input_file)
self.input_file_content = urllib2.urlopen(self.input_file).read()
self.mimetype = mimetypes.guess_type(self.filename)[0] or \
'application/octet-stream'

View file

@ -33,7 +33,7 @@ class BotTest(unittest.TestCase):
'''Test the telegram.Bot getUpdates method'''
print 'Testing getUpdates'
updates = self._bot.getUpdates()
self.assertEqual(129566577, updates[0].update_id)
self.assertEqual(129566590, updates[0].update_id)
def testForwardMessage(self):
'''Test the telegram.Bot forwardMessage method'''