X Tutup
Skip to content

Commit 3c88965

Browse files
committed
Allow http url as a file_id
N.B. test_send_video_mp4_file_url() is still failing, probably because of telegram servers bug. Will contact telegram bot support about that.
1 parent c2b8c4b commit 3c88965

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

telegram/inputfile.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import os
3232
import sys
3333

34-
from future.moves.urllib.request import urlopen
35-
3634
from telegram import TelegramError
3735

3836
DEFAULT_MIME_TYPE = 'application/octet-stream'
@@ -49,32 +47,28 @@ def __init__(self, data):
4947
if 'audio' in data:
5048
self.input_name = 'audio'
5149
self.input_file = data.pop('audio')
52-
if 'document' in data:
50+
elif 'document' in data:
5351
self.input_name = 'document'
5452
self.input_file = data.pop('document')
55-
if 'photo' in data:
53+
elif 'photo' in data:
5654
self.input_name = 'photo'
5755
self.input_file = data.pop('photo')
58-
if 'sticker' in data:
56+
elif 'sticker' in data:
5957
self.input_name = 'sticker'
6058
self.input_file = data.pop('sticker')
61-
if 'video' in data:
59+
elif 'video' in data:
6260
self.input_name = 'video'
6361
self.input_file = data.pop('video')
64-
if 'voice' in data:
62+
elif 'voice' in data:
6563
self.input_name = 'voice'
6664
self.input_file = data.pop('voice')
67-
if 'certificate' in data:
65+
elif 'certificate' in data:
6866
self.input_name = 'certificate'
6967
self.input_file = data.pop('certificate')
70-
71-
if str(self.input_file).startswith('http'):
72-
from_url = True
73-
self.input_file = urlopen(self.input_file)
7468
else:
75-
from_url = False
69+
raise TelegramError('Unknown inputfile type')
7670

77-
if hasattr(self.input_file, 'read') or from_url:
71+
if hasattr(self.input_file, 'read'):
7872
self.filename = None
7973
self.input_file_content = self.input_file.read()
8074
if 'filename' in data:
@@ -83,8 +77,6 @@ def __init__(self, data):
8377
# on py2.7, pylint fails to understand this properly
8478
# pylint: disable=E1101
8579
self.filename = os.path.basename(self.input_file.name)
86-
elif from_url:
87-
self.filename = os.path.basename(self.input_file.url).split('?')[0].split('&')[0]
8880

8981
try:
9082
self.mimetype = InputFile.is_image(self.input_file_content)
@@ -186,6 +178,6 @@ def is_inputfile(data):
186178
if file_type:
187179
file_content = data[file_type[0]]
188180

189-
return hasattr(file_content, 'read') or str(file_content).startswith('http')
181+
return hasattr(file_content, 'read')
190182

191183
return False

tests/test_audio.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,13 @@ def test_send_audio_mp3_file_custom_filename(self):
134134
@timeout(10)
135135
def test_send_audio_mp3_url_file(self):
136136
message = self._bot.sendAudio(
137-
chat_id=self._chat_id,
138-
audio=self.audio_file_url,
139-
duration=self.duration,
140-
performer=self.performer,
141-
title=self.title)
137+
chat_id=self._chat_id, audio=self.audio_file_url, duration=self.duration)
142138

143139
audio = message.audio
144140

145141
self.assertTrue(isinstance(audio.file_id, str))
146142
self.assertNotEqual(audio.file_id, '')
147143
self.assertEqual(audio.duration, self.duration)
148-
self.assertEqual(audio.performer, self.performer)
149-
self.assertEqual(audio.title, self.title)
150144
self.assertEqual(audio.mime_type, self.mime_type)
151145
self.assertEqual(audio.file_size, self.file_size)
152146

0 commit comments

Comments
 (0)
X Tutup