mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-01-19 15:43:24 +01:00
Adding URL support to send files
This commit is contained in:
parent
39ee67b95c
commit
8ab910bd62
3 changed files with 17 additions and 11 deletions
|
@ -602,10 +602,10 @@ class Bot(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if method == 'POST':
|
if method == 'POST':
|
||||||
if 'audio' in data and isinstance(data['audio'], file) or \
|
if 'audio' in data and (isinstance(data['audio'], file) or 'http' in data['audio']) or \
|
||||||
'document' in data and isinstance(data['document'], file) or \
|
'document' in data and (isinstance(data['document'], file) or 'http' in data['document']) or \
|
||||||
'photo' in data and isinstance(data['photo'], file) or \
|
'photo' in data and (isinstance(data['photo'], file) or 'http' in data['photo']) or \
|
||||||
'video' in data and isinstance(data['video'], file):
|
'video' in data and (isinstance(data['video'], file) or 'http' in data['video']):
|
||||||
try:
|
try:
|
||||||
data = InputFile(data)
|
data = InputFile(data)
|
||||||
request = urllib2.Request(
|
request = urllib2.Request(
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
import mimetools
|
import mimetools
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
|
import urllib2
|
||||||
|
|
||||||
|
|
||||||
class InputFile(object):
|
class InputFile(object):
|
||||||
|
@ -12,21 +13,26 @@ class InputFile(object):
|
||||||
self.data = data
|
self.data = data
|
||||||
self.boundary = mimetools.choose_boundary()
|
self.boundary = mimetools.choose_boundary()
|
||||||
|
|
||||||
if 'audio' in data and isinstance(data['audio'], file):
|
if 'audio' in data:
|
||||||
self.input_name = 'audio'
|
self.input_name = 'audio'
|
||||||
self.input_file = data.pop('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_name = 'document'
|
||||||
self.input_file = data.pop('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_name = 'photo'
|
||||||
self.input_file = data.pop('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_name = 'video'
|
||||||
self.input_file = data.pop('video')
|
self.input_file = data.pop('video')
|
||||||
|
|
||||||
self.input_file_content = self.input_file.read()
|
if isinstance(self.input_file, file):
|
||||||
self.filename = os.path.basename(self.input_file.name)
|
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 \
|
self.mimetype = mimetypes.guess_type(self.filename)[0] or \
|
||||||
'application/octet-stream'
|
'application/octet-stream'
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ class BotTest(unittest.TestCase):
|
||||||
'''Test the telegram.Bot getUpdates method'''
|
'''Test the telegram.Bot getUpdates method'''
|
||||||
print 'Testing getUpdates'
|
print 'Testing getUpdates'
|
||||||
updates = self._bot.getUpdates()
|
updates = self._bot.getUpdates()
|
||||||
self.assertEqual(129566577, updates[0].update_id)
|
self.assertEqual(129566590, updates[0].update_id)
|
||||||
|
|
||||||
def testForwardMessage(self):
|
def testForwardMessage(self):
|
||||||
'''Test the telegram.Bot forwardMessage method'''
|
'''Test the telegram.Bot forwardMessage method'''
|
||||||
|
|
Loading…
Reference in a new issue