Slack Connection
Follow these instructions to create a connection to Slack.
Create Slack App
First, you will need to navigate to Slack API to create an app with a name and a Slack domain. Here we walk through creating a simple slack app which allows a bot to post a message to a user.
Tip: We suggest setting up your Slack app on a development workspace first.
Set up the Slack Resource
Slack Examples
Having set up your Slack connection, you can now use it in your ScriptRunner scripts using the resource Connection Name.
Using Blocks and Attachments
The following example sends a message using blocks and attachments. In this case the message text provided is used as a fallback option.
import com.onresolve.scriptrunner.slack.SlackUtil
SlackUtil.message("slack",
"acme-developers",
"This is the fallback message text",
[
username : "ScriptRunner Bot",
blocks : [
[
type: 'section',
text: [
type: 'plain_text',
text: 'Message text'
]
]
],
attachments: [
[
pretext: 'Introductory text',
text : 'Text message as part of attachment'
],
]
]
)Tip: Use the block kit builder to preview your messages.
Uploading a File
Upload file to a Slack channel. Scope files:write is required. You can either upload a file to a channel (if the bot user is part of this channel) or to a specific user:
import com.onresolve.scriptrunner.slack.SlackUtil
SlackUtil.upload("slack",
"acme-developers",
new File("/tmp/file.txt"),
[
text : 'Fallback text',
initial_comment: 'Comment about the file being uploaded',
]
)Note: To send an attachment the app must be added to the channel, which you can do by clicking the Add apps link from the channel details, then the More menu.
Tip: The full list of allowed parameters can be found here.