Slack Connection
A Slack connection allows you to send messages and upload files to Slack channels and users directly from your ScriptRunner scripts.
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.
Set up the Slack Resource
The full list of allowed parameters that can be used to format your Slack messages can be found here.
- chat:write: send messages to channels
- chat:write.public: send messages to channels the bot user user isn't a member of.
- files:write: upload files
- users:read and users:read.email: send direct messages to a user
- chat:write.customize: send messages as bot user
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'
],
]
]
)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',
]
)