Friday, 30 November 2018

Scripted REST API to Check Parking availability 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=='parking_availability')
{
var ub ='',lb='',slot_count,flag,company1,city1;
city_name = city_name.toLowerCase();
if(city_name == "banglore" || city_name == "bglr" || city_name == "bangaluru")
company = company.toLowerCase(); { city1 = "Bangalore"; }
if(company == "cts" || company == "cognizant technology solution")
else { city1 = city_name; } { company1 = "Cognizant";
gs.log("@@@"+company1);
} else { company1 = company; } gs.log("@@@"+city1);
g_parking.addQuery('u_city',city1);
var g_parking = new GlideRecord('u_light_and_parking_space_track'); g_parking.addQuery('u_company',company1);
{
g_parking.addQuery('u_building',building); g_parking.addQuery('u_status', 'false'); g_parking.query(); while(g_parking.next()) flag = "1";
ub_slots = ub.substring(0, ub.length - 1);
slot_count=g_parking.getRowCount(); if(g_parking.u_upper_basement == "UB") { ub= ub+"UB"+g_parking.u_slot_no+","; } if(g_parking.u_lower_basement == "LB") {
var message1="Total Number available slot "+ slot_count + "! Upper Basement Empty Slots are: " + ub_slots + " and Lower Basement Empty slots are: "+lb_slots+ "! Click on the below URL to go to that Location!";
lb= lb+"LB"+g_parking.u_slot_no+","; lb_slots = lb.substring(0, lb.length - 1); } } if(flag == "1") {
}
var message2 ="<http://www.google.com/maps/place/>"+g_parking.u_lattitude+","+g_parking.u_lognitude; message = message1 + message2; } else { message= "There is no parking available!"; }

/*****************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);

1 comment:

  1. Your blog had very good knowledge and that gives huge instructions and that was really commendable ideas you have good knowledge on this topic please share more information with us. Asphalt Paving Companies

    ReplyDelete