Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,553,467
Community Members
 
Community Events
184
Community Groups

Scriptrunner script works in console but not in a listener

I am trying to create a Slack channel when a version is created. I created a script that that can create a slack channel from Jira. It works when executing it in the script console but not when I put it into a listener. The frustrating thing is that I am getting a 200 response in the listener logs. Nothing is changed between the console and listener scripts. The Slack bot is also that same. I am on Jira Datacenter.

What am I missing?

Script

import groovyx.net.http.ContentType

import groovyx.net.http.RESTClient

import groovyx.net.http.HttpResponseDecorator

import groovyx.net.http.RESTClient

final String URL = "https://slack.com/api/"

def channelName = "pretend-channel-4"

def channelPrivacy = "false" //true = private / false = public

def data = [:]

 

data.put("name", channelName)

data.put("is_private", channelPrivacy)

data.put("prety", "1")

def postResponse = post(URL, "conversations.create?", data)

def post(def hostUrl, def endpointAndQuery, def bodyJson) {

    //TOKEN FOR SLACK APP

    def SLACK_API_TOKEN = "<SLACK TOKEN>"

    def client = new RESTClient(hostUrl)

    client.setHeaders([

        'Authorization': "Bearer $SLACK_API_TOKEN"

    ])

    client.handler.success = { HttpResponseDecorator response ->

        log.debug("POST Success: $response.")

    }

    client.handler.failure= { HttpResponseDecorator response ->

        log.error "POST Error: $response.status"

    }

    client.post(

            path: endpointAndQuery,

            contentType: ContentType.JSON,

            body: bodyJson

    )  

}

 

Log screenshot

script logs.PNG

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 24, 2023

So, when executing in the listener, you get a log entry that shows the post request was successful but the corresponding slack channel isn't created?

It's possible for an API to respond with a success status code, but report a failure as part of the message.

Maybe you need to extract a bit more out of that response to get some clues. 

import groovyx.net.http.ContentType
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient

final String URL = "https://slack.com/api/"
def channelName = "pretend-channel-4"
def channelPrivacy = "false" //true = private / false = public

def data = [:]
data.put("name", channelName)
data.put("is_private", channelPrivacy)
data.put("prety", "1")

def postResponse = post(URL, "conversations.create?", data)

def post(def hostUrl, def endpointAndQuery, def bodyJson) {
//TOKEN FOR SLACK APP
def SLACK_API_TOKEN = "<SLACK TOKEN>"

def client = new RESTClient(hostUrl)
client.setHeaders([
'Authorization': "Bearer $SLACK_API_TOKEN"
])

client.handler.success = { HttpResponseDecorator response ->
log.debug("POST Status: $response.")
log.debug("POST Response: $response.entity.content.text")
}

client.handler.failure = { HttpResponseDecorator response ->
log.error "POST Error: $response."
}

client.post(
path: endpointAndQuery,
contentType: ContentType.JSON,
body: bodyJson
)

}

@Peter-Dave Sheehan Thanks for the direction. It looks like it is doing exactly what you say. The request is successful but I am getting 

POST Response: {"ok":false,"error":"invalid_name_specials","warning":"missing_charset","response_metadata":{"warnings":["missing_charset"]}}

do you know how to fix this? I have never had to define the ContentType other then what is currently in the script. From what I can tell it needs the Charset defined. 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 25, 2023

I don't think it'a content type error.

I haven't worked with Slack API before, but a quick google search revealed this:

https://api.slack.com/methods/conversations.create#errors

See the "invalid_name_specials" error.
That means your channel name "pretend-channel-4" is apparently invalid.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 25, 2023

And if you want to take care of the warning too... you can try adding something like this:

client.encoder.charset = 'utf-8'

But I'm not sure. I've never tried that. I just found it from the javadocs.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events