I am invoking a Lambda function that retrieves data from ServiceNow via an API call within the Lambda. I have tested the code using call flows withing Amazon Connect, but when trying to utilise the Lambda Test Functionality, it succeeds, but the response returned is null and expect at least a name to be returned.
The input from Amazon Connect to the Lambda Function is a telephone number and I have tried adding this to the parameters section and the customerEndpointAddress section.
const https = require('https');
//Get Phone Details of Customer via Typed in Phone Number or Actual Phone Number
const getPhone = contact => {
const phone = contact.Details.ContactData.CustomerEndpoint.Address;
console.log(`Customer Phone is : ${phone}`);
return phone.length === 13 ? `0${phone.substring(3)}` : phone;
}
//Set API config, passing in the Phone Parameter as query and return both firstname and SysId
const getPhoneRequestOptions = phone => {
const path = `/api/now/table/sys_user?sysparm_query=phone%3D${phone}^ORmobile_phone%3D${phone}&sysparm_fields=first_name,sys_id`;
return {
host: process.env.SERVICENOW_HOST,
port: '443',
path: path,
method: 'get',
headers: {
"Content-Type": 'application/json',
Accept: 'application/json',
Authorization: 'Basic ' + Buffer.from(`${process.env.SERVICENOW_USERNAME}:${process.env.SERVICENOW_PASSWORD}`).toString('base64'),
}
};
};
//Retrieve data, in this case firstname and SysId
const requestUser = (phone, callback) => {
let get_request = https.request(getPhoneRequestOptions(phone), res => {
let body = '';
res.on('data', chunk => {body += chunk});
res.on('end', () => {callback(JSON.parse(body))});
res.on('error', e => {callback(e.message)});
})
get_request.end();
}
//Return data
exports.handler = (contact, context, callback) => {
if (!contact.Details || !contact.Details.Parameters) return;
requestUser(getPhone(contact), response => {
if (response.result && response.result[0] && response.result[0].first_name) {
callback(null, {
"first_name": response.result[0].first_name
});
} else {
callback(null, {
"Error": "No user found"
});
}
});
};
and the test code I have used is:
{
"Details": {
"ContactData" :{
"CustomerEndPoint" : {
"Address" : "01234567890"
}
}
}
}
When the code has been Invoked, the name "Abel" is returned within Amazon Connect, but its' not the case when running the test case against it.
Aucun commentaire:
Enregistrer un commentaire