1

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

Tayyab
  • 329
  • 1
  • 16
  • There are quite a few errors in your code, some basic, most have to do with how you are formatting the response to Lex. You should get very familiar with this doc: https://docs.aws.amazon.com/lex/latest/dg/lambda-input-response-format.html – Jay A. Little Jul 11 '20 at 07:59
  • what are the errors, can you please solve that for me?? – Tayyab Jul 11 '20 at 08:36
  • Its not a simple, change this, add that. I would be rewriting a lot of it without knowing what the purpose of the bot is, and if we go back and forth so that I'm writing the code, I would be doing the whole job for you for free. So I politely decline solving all of your errors (writing the code for you). Learn what the code is doing, learn the response format that you need to pass in the callback, and start with exports.handler because that is immediately running the callback response with an incorrect format, causing your first error. – Jay A. Little Jul 11 '20 at 09:08
  • 1
    Also you can compare your code with the Lex-Lambda Blueprint to better understand where you want to write your code and how to format your responses properly: https://docs.aws.amazon.com/lex/latest/dg/lex-lambda-blueprints.html – Jay A. Little Jul 11 '20 at 09:10

0 Answers0