Invalid Lambda Response: Received error response from Lambda: Unhandled

坚强是说给别人听的谎言 提交于 2020-07-22 07:21:10

问题


i am working on Amazon lex. when i give input to amazon lex its gives me following error

Invalid Lambda Response: Received error response from Lambda: Unhandled

i add lambda function in Fulfillment and from lambda i am getting customer information from MySQL Database

here is my lambda function

const mysql = require('mysql');
var pool  = mysql.createPool({ //  a connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required
   host: process.env.RDS_HOSTNAME,
   user: process.env.RDS_USERNAME,
   password: process.env.RDS_PASSWORD,
   port: process.env.RDS_PORT,
  database: process.env.RDS_DATABASE
});

  exports.handler = (event, context, callback) => {
        callback(null, {
        "dialogAction": {
        "type": "ConfirmIntent",
        "fulfillmentState": "Fulfilled",
        "message": {
        "contentType": "PlainText",
        }
    }
});
  //let phone_number = event.Details.ContactData.CustomerEndpoint.Address;
  let MembershipNumber = event.currentIntent.slots.MembershipNumber;
  //console.log(phone_number);
  
  // allows for using callbacks as finish/error-handlers
  context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
    if (err) throw err;
    let sql = "select * from Sales.CustomerTable where MembershipNumber= ? LIMIT 1";
    let field = [MembershipNumber];
    connection.query(sql,field, function (err, result, fields) {
    if (err) {
            console.log(err); // an error occurred
            context.fail(buildResponse(false));
        } 
        else {
            if (result.length === 1) {
                //console.log(data.Items[0].Arrival_city);
                var FirstName = result[0].FirstName;
                var LastName = result[0].LastName;
                var Address = result[0].Address;
                var PrimaryPhone = result[0].PrimaryPhone;
                var SecondaryPhone = result[0].SecondaryPhone;
                var Email = result[0].Email;
                var Type = result[0].Type;
                var MembershipNumber = result[0].MembershipNumber;
                var Pin = result[0].Pin;
                callback(null, buildResponse(true, FirstName, LastName, Address, PrimaryPhone, SecondaryPhone, Email, Type, MembershipNumber, Pin));
            } 
            else {
                console.log("Membership Number not found");
                callback(null, buildResponse(true, "none"));
            }
        }
    });
  });
};
function buildResponse(isSuccess, FirstName, LastName, Address, PrimaryPhone, SecondaryPhone, Email, Type, MembershipNumber, Pin) {
    if (isSuccess) {
        return { 
            FirstName: FirstName,
            LastName: LastName,
            Address: Address,
            PrimaryPhone: PrimaryPhone,
            SecondaryPhone: SecondaryPhone,
            Email:Email,
            Type:Type,
            MembershipNumber:MembershipNumber,
            Pin:Pin,
            lambdaResult: "success"
        };
    } 
    else {
        console.log("Lambda returned error to Connect");
        return { lambdaResult: "Error" };
    }
}

here is the test event object

{
  "messageVersion": "1.0",
  "invocationSource": "FulfillmentCodeHook",
  "userId": "user-1",
  "sessionAttributes": {},
  "bot": {
    "name": "Sales",
    "alias": "sales",
    "version": "$LATEST"
  },
  "outputDialogMode": "Text",
  "currentIntent": {
    "name": "iamamember",
    "slots": {
      "MembershipNumber": "78541258"
    },
    "confirmationStatus": "None"
  }
}

i want to integrate this with my contact flow. i don't what is this error about. If anyone of you know help me. Thanks

来源:https://stackoverflow.com/questions/62700757/invalid-lambda-response-received-error-response-from-lambda-unhandled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!