Set up the Slack Resource
Set up the Slack Resource
Navigate to ScriptRunner→ Resources→ Add New Item→ Slack Connection.
Provide a name for the connection in Connection Name. For example, slack or if you use multiple workspaces use the workspace name.
Enter the Bot OAuth Token for your Slack workspace in Token.
For help getting your Bot OAuth Token see Create a Slack App above.
The token provided can be viewed by administrators.
Click Preview to validate the connection using the token provided.
Click Add. Your Slack connection is now configured.
To test the connection, navigate to the ScriptRunner→ Script Console.
Use the simple script below to test that the bot can post to a user and/or a channel.
We recommend you test your slack connection using your own user details, or set up a test slack room.
When sending a message to a Slack channel the scope chat:write is required… for a public channel chat:write_public is required. To send a direct message the scope users:read.email is required.
import com.onresolve.scriptrunner.slack.SlackUtil SlackUtil.message( "slack", // Identifier you provided when creating the resource "acme-developers", // channel name or ID, or user email "Hi, this is the message text" // Message text )
Accepted values for the channel argument (the second argument) are public or private channel name (or ID), or a user’s email address, or ID, to send a direct message.
The full list of allowed parameters that can be used to format your Slack messages can be found here.
Remember that a Slack bot requires certain scopes to message a user and/or channel. If these test scripts fail, check the Bot Token Scopes under OAuth & Permissions for your Slack app.
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 with a customized username and avatar
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'
],
]
]
)
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',
]
)
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.
The full list of allowed parameters can be found here.