Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file upload method #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions pyslack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import datetime
import logging
import os.path
import requests


class SlackError(Exception):
pass

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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!
Expand Down