gpt4 book ai didi

javascript - AWS Lambda - 如何处理函数中请求处理的顺序

转载 作者:行者123 更新时间:2023-11-30 20:30:00 24 4
gpt4 key购买 nike

我正在为与 facebook 集成的 AWS Lex 聊天机器人使用 AWS Lambda (Node.js 8.1),想知道如何让机器人先打印出 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,
},
};
}

最佳答案

/*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"]
};

const confirmation = 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);
callback(null, confirmation);
});
});

您可以这样做以确保第二个请求总是在第一个请求之后发生。

如果你有 Node Carbon,你也可以像这样使用 async await:

/*request 1 - Get FB user data*/
const request = require('request-native-promise') //need this for promises (async await are basically promises)
const response1 = await request(options);
const body = JSON.parse(response1.body);

params = {
first_name: body["first_name"]
};

const confirmation = 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?"
}
);

callback(null, confirmation);

/*request 2 - Quick replies*/
const response2 = await request(options_2);

关于javascript - AWS Lambda - 如何处理函数中请求处理的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com