Skip to main content
Book your demo
Example scripts
To homepage
Jira
Cloud icon
Cloud

Post to slack when Issue Created

Features
Listeners
Created 12 month(s) ago, Updated 16 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira
Language |
groovy
// Specify the key of the work item to get the fields from
def workItemKey = issue.key

// Get the work item summary, and description
def workItemObj = WorkItems.getByKey(workItemKey)
def summary = workItemObj.getSummary()
def description = workItemObj.getDescription()

// Specify the name of the slack room to post to
def channelName = '<ChannelNameHere>'

// Specify the name of the user who will make the post
def username = '<UsernameHere>'

// Specify the message metadata
Map msg_meta = [ channel: channelName, username: username ,icon_emoji: ':rocket:']

// Specify the message body which is a simple string
Map msg_dets = [text: "A new work item was created with the details below: \n Work Item key = ${workItemKey} \n Work Item Summary = ${summary} \n Work Item Description = ${description}"]

// Post the constructed message to slack
def postToSlack = post('https://slack.com/api/chat.postMessage')
    .header('Content-Type', 'application/json')
    .header('Authorization', "Bearer ${SLACK_API_TOKEN}") // Store the API token as a script variable named SLACK_API_TOKEN
    .body(msg_meta + msg_dets)
    .asObject(Map)
    .body

assert postToSlack : "Failed to create Slack message check the logs tab for more details"
Having an issue with this script?
Report it here