Search code examples
aws-lambdaamazon-lexamazon-connect

Amazon Connect - Invoke Lambda


get_customer_input ReasonForCalling_Play_prompt

Lambda function returns properly when testing Lex chatbot. Invoking the function through Amazon Connect results in an Error. Any ideas on how to save the return from Lambda in Connect with proper formatting? * Updated to add updated lambda code and Lex configuration image.

Lambda Code:

    console.log('Loading event');
    var AWS = require('aws-sdk');
    var db = new AWS.DynamoDB({ apiVersion: '2012-08-10' });


    exports.handler = (event, context, callback) => {

        var ssn = event.currentIntent.slots.userSSN;
        var mySecret = event.currentIntent.slots.secretWord;


        var params = {

            TableName: 'users',
            Key: {
                "fourDigSSN": {
                    "N": ssn
                },
                "mySecretWord": {
                    "S": mySecret
                }
            },
            AttributesToGet: ["accountBalance"]
        };

        db.getItem(params, function(err, data) {
            if (err) {
                console.log(" It didn't work and here is the error " + err); // an error occurred
            }
            else
                callback(null, {
                    "sessionAttributes":{
                        "accountBal": data.Item.accountBalance.N
                    },
                    "dialogAction": {
                        "type": "ConfirmIntent",
                        "message": {
                            "contentType": "PlainText",
                            "content": "Your account has been verified. Your account balance is " + data.Item.accountBalance.N + "."

                        },
                        "intentName": "ReasonForCalling",
                        "slots": {
                            "userSSN": ssn,
                            "secretWord": mySecret
                        }
                    }

                });
                // var accountBal = data.Item.accountBalance.N;
                // //console.log("GetDBItem succeeded:", JSON.stringify(data, null, 2));
                // callback(null, {accountBalance : accountBal});
        });



    };

[lex_configuration][1]
[connect_contact_flow][2]
[connect_lambda_Details][3]

Solution

  • Your Amazon Connect contact flow appears to be referencing $.External.(something) after matching the "ReasonForCalling" intent. $.External is only used if Amazon Connect is calling upon a Lambda function directly. In your case, Amazon Connect is interacting with Lex. Lex is calling Lambda, so any response you get back to Amazon Connect will be coming from Lex, not Lambda. In order for Amazon Connect to read back something from Lex, you will need to use one of the following in your Play prompt step:

    • $.Lex.IntentName
    • $.Lex.Slots.(slotName)
    • $.Lex.SessionAttributes.(sessionAttributeKey)

    Option1: You can configure Lex to insert the Account Balance into a slot called "AccountBal", then "return parameters to client". From there, Amazon Connect can access that value as $.Lex.Slots.AccountBal.

    Option2: If you want Amazon Connect to interact directly to Lambda, you can use Lex to collect userSSN and secretWord and then build a separate Lambda function that Amazon Connect calls directly using an "Invoke AWS Lambda Function" step to perform the database lookup. With this method, you would be receiving a response directly from Lambda and can reference it as $.External.accountBalance.

    EDITED ----------

    Your callback has DialogAction type set as "ConfirmIntent" so Lex is most likely still expecting some user response. I doubt Amazon Connect is even getting anything back from Lex. Try updating the callback to something like the following so Lex will finish fulfillment and return to Amazon Connect:

    callback(null, {
        "sessionAttributes":{
            "accountBal": data.Item.accountBalance.N
        },
        "dialogAction": {
            "type": "Close",
            "fulfillmentState": "Fulfilled",
            "message": {
                "contentType": "PlainText",
                "content": "Your account has been verified. Your account balance is " + data.Item.accountBalance.N + "."
    
            }
        }
    
    });