【发布时间】:2018-05-22 04:48:22
【问题描述】:
我将 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,
},
};
}
【问题讨论】:
-
这取决于你的
confirmIntent函数 - 你从哪里得到这个? -
并且作为一般规则,
callback函数具有错误优先样式 - 这意味着您可能应该执行类似callback(null, confirmIntent())的操作 -
另外,
confirmIntent()是异步函数吗? -
我在主要问题中添加了confirmIntent函数,它不是异步函数。
标签: javascript node.js aws-lambda facebook-chatbot aws-lex