mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-01-03 09:49:21 +01:00
create _isFileRequest to simplify _requestUrl
This commit is contained in:
parent
4c306de3a7
commit
f0a5b756cf
1 changed files with 19 additions and 4 deletions
|
@ -583,6 +583,24 @@ class Bot(object):
|
|||
|
||||
return True
|
||||
|
||||
def _isFileRequest(self,
|
||||
data=None):
|
||||
"""Check if the request is a file request
|
||||
Args:
|
||||
data:
|
||||
A dict od (str, unicode) key/value pairs
|
||||
|
||||
Returns:
|
||||
bool
|
||||
"""
|
||||
if data:
|
||||
file_types = ['audio', 'document', 'photo', 'video']
|
||||
file_type = [i for i in data.keys() if i in file_types]
|
||||
if file_type:
|
||||
file_content = data[file_type[0]]
|
||||
return isinstance(file_content, file) or str(file_content).startswith('http')
|
||||
return False
|
||||
|
||||
def _requestUrl(self,
|
||||
url,
|
||||
method,
|
||||
|
@ -602,10 +620,7 @@ class Bot(object):
|
|||
"""
|
||||
|
||||
if method == 'POST':
|
||||
file_type = ['audio', 'document', 'photo', 'video']
|
||||
# len(dict{key1: val1, key2: val2}) == 2, it is used in case data is None
|
||||
if len(data) >= 2 and data.keys()[1] in file_type and \
|
||||
(isinstance(data.items()[1], file) or str(data.items()[1]).startswith('http')):
|
||||
if self._isFileRequest(data):
|
||||
try:
|
||||
data = InputFile(data)
|
||||
request = urllib2.Request(
|
||||
|
|
Loading…
Reference in a new issue