0

我正在调用一个 Lambda 函数,该函数通过 Lambda 中的 API 调用从 ServiceNow 检索数据。我已经使用 Amazon Connect 的调用流测试了代码,但是在尝试使用 Lambda 测试功能时,它成功了,但返回的响应为空,并且期望至少返回一个名称。

从 Amazon Connect 到 Lambda 函数的输入是电话号码,我尝试将其添加到参数部分和 customerEndpointAddress 部分。

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"
            });
        }
    });
};

我使用的测试代码是:

{
  "Details": {
   "ContactData" :{
       "CustomerEndPoint" : {
           "Address" : "01234567890"
       }
   }
  }
}

调用代码后,名称“Abel”会在 Amazon Connect 中返回,但在针对它运行测试用例时并非如此。

4

1 回答 1

0

这是因为这条线:

if (!contact.Details || !contact.Details.Parameters) return;

在您使用的测试事件Details中没有该属性Parameters(仅ContactData)。这会导致您返回而不用返回值。

于 2019-06-22T07:11:43.053 回答