Skip to content

Instantly share code, notes, and snippets.

@zonble
Last active December 28, 2016 17:58
Show Gist options
  • Save zonble/09cc047b1ae9b6cfdc87c5e4694a2fdc to your computer and use it in GitHub Desktop.
Save zonble/09cc047b1ae9b6cfdc87c5e4694a2fdc to your computer and use it in GitHub Desktop.
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.HttpResponseException
import static groovyx.net.http.ContentType.*
import groovy.json.JsonBuilder
def post_to_slack = {
token, text, channel, domain, username, icon_emoji ->
def site = "https://${domain}.slack.com/"
println site
def path = "/services/hooks/incoming-webhook"
def json_builder = new JsonBuilder()
def json_object = json_builder channel: channel, username: username, icon_emoji: icon_emoji, text: text
def json = json_builder.toString()
def postData = [payload: json]
def http = new HTTPBuilder(site)
try {
http.post( path: path, body: postData,
query: [token: token],
headers: ['Content-Type': 'application/x-www-form-urlencoded']
) { resp ->
println "POST Success: ${resp.statusLine}"
}
} catch (groovyx.net.http.HttpResponseException e) {
r = e.response
println("Success: $r.success")
println("Status: $r.status")
println("Reason: $r.statusLine.reasonPhrase")
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment