I am getting an issue while making the API call in google dialogflow. I want to call the API in first intent (getAPIData) and then simultaneously call the second intent (followOne) and then third Intent (followTwo). (Do not want to wait to complete the API call). In third intent I want a result of the API call which we call in first intent and add the API response to dialogflow agent. Please review the code for more detail and let me know the issue. The API taking more than 11 second to response.
const {WebhookClient} = require('dialogflow-fulfillment');
const axios = require('axios');
const {Card, Suggestion, List, BrowseCarousel, BrowseCarouselItem} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug';
exports.GetAPIDataHandle = functions.https.onRequest((request, response) => {
let dialogflowResponse = '';
const agent = new WebhookClient({ request, response });
async function getAPIData(){
axios.get('https://apiurl.com')
.then((resp) => {
dialogflowResponse = resp.data.data;
})
doTimeOut(3.5);
agent.setFollowupEvent('followUpOne');
}
function followOne(agent){
doTimeOut(4);
agent.setFollowupEvent('followUpTwo');
}
function followTwo(agent){
doTimeOut(4);
if(dialogflowResponse != ''){
agent.add('Here is the search result.');
dialogflowResponse.map(result => {
agent.add(result.word);
})
}else{
agent.add(`Please try again later`);
}
}
let intentMap = new Map();
intentMap.set('getFunnyWords', getAPIData);
intentMap.set('followupeventone', followOne);
intentMap.set('followupeventtwo', followTwo);
agent.handleRequest(intentMap);
});
function doTimeOut(numsec){
var dt1 = new Date();
dt1.setSeconds( dt1.getSeconds() + numsec);
do{
}while((new Date()).getSeconds() < dt1.getSeconds());
}
As per the current code, We get the global variable 'dialogflowResponse' is undefined in third intent.