mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Added download_file
This commit is contained in:
parent
fd1f16598b
commit
1038d4fafa
@ -22,10 +22,6 @@ from telebot import apihelper, types, util
|
||||
"""
|
||||
Module : telebot
|
||||
"""
|
||||
|
||||
API_URL = r"https://api.telegram.org/"
|
||||
|
||||
|
||||
class TeleBot:
|
||||
""" This is TeleBot Class
|
||||
Methods:
|
||||
@ -172,6 +168,9 @@ class TeleBot:
|
||||
def get_file(self, file_id):
|
||||
return types.File.de_json(apihelper.get_file(self.token, file_id))
|
||||
|
||||
def download_file(self, file_path):
|
||||
return apihelper.download_file(self.token, file_path)
|
||||
|
||||
def get_user_profile_photos(self, user_id, offset=None, limit=None):
|
||||
"""
|
||||
Retrieves the user profile photos of the person with 'user_id'
|
||||
|
@ -7,8 +7,11 @@ from telebot import util
|
||||
|
||||
logger = telebot.logger
|
||||
|
||||
API_URL = "https://api.telegram.org/bot{0}/{1}"
|
||||
FILE_URL = "https://api.telegram.org/file/bot{0}/{1}"
|
||||
|
||||
def _make_request(token, method_name, method='get', params=None, files=None):
|
||||
|
||||
def _make_request(token, method_name, method='get', params=None, files=None, base_url=API_URL):
|
||||
"""
|
||||
Makes a request to the Telegram API.
|
||||
: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):
|
||||
:param files: Optional files.
|
||||
:return: The result parsed to a JSON dictionary.
|
||||
"""
|
||||
request_url = telebot.API_URL + 'bot' + token + '/' + method_name
|
||||
request_url = base_url.format(token, method_name)
|
||||
logger.debug("Request: method={0} url={1} params={2} files={3}".format(method, request_url, params, files))
|
||||
result = requests.request(method, request_url, params=params, files=files)
|
||||
logger.debug("The server returned: '{0}'".format(result.text))
|
||||
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
|
||||
return _check_result(method_name, result)['result']
|
||||
|
||||
|
||||
@ -61,11 +64,22 @@ def get_me(token):
|
||||
method_url = 'getMe'
|
||||
return _make_request(token, method_url)
|
||||
|
||||
|
||||
def get_file(token, file_id):
|
||||
method_url = 'getFile'
|
||||
return _make_request(token, method_url, params={'file_id': file_id})
|
||||
|
||||
|
||||
def download_file(token, file_path):
|
||||
url = FILE_URL.format(token, file_path)
|
||||
result = requests.get(url)
|
||||
if result.status_code != 200:
|
||||
msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]'\
|
||||
.format(result.status_code, result.reason, result.text)
|
||||
raise ApiException(msg, 'Download file', result)
|
||||
return result.content
|
||||
|
||||
|
||||
def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None,
|
||||
parse_mode=None):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user