Comcast
Design Document for Slack
– SeviceNow Integration
For ServiceNow (Version: Helsinki)
1. Document History
Date |
Version |
Author |
Associate
id |
Reason |
Revision
Details |
10/24/2016 |
1.0 |
Ujjwal Kumar |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Table
of Contents
4.3 For inbound request coming to servicenow
4.4 For outbound requests from servicenow to slack
1.0
Introduction
1.1 Purpose
The purpose of this design document is to capture
the overall architecture and functionality that will be configured for the Slack
integration with ServiceNow version of Helsinki release.
1.2
Description
The
integration between Slack and ServiceNow is to incorporate bi-directional
request from either platforms. For Inbound request coming to ServiceNow,
Scripted REST API (on ServiceNow platform) and Slash Command(on Slack) can be
used. For outbound requests from ServiceNow, REST messages(on ServiceNow
platform) and Incoming webhook(on Slack)
can be used.
2.0
Scope
Definition
The scope of this document is to capture and document
functional and technical overview of slack integration with the ServiceNow
system.
3.0
Acronym
and Definitions
Term |
EXPANSION |
WSDL |
Web Service
Defination Language |
REST |
Representational State Transition |
4.0
Integration Architecture
4.1
System Overview
Slack
will send a request to get the details of Change tickets from ServiceNow and
will consume the webservices configured in ServiceNow instance.
Whenever
the assignee of the change ticket is updated on ServiceNow, ServiceNow will
update the slack channel about the assignee change .
4.2
Integration Overview
Slack integration with ServiceNow is Bi-Direction.
When a Slack user wants to get the details of a
ServiceNow change ticket, a slash command is used in Slack followed by change
ticket number. Below is an example -
When there is an assignee change in ServiceNow change
ticket, Slack channel will be updated. Below is an example-
4.3 For
Inbound requests coming to ServiceNow
· Configuration done on ServiceNow-
· Create
a Scripted REST service. Below is an example-
· Create
a scripted REST resource. Below is an example
· Below
is the script in the scripted REST resource-
(function
process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
//gs.log('### inside Scripted rest
API');
var returnObj = {
//response_type:
"ephemeral",
response_type:
"in_channel",
text:
"declared"
};
var JSON = new global.JSON();
//var auth = false;
var instance = 'https://' + gs.getProperty('instance_name')
+ '.service-now.com/';
var responseText = '';
//var user = '';
try{
var text =
request.queryParams.text.toString();
//sn sys_user searchterm
var textArr =
text.split(' ');
var
chgNumber=textArr[0].toString().trim();
var
chgNum=chgNumber.toLowerCase();
//gs.log('### change
number'+chgNum);
if(chgNum!=''){
if(chgNum.search('chg')>=0
|| chgNum.search('changev')>=0){
var gr= new
GlideRecord('change_request');
if(chgNum.search('chg')>=0){
gr.addQuery('number',chgNum);
}else{
gr.addQuery('u_external_ticket_',chgNum);
}
gr.query();
if(gr.next()){
var
returnStr = '';
returnStr
+= '<' + instance + gr.getLink() + '|' + gr.number.toString() + '> -
'+gr.short_description;
returnStr
+= '\n *Planned Start Date *: '+gr.start_date.getDisplayValue();
returnStr
+= '\n *Planned End Date *: '+gr.end_date.getDisplayValue();
returnStr
+= '\n *State *: '+gr.state.getDisplayValue();
returnStr
+= '\n *Assignee *: '+gr.assigned_to.getDisplayValue();
returnStr
+= '\n *Risk Assessment Score *: '+gr.u_risk_assessment_score;
returnStr
+= '\n *Description *: '+gr.description;
responseText=returnStr;
}else{
responseText='Could
not find the '+chgNumber +' in Service Now';
}
}else{
responseText='Could
not find the '+chgNumber +' in Service Now';
}
}else{
responseText='You
need to add the change ticket number after /getchange';
}
returnObj.text =
responseText;
}catch(e){
gs.log('Error in
getchange (scripted rest resource) : '+ e);
returnObj.text = e;
}
response.setContentType('application/json');
response.getStreamWriter().writeString(JSON.encode(returnObj));
})(request,
response);
· Configuration done on Slack-
Create
a slash command on slack by using below steps-
1. Go
to link {slack url}/apps
2. Search
for slash commands
3. Configure
a slash command. Below is an example-
The
URL field in the above slash command will be setup as
{ServiceNow
Url}/ {Resource path in ServiceNow REST resource}
4.4 For
Outbound requests from ServiceNow to Slack
· Configuration done on Slack-
Create an incoming Webhook by following below
steps
1. Go
to link {slack url}/apps
2. Search
for incoming webhook
3. Configure
an incoming webhook. Below is an example-
The
Webhook URL will be autogenerated once the webhook is created.
This
URL will be used as a RES message end point in ServiceNow.
· Configuration done on ServiceNow-
o Create
a REST Message(Slack integration) with End point same as the webhook URL in the
incoming webhook of Slack.
o Create
a Script include ‘SlackMessageNew’ with below script in it.
var
SlackMessageNew = Class.create();
SlackMessageNew.prototype
= {
initialize: function() {
},
send: function (text, channel) {
var chgObj = new
Object();
chgObj.text =text;
var json = new JSON();
var body =
json.encode(chgObj);
// Create and send the
REST Message
try {
var r = new
sn_ws.RESTMessageV2('Slack integration', 'post');
r.setRequestBody(body);
var response
= r.execute();
var responseBody
= response.getBody();
var
httpStatus = response.getStatusCode();
}
catch(ex) {
var message
= ex.getMessage();
}
},
type: 'SlackMessageNew'
};
o Create
a business rule with below details-
When- After
Insert- true
Update- true
Filter Condition- Assigned to changes
Script-
(function executeRule(current, previous /*null
when async*/) {
//
Add your code here
var
slack = new SlackMessageNew();
var
instance = 'https://' + gs.getProperty('instance_name') + '.service-now.com/';
var
chgLink='<' + instance + current.getLink() + '|' + current.number.toString()
+ '>';
var
text=current.assigned_to.getDisplayValue() +'is the new assignee for '+chgLink;
//
Fire off the message
slack.send(text,'#change-slack-snow');
})(current, previous);
No comments:
Post a Comment