代码之家  ›  专栏  ›  技术社区  ›  sptramp

如何使用Lex在另一个Lambda函数中调用Lambda函数?

  •  -1
  • sptramp  · 技术社区  · 7 年前

    我正在与AWS Lambda和Twilio一起比赛。我有一个Lambda函数,它集成了Lex和Twilio。我还有另一个Lambda函数,它对我的LexBot进行验证。两者都可以单独工作。然而,我试图将它们放在一起,所以每当我的LexBot与Twilio集成时,它也会在同一个Lambda函数中调用我的验证。

    有什么想法吗?非常感谢。

    以下是将Lex与Twilio集成在一起的Lambda:

    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);
    }
    };
    

    这是我的Lambda和验证:

    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
            }
        }
    });
    };
    
    2 回复  |  直到 7 年前
        2
  •  0
  •   sptramp    7 年前

    我意识到不需要编写Lambda函数来连接Lex和Twilio。我所要做的就是转到LexBot控制台下的“频道”,手动将我的bot与我的Twilio帐户集成。