我和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
}
}
});
};
答案 0 :(得分:0)
步骤功能将是你的朋友。请看下面的链接,
https://aws.amazon.com/step-functions/
https://cloudacademy.com/blog/aws-step-functions-a-serverless-orchestrator/
答案 1 :(得分:0)
我意识到没有必要写一个Lambda函数来连接Lex和Twilio。我所要做的就是前往频道'在我的LexBot控制台下,手动将我的机器人与我的Twilio帐户集成。