Created
March 6, 2024 12:40
-
-
Save tomorgan/b9f78f98a157aa1aef773a8f84b1c5a4 to your computer and use it in GitHub Desktop.
Amazon Connect - Find Incidents in SNOW
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
const https = require('https'); | |
const AWS = require('aws-sdk'); | |
var ssm = new AWS.SSM(); | |
var params; | |
var username; | |
var password; | |
var host; | |
var userId; | |
var response; | |
exports.handler = async (event) => { | |
// These paramater paths are from the CTI integration and are stored in Systems Manager. Replace with your values. | |
username = await getParameter("/com.servicenow.cti/.../service_account_user"); | |
password = await getParameter("/com.servicenow.cti/.../service_account_password"); | |
host = await getParameter("/com.servicenow.cti/.../host"); | |
// Save the sysId. | |
userId = event.Details.Parameters.sysId; | |
var data = await getRequest(userId); | |
//validate only 1 incident is returned | |
if (data.result.length > 0 && data.result.length < 2) { | |
console.log("Returned incident is", data.result) | |
//remove any unnecessary detials from the commets | |
var comments = data.result[0]['comments'] | |
const regex = /\n(.*?)\n/g; | |
let matches = []; | |
let match; | |
while ((match = regex.exec(comments)) !== null) { | |
matches.push(match[1]); | |
} | |
//return incident number, comments and state | |
response = { | |
incident: data.result[0]['number'], | |
comments: matches[0], | |
state: data.result[0]['state'] | |
}; | |
return response; | |
} else { | |
response = { | |
incident: "NO_INCIDENT", | |
}; | |
return response; | |
} | |
function getRequest(userId) { | |
const options = { | |
host: host, | |
//Replace hardcoded sysId with the variable you defined on line 19 | |
path: "/api/now/table/incident?sysparm_query=state%3D1%5EORstate%3D2%5EORstate%3D3%5Ecaller_id%3D" + | |
userId + "&sysparm_fields=number%2C%20state%2C%20comments&sysparm_display_value=true", | |
method: "GET", | |
port: 443, | |
headers: { | |
'Content-Type': 'application/json', | |
'Accept': 'application/json', | |
'Authorization': 'Basic ' + Buffer.from(username + ":" + password).toString('base64'), | |
} | |
}; | |
return new Promise((resolve, reject) => { | |
const req = https.request(options, res => { | |
let rawData = ''; | |
res.on('data', chunk => { | |
rawData += chunk; | |
}); | |
res.on('end', () => { | |
try { | |
resolve(JSON.parse(rawData)); | |
} catch (err) { | |
reject(new Error(err)); | |
} | |
}); | |
}); | |
req.on('error', err => { | |
reject(new Error(err)); | |
}); | |
req.end(); | |
}); | |
} | |
//Function to get parameters from System Manager parameter store. | |
async function getParameter(name) { | |
var parameter; | |
var params = { | |
Name: name, | |
WithDecryption: true | |
}; | |
const data = await ssm.getParameter(params).promise(); | |
console.log(JSON.stringify(data)); | |
parameter = data.Parameter.Value; | |
return parameter; | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment