0

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.

Sairaj Sawant
  • 1,842
  • 1
  • 12
  • 16
Tom Smith
  • 1
  • 1
  • 2

2 Answers2

0

There are couple of ways to achieve this.

  1. So here's sample how you can declare global variable in Node JS.
  2. If you are not using Inline Editor of Dialogflow then better to use Redis database, it's really quick. You can manage it session by session for your request. When followTwo intent webhook triggers you can get data from Redis and store in context and empty the Redis for other queries. Coz Redis is in-memory database and there are memory restrictions.
  3. Use any other Database to store the request data.

Try not to use Global variable. Use any Database instead.

  • I have used Redis for saving the response but I have not received a response. I have inserted the following code in getAPIData function. I placed console.log("Fun 0") after redisClient.set(sessionKey, dfRes); sessionKey = 'rsessionid'; redisClient.set(sessionKey, dfRes); The following code is in the followTwo function but the response is not displayed in the simulator of DialogFlow. I have inserted console.log("Fun 2") after redisClient.get. sessionKey = 'rsessionid';redisClient.get(sessionKey, function (er, reply) {agent.add(reply); }); In Logging, I have received "Fun 2" then "Fun 0". – Tom Smith Jul 08 '19 at 07:46
0

You can use output context to pass variable intent to followup intent,

 agent.context.set({ "name": 'test-followup', "lifespan": 5, "parameters": {"name": value}});

this output context pass as input context to followup intent

CHanaka
  • 472
  • 5
  • 10