我正在将 AWS Lambda (Node.js 8.1) 用于与 facebook 集成的 AWS Lex 聊天机器人,并且想知道如何让机器人首先打印出 confirmIntent 响应(请求 1),然后打印快速回复(请求 2 )。
我尝试了各种方法,例如将“请求 2”添加到与 confirmIntent 函数相同的回调中,但快速回复总是首先打印。我知道这个问题是因为 NodeJS 是异步的,但仍然不知道如何解决它。
function greeting(intentRequest, callback) {
const hello = intentRequest.currentIntent.slots.Hello;
const sessionAttributes = intentRequest.sessionAttributes || {};
const confirmationStatus = intentRequest.currentIntent.confirmationStatus;
var params = null;
var options = {
uri: 'https://graph.facebook.com/v2.6/'+PSID+'?fields=first_name,last_name,gender&access_token='+process.env.FB_access_token,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
var options_2 = {
uri: 'https://graph.facebook.com/v2.6/me/messages?access_token='+process.env.FB_access_token,
body: JSON.stringify({
recipient: {"id":PSID},
message: {
text: "Here is a quick reply!",
quick_replies: [
{
content_type: "text",
title: "Search",
payload: "yes",
image_url: "http://example.com/img/red.png"
},
{
content_type: "location"
}
]
}
}),
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
/*request 1 - Get FB user data*/
request(options, function (error, response, body) {
console.log(error,response.body);
body = JSON.parse(body);
params = {
first_name: body['first_name'],
};
callback(confirmIntent(sessionAttributes, 'GetStarted',
{
Age: null,
},
{ contentType: 'PlainText', content: 'Hi ' +params.first_name+ ', I will guide you through a series of questions to select your own customized product. Are you ready to start?' }));
});
/*request 2 - Quick replies*/
request(options_2, function (error, response) {
console.log(error,response.body);
});
}
我的 confirmIntent 功能
function confirmIntent(sessionAttributes, intentName, slots, message) {
return {
sessionAttributes,
dialogAction: {
type: 'ConfirmIntent',
intentName,
slots,
message,
},
};
}