Skip to content

Commit 1038d4f

Browse files
committed
Added download_file
1 parent fd1f165 commit 1038d4f

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
"""
2323
Module : telebot
2424
"""
25-
26-
API_URL = r"https://api.telegram.org/"
27-
28-
2925
class TeleBot:
3026
""" This is TeleBot Class
3127
Methods:
@@ -172,6 +168,9 @@ def get_me(self):
172168
def get_file(self, file_id):
173169
return types.File.de_json(apihelper.get_file(self.token, file_id))
174170

171+
def download_file(self, file_path):
172+
return apihelper.download_file(self.token, file_path)
173+
175174
def get_user_profile_photos(self, user_id, offset=None, limit=None):
176175
"""
177176
Retrieves the user profile photos of the person with 'user_id'

‎telebot/apihelper.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
logger = telebot.logger
99

10+
API_URL = "https://api.telegram.org/bot{0}/{1}"
11+
FILE_URL = "https://api.telegram.org/file/bot{0}/{1}"
1012

11-
def _make_request(token, method_name, method='get', params=None, files=None):
13+
14+
def _make_request(token, method_name, method='get', params=None, files=None, base_url=API_URL):
1215
"""
1316
Makes a request to the Telegram API.
1417
:param token: The bot's API token. (Created with @BotFather)
@@ -18,10 +21,10 @@ def _make_request(token, method_name, method='get', params=None, files=None):
1821
:param files: Optional files.
1922
:return: The result parsed to a JSON dictionary.
2023
"""
21-
request_url = telebot.API_URL + 'bot' + token + '/' + method_name
24+
request_url = base_url.format(token, method_name)
2225
logger.debug("Request: method={0} url={1} params={2} files={3}".format(method, request_url, params, files))
2326
result = requests.request(method, request_url, params=params, files=files)
24-
logger.debug("The server returned: '{0}'".format(result.text))
27+
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
2528
return _check_result(method_name, result)['result']
2629

2730

@@ -61,11 +64,22 @@ def get_me(token):
6164
method_url = 'getMe'
6265
return _make_request(token, method_url)
6366

67+
6468
def get_file(token, file_id):
6569
method_url = 'getFile'
6670
return _make_request(token, method_url, params={'file_id': file_id})
6771

6872

73+
def download_file(token, file_path):
74+
url = FILE_URL.format(token, file_path)
75+
result = requests.get(url)
76+
if result.status_code != 200:
77+
msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]'\
78+
.format(result.status_code, result.reason, result.text)
79+
raise ApiException(msg, 'Download file', result)
80+
return result.content
81+
82+
6983
def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None,
7084
parse_mode=None):
7185
"""

0 commit comments

Comments
 (0)