Search code examples
node.jslambdatwilioamazon-lex

How to call a Lambda function within a another Lambda function using Lex?


I am playing with AWS Lambda along with Twilio. I have a Lambda function that integrates Lex with Twilio. I also have another Lambda function that does the the validations for my LexBot. Both work fine separately. However, I'm trying to put them together so whenever my LexBot integrates with Twilio, it also calls my validations in the same Lambda function.

Any ideas? Thank you.

Here is the Lambda that integrates Lex with Twilio:

var twilio = require('twilio');
var qs = require('qs');
var AWS = require('aws-sdk');

exports.handler = (event, context, callback) => {
    try {
        var twilioSMS = qs.parse(event["body-json"]);
        //  ************************
        //  validate and filter bad/empty messages
        //  ************************
        if(!twilioSMS.hasOwnProperty('Body')){
            var error = new Error("Cannot process message without a Body.");
        callback(error);
    }
    else {
            //  Message is valid so now we prepare to pass it along to the Lex API.
            AWS.config.region = 'us-east-1';
            var lexruntime = new AWS.LexRuntime();
            var userNumber = twilioSMS.From.replace('+', '');
            var params = {
              botAlias: process.env.BOT_ALIAS,
              botName: process.env.BOT_NAME,
              inputText: twilioSMS.Body,
              userId: userNumber,
              sessionAttributes: {
              }
            };
            lexruntime.postText(params, function(err, data) {
                var twimlResponse = new twilio.TwimlResponse();
              if (err) {
                    console.log(err, err.stack); // an error occurred
              twimlResponse.message('Sorry, we ran into a problem at our end.');
              callback(err, twimlResponse.toString());
                } else {
                    console.log(data);           // got something back from Amazon Lex
                    twimlResponse.message(data.message);
            callback(null, twimlResponse.toString());
                }
            });
    }
} catch(e) {
    console.log(e);
    callback(e);
}
};

And here is my Lambda with the validations:

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

var numberType =event.currentIntent.slots.number,
    response = "is not valid. Try 'One' or 'Two'." ;

if(numberType === "one" ) {
    response = "Call: 111 111 1111 "
}
else if(numberType === "two") {
    response = "Call: 222 222 2222"
}
callback(null, {
    "dialogAction": {
        "type": "Close",
        "fulfillmentState": "Fulfilled",
        "message": {
            "contentType": "PlainText",
            "content": "Your option: " + event.currentIntent.slots.number + ": " + response
        }
    }
});
};

Solution

  • I realized didn't need to write a Lambda function to connect Lex with Twilio. All I had to do was go to 'Channels' under my LexBot Console and integrate manually my bot with my Twilio account.