I am using AWS Lambda (Node.js 8.1) for my AWS Lex chatbot which is integrated with facebook and would like to know how to make the bot print out the confirmIntent response (request 1) first and then print the quick replies (request 2).
I have tried a variety of things like adding "request 2" into the same callback as the confirmIntent function, but the quick replies always prints first. I know the issue is because NodeJS is asychronous, but still dont know how to solve it.
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);
});
}
My confirmIntent function
function confirmIntent(sessionAttributes, intentName, slots, message) {
return {
sessionAttributes,
dialogAction: {
type: 'ConfirmIntent',
intentName,
slots,
message,
},
};
}
confirmIntent
function - where are you getting this from? – Faizuddin Mohammedcallback
functions have error first style - which means you should probably be doing something likecallback(null, confirmIntent())
– Faizuddin MohammedconfirmIntent()
an async function? – Faizuddin Mohammed