From dd1a926b209782d8d4eccdf81a1d66a2a9239651 Mon Sep 17 00:00:00 2001 From: Caique Lima Date: Mon, 12 Oct 2015 20:11:25 -0300 Subject: [PATCH] Add file upload method Add post_file method to handle files.upload api call --- pyslack/__init__.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/pyslack/__init__.py b/pyslack/__init__.py index 2be0a97..40acbce 100644 --- a/pyslack/__init__.py +++ b/pyslack/__init__.py @@ -1,8 +1,8 @@ import datetime import logging +import os.path import requests - class SlackError(Exception): pass @@ -20,7 +20,7 @@ def __init__(self, token, verify=False): def _channel_is_name(self, channel): return channel.startswith('#') - def _make_request(self, method, params): + def _make_request(self, method, params, files = None): """Make request to API endpoint Note: Ignoring SSL cert validation due to intermittent failures @@ -33,7 +33,7 @@ def _make_request(self, method, params): url = "%s/%s" % (SlackClient.BASE_URL, method) params['token'] = self.token - response = requests.post(url, data=params, verify=self.verify) + response = requests.post(url, data=params, files=files, verify=self.verify) if response.status_code == 429: # Too many requests @@ -108,6 +108,25 @@ def chat_update_message(self, channel, text, timestamp, **params): }) return self._make_request(method, params) + def post_file(self, file_path, **params): + """files.upload + + This method allows you to create or upload an existing file. + + Required parameters: + `file_path`: Uploaded file full path + + https://api.slack.com/methods/files.upload + """ + + method = 'files.upload' + + if os.path.isfile(file_path): + with open(file_path, 'rb') as f: + files = {'file': f} + return self._make_request(method, params, files) + else: + raise SlackError("File not found") class SlackHandler(logging.Handler): """A logging handler that posts messages to a Slack channel!