Skip to content

Commit 8ab910b

Browse files
committed
Adding URL support to send files
1 parent 39ee67b commit 8ab910b

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

telegram/bot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,10 @@ def _requestUrl(self,
602602
"""
603603

604604
if method == 'POST':
605-
if 'audio' in data and isinstance(data['audio'], file) or \
606-
'document' in data and isinstance(data['document'], file) or \
607-
'photo' in data and isinstance(data['photo'], file) or \
608-
'video' in data and isinstance(data['video'], file):
605+
if 'audio' in data and (isinstance(data['audio'], file) or 'http' in data['audio']) or \
606+
'document' in data and (isinstance(data['document'], file) or 'http' in data['document']) or \
607+
'photo' in data and (isinstance(data['photo'], file) or 'http' in data['photo']) or \
608+
'video' in data and (isinstance(data['video'], file) or 'http' in data['video']):
609609
try:
610610
data = InputFile(data)
611611
request = urllib2.Request(

telegram/inputfile.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import mimetools
55
import mimetypes
66
import os
7+
import urllib2
78

89

910
class InputFile(object):
@@ -12,21 +13,26 @@ def __init__(self,
1213
self.data = data
1314
self.boundary = mimetools.choose_boundary()
1415

15-
if 'audio' in data and isinstance(data['audio'], file):
16+
if 'audio' in data:
1617
self.input_name = 'audio'
1718
self.input_file = data.pop('audio')
18-
if 'document' in data and isinstance(data['document'], file):
19+
if 'document' in data:
1920
self.input_name = 'document'
2021
self.input_file = data.pop('document')
21-
if 'photo' in data and isinstance(data['photo'], file):
22+
if 'photo' in data:
2223
self.input_name = 'photo'
2324
self.input_file = data.pop('photo')
24-
if 'video' in data and isinstance(data['video'], file):
25+
if 'video' in data:
2526
self.input_name = 'video'
2627
self.input_file = data.pop('video')
2728

28-
self.input_file_content = self.input_file.read()
29-
self.filename = os.path.basename(self.input_file.name)
29+
if isinstance(self.input_file, file):
30+
self.filename = os.path.basename(self.input_file.name)
31+
self.input_file_content = self.input_file.read()
32+
if 'http' in self.input_file:
33+
self.filename = os.path.basename(self.input_file)
34+
self.input_file_content = urllib2.urlopen(self.input_file).read()
35+
3036
self.mimetype = mimetypes.guess_type(self.filename)[0] or \
3137
'application/octet-stream'
3238

tests/test_bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def testGetUpdates(self):
3333
'''Test the telegram.Bot getUpdates method'''
3434
print 'Testing getUpdates'
3535
updates = self._bot.getUpdates()
36-
self.assertEqual(129566577, updates[0].update_id)
36+
self.assertEqual(129566590, updates[0].update_id)
3737

3838
def testForwardMessage(self):
3939
'''Test the telegram.Bot forwardMessage method'''

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy