A Concourse resource for using the slack API implemented using either the legacy token or OAuth2 token returned when creating a slack app.
Note that this differs from the slack-notification-resource as it uses the slack API rather than the slack webhook, therefore allowing for use of all slack API functionality and not just message posting.
There is currently no ability to execute the OAuth2 workflow to create a token. Instead, you are expected to pass a working token. Three options:
- Use a 'legacy' token - https://api.slack.com/custom-integrations/legacy-tokens
- Using a slack app, set permission scopes and then use the OAuth Access Token
- Using a slack app, previous to invoking the resource, follow the OAuth2 workflow to get a token - https://api.slack.com/docs/oauth
- method - See list of methods
- token - See Authentication section
Example
resource_types:
- name: slack-file-upload
source:
token: REDACTED
method: files.upload
type: slack-api
This resource has not implemented all available slack API methods. Feel free to contribute!
- files.upload - https://api.slack.com/methods/files.upload
- chat.postMessage - https://api.slack.com/methods/chat.postMessage
Currently only supports text data.
- channels - comma separated list of channels
- content - the text that will be sent to the given channel(s) as a snippet and also stored as a file
- title - the title of the snippet and file
- file - the file, passed from a task or resource in the job, whose contents will be uploaded
- fallback_channel - an optional fallback channel to post to, in the case of channels archived or that do not exist
Note that you can ONLY use file
or content
, not both
Currently only supports posting attachments
property in the slack message, and not other top level properties.
- channel - specify what channel to post to
- attachments_file - the array of attachments to post, passed in as a JSON file from a task or resource in the job
- attachments - the array of attachments to post
- icon_url (optional) - url for the icon to use in post
- username (optional) - user to post as
- link_names (optional) - enables user/channel linking in attachment
- fallback_channel - an optional fallback channel to post to, in the case of channels archived or that do not exist
Note that you can ONLY use attachments_file
or attachments
, not both
Example attachments_file would be a path to a file with the following contents.
Or you could pass this in as the value of attachments directly:
[
{
"title": "test attachment 1",
"text": "test attachment 1 text"
}
]
Note that the tests are run as part of the Docker build, and the build will fail if any tests fail.
docker build -t slack-api-resource .
You can also test with the script end-to-end by starting that container in interactive mode via
docker run -it slack-api-resource bash
and then calling the script of your choice. As an example, to execute posting a message to some slack channel:
cd /opt/resource
export SLACK_TOKEN=REDACTED
export TEST_SLACK_CHANNEL=channel1
export TEST_FALLBACK_CHANNEL=fallbackchannel
echo '[{ "title": "test chat.postMessage attachment 1", "text": "test chat.postMessage attachment 1 text" }]' > msg_attachments.json
echo "{\"source\": { \"token\" :\"${SLACK_TOKEN}\", \"method\": \"chat.postMessage\" }, \"params\": { \"attachments_file\": \"msg_attachments.json\", \"channel\" : \"${TEST_SLACK_CHANNEL}\", \"fallback_channel\" : \"${TEST_FALLBACK_CHANNEL}\", \"icon_url\": \"http://cl.ly/image/3e1h0H3H2s0P/concourse-logo.png\", \"username\": \"concourse\", \"link_names\": 1}}" | ./out .
Another example using the 'files.upload' method:
cd /opt/resource
export SLACK_TOKEN=REDACTED
export TEST_SLACK_CHANNELS=channel1,channel2,channel3
export TEST_FALLBACK_CHANNEL=fallbackchannel
echo "{\"source\": { \"token\" :\"${SLACK_TOKEN}\", \"method\": \"files.upload\" }, \"params\": { \"channels\" : \"${TEST_SLACK_CHANNELS}\", \"fallback_channel\" : \"${TEST_FALLBACK_CHANNEL}\", \"content\": \"some content\", \"title\": \"Title for files.upload\"}}" | ./out .
Currently you'll need to build and push to your local registry until we build support for this in the public docker registry.