# Post a message to Slack from Jira Cloud

**URL:** https://forum.library.adaptavist.com/t/post-a-message-to-slack-from-jira-cloud/37
**Category:** Scripts
**Tags:** slack, jira, cloud
**Created:** [March 4, 2022, 7:21pm UTC](https://forum.library.adaptavist.com/t/post-a-message-to-slack-from-jira-cloud/37 "2022-03-04T19:21:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![kwalker](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.library.adaptavist.com/kwalker/32/15_2.png) [@kwalker](https://forum.library.adaptavist.com/u/kwalker)
#### Post date: [March 4, 2022, 7:21pm UTC](https://forum.library.adaptavist.com/t/post-a-message-to-slack-from-jira-cloud/37/1 "2022-03-04T19:21:24Z")

</div>

# Overview

This script allows you to post a message to Slack in the format specified, along with issue details.

# Example

As a project manager, I want a message be posted on Slack when an issue has been done. So everyone is informed when an issue has been finished.

# Good to Know

- A Slack API token need to be created: [Slack Help Centre | Slack](https://slack.com/intl/en-gb/help/articles/215770388-Create-and-regenerate-API-tokens)

```auto
// Specify the issue key.
def issueKey = '<IssueKeyHere>'

//Get the issue
def issueResp = get("/rest/api/2/issue/${issueKey}")
        .header("Content-Type", "application/json")
        .asObject(Map)

assert issueResp.status == 200

def issue = issueResp.body

// Get the issue summary.
def summary = issue.fields.summary

// Get the issue description.
def description = issue.fields.description

// Specify the name of the Slack room you want to post to.
def channelName = '<ChannelName>'

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

// 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 issue was created with the details below: \n Issue key = ${issueKey} \n Issue Sumamry = ${summary} \n Issue 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"

return "Slack message created successfully"

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/adaptavistlibrary/original/1X/ceb18adf14fa58b4c6de03aa69ada8b07359620c.png) [@system](https://forum.library.adaptavist.com/u/system)
#### Post date: [August 31, 2022, 7:21pm UTC](https://forum.library.adaptavist.com/t/post-a-message-to-slack-from-jira-cloud/37/2 "2022-08-31T19:21:31Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
