Friday, 30 November 2018

Scripted REST API to Check Incident status in ServiceNow using Google Assistant

Scripted REST API:

(function process(/*REST API Request*/ request, /*REST API Response*/ response){

var state,assignTo,short_desc,nmbr,description,urgency,impact,priority,assignGroup,message;

/***************Recieve data from Google assistant for Incident***************/

var resWriter=response.getStreamWriter();
var headers={};
var responseobj = {};
var requestBody = request.body;
var requestobj = requestBody.data;
var displayName=requestobj.queryResult.intent.displayName;
var incident_number = requestobj.queryResult.parameters.incident_number;
var city_name = requestobj.queryResult.parameters.geo_city;
var company = requestobj.queryResult.parameters.companies;
var building = requestobj.queryResult.parameters.buildings;


short_desc = requestobj.queryResult.parameters.short_description;
var desc = requestobj.queryResult.parameters.description;
description=short_desc+desc;

/************************* Recieve Impact for Incident****************************************/

var imp=requestobj.queryResult.parameters.impact;
if(imp==1||"High" || "Hi"){
impact=imp;
}
if(imp==2||"medium"){
impact=imp;
}
if(imp==3||"low"){
impact=imp;
}
/************************* Recieve Urgency for Incident*************************************************/

var urg=requestobj.queryResult.parameters.urgency;
if(urg==1||"High" || "Hi"){
urgency=urg;
}
if(urg==2||"medium"){
urgency=urg;
}
if(urg==3||"low"){
urgency=urg;
}



if(displayName=='status_check')
{
var gr=new GlideRecord('incident');
//var query="'numberLIKE'+incident_number";
gr.addEncodedQuery("numberLIKE" +incident_number);
gr.query();
if(gr.next()){
state= gr.getDisplayValue('state');
assignTo=gr.assigned_to.getDisplayValue();
short_desc=gr.short_description;
nmbr=gr.number;
assignGroup=gr.assignment_group.getDisplayValue();
priority=gr.getDisplayValue('priority');

message= "Your Tncident "+nmbr+" of priority "+priority+" is in state "+ state +" and its currently assigned to "+ assignTo+assignGroup;
}
}



/*****************Send response back to Ok Google **********************/


headers['Content-Type']='application/json';
response.setStatus(200);
response.setHeaders(headers);
var response_body={
"fulfillmentText": message ,
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": message
}
}
]
}
}
}
};
resWriter.writeString(global.JSON.stringify(response_body));



})(request, response);

No comments:

Post a comment