DialogFlow Agent does not return result of nested axios

  axios, dialogflow-es, javascript, node.js

I am trying to integrate an external API with my dialogflow agent.
I have the pay as you use plan. I am trying to do nested API calls(sequentially) and then creating a dynamic sentence which I want my agent to speak.

The problem is that I can see the console.log() in the Logs Explorer but the agent does NOT speak the sentence out.

I am using separate functions for axios calls.

Here’s my code:

function get_portfolio_status(agent) {

    agent.add("Here are your details. "); // the agent speaks only this

    var username = "uname";
    var password = "pwd";
    var product = "product";
    var to_speak = "";

// First call
    return login(username, password, product)
      .then((result) => {
        let SID = result.data.sid;

// Second nested function call using result of first one

        get_token(SID)
        .then((result) => {
            let JWT = result.data;
            console.log('Request token received:');

//Third call again using result of previous one

            get_portfolios(JWT)
            .then((result) => {
                let portfolios = result.data.data;
          
// Fourth call

                get_change([], JWT).then((result) => {
                  let changed_portfolio = result.data.data;
                  let to_speak = "Your portfolios are: " + changed_portfolio + portfolios;

                 console.log(to_speak); // I can see this in the Logs.

 // I cant seem to make this below work. Its just skipped due to js behavior I think

                 agent.add(to_speak + 'nWould you like to get your today's winners? '); 

                }).catch((err) => {console.error(err);}); 
            }).catch((err) => {console.error(err);}); 
          }).catch((err) => {console.error(err);});     
    }).catch((err) => {console.error(err);});
  }


The line which should work is
agent.add(to_speak + 'nWould you like to get your today's winners? ');

The agent does not speak out that line. I am quite sure this is the asynchronous behavior of JS but I am handling the promises and returning that.

The login function and others are simply returning axios. Some of them are POST and some are GET

For example:

function login(username, password, product){

  var data = {...};

  config = {
   method: 'post',
   url:'my_url',
   headers:{...}
  }

  return axios(config);
}

Source: Ask Javascript Questions

LEAVE A COMMENT