1

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

enter image description here

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) => {
        
  let MembershipNumber = event.currentIntent.slots.MembershipNumber;
  
  // 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, {
                    "dialogAction": {
                    "type": "ConfirmIntent",
                    "fulfillmentState": "Fulfilled",
                    "message": {
                    "contentType": "PlainText",
                    "content": 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

1 Answers1

0

From the error message, the response JSON from lambda is not matching with what Lex expects. As per the error message, it appears that this line has the problem:

"content": FirstName,LastName,Address,PrimaryPhone,SecondaryPhone,Email,Type,MembershipNumber,Pin

The value of content needs to be a string. However, from your screenshot, this is getting resolved as "content": "alys", "Lastname",...

I believe what you want to do is a concatenation of FirstName, LastName and all the other things you want to return in the content. Use a correct function that will do concatenation of these fields and then put them as comma-separated.

Reference: https://docs.aws.amazon.com/lex/latest/dg/lambda-input-response-format.html

aerial
  • 1,188
  • 3
  • 7
  • 15
MVS
  • 66
  • 5