Created
August 24, 2018 20:55
-
-
Save rbocchinfuso/d85cf33eeef42bad3a7d61ffd3c0e4c1 to your computer and use it in GitHub Desktop.
Slack Message Script Include
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ServiceNow Slack Message | |
// Script Include | |
// SlackMessage.js | |
// Rich Bocchinfuso - 2017/05/10 | |
var SlackMessage = Class.create(); | |
var iconurl = 'http://www.gb-advisors.com/wp-content/uploads/2016/06/servicenow-sm-icon.png'; | |
var emoji = ':warning:'; | |
var channel = '#tickets'; | |
var username = 'ServiceNow'; | |
var endpoint = 'SLACK-WEBHOOK-ENDPOINT'; | |
gs.print(iconurl); | |
gs.print(emoji); | |
gs.print(channel); | |
gs.print(username); | |
gs.print(endpoint); | |
SlackMessage.prototype = { | |
'initialize': function() { | |
if (gs.getProperty('slack_message.default_icon_url') != '') { | |
this.payload.icon_url = gs.getProperty('slack_message.default_icon_url'); | |
} | |
else if (gs.getProperty('slack_message.default_icon_emoji') != '') { | |
this.payload.icon_emoji = gs.getProperty('slack_message.default_icon_emoji'); | |
} | |
}, | |
'send': function (text, channel) { | |
// Set the text and channel (or fall back to defaults) | |
this.payload.text = text || this.payload.text; | |
this.payload.channel = channel || this.payload.channel; | |
// Encode the payload as JSON | |
var SNJSON = JSON; // Workaround for JSLint warning about using JSON as a constructor | |
var myjson = new SNJSON(); | |
var encoded_payload = myjson.encode(this.payload); | |
// Create and send the REST Message | |
var msg = new sn_ws.RESTMessageV2(); | |
msg.setEndpoint(this.endpoint); | |
msg.setHttpMethod(this.method); | |
msg.setRequestBody(encoded_payload); | |
var res = msg.execute(); | |
return res; | |
}, | |
'endpoint': endpoint, | |
'method': 'post', | |
'payload': { | |
'channel': channel, | |
'username': username, | |
'text': '', | |
'attachments': [] | |
}, | |
'type': 'SlackMessage' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment