title: Sending dynamic data in response using agent.add()
body: <p>My requirement is to send dynamic data with response using Dialogflow’s  Nodejs fulfillment library beta.</p>
  
  <p>My query is to know the possibility of sending dynamic data with response which we got that from external api ? There are cases where we need to send real time data to dialogflow agent ( in turn to client) by calling a API . I tried below code to implement this but its not working , here " value" is the data that we are getting from api response and the same has been parsed / navigated to extract json value.</p>
  
  <p>agent.add(stock price is + JSON.parse(value).IRXML.StockQuotes.Stock_Quote[0].Trade)</p>
  
  <p>Prior above code execution , i'm making a synchronous external API call using promotion so that the result is replaced in the agent.add section.</p>
  
  <p>At present, i'm not able to execute agent.add function inside promotion then function. Please do assist me to implement this or let me know is there any alternative approaches to do this.</p>
  
  <p>Im making a external api call to fetch data so that i can sent same result back to agent.  I could able to make call but i'm really not sure how to pass this received data to agent. As you can see in the below code , im calling externalCall() to make api call and this function is called from SpineDay(). The result returned from  externalCall()  is replaced in  SpineDay(), still the result is not passed to agent using agent.add(). Any instruction agent.add() with in "externalCall(req,res).then" is not working.</p>
  
  <pre><code>    function SpineDay(agent){
  
  
  
          externalCall(req,res).then((output,agent) =&gt; {
  
                  //res.json({ 'fulfillmentText':  JSON.parse(output).IRXML.StockQuotes.Stock_Quote[0].Trade }); 
                   agent.add(new Card({
                          title: output,
                          imageUrl: 'https://dialogflow.com/images/api_home_laptop.svg',
                          text: `This is the body text of a card.  You can even use line\n  breaks and emoji!`,
                          buttonText: 'Se rendre sur XXX',
                          buttonUrl: 'https://XXX/'
                          })
                      );
                      agent.add(new Suggestion(`Quelles sont les dernière sorties à la demande?`));
                      agent.add(new Suggestion(`Quel est le programme de ce soir?`));
                }).catch(() =&gt; {
                  res.json({ 'fulfillmentText': `Error calling the weather API!` });
                });
  
            /*agent.add("Quelles sont les dernière sorties à la demande?");
              agent.add("`Quel est le programme de ce soir?");*/
    } 
  
  
  
  var  externalCall = function(mainreq, mainres) { 
        return new Promise(function(resolve, reject){
          // Create the path for the HTTP request to get the weather
            var jsonObject = JSON.stringify(mainreq.body);
            const agent = new WebhookClient({request: req, response: res});
            console.log('In side post code' + Buffer.byteLength(jsonObject, 'utf8'))
            // An object of options to indicate where to post to
            var options = {
                port: '443',
                uri: 'https://xxxx.xxx.xxx//ticker/quote/stock?compid=22323&amp;reqtype=quotes',
                method: 'POST',
                headers: {
                    'Content-Length' : Buffer.byteLength(jsonObject, 'utf8'),
                    'X-MDT-API-KEY': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'    
                }
            };
  
          // Make the HTTP request to get the weather
          request(options, function(err, res, body) { 
  
            if (!err &amp;&amp; res.statusCode == '200'){
              // After all the data has been received parse the JSON for desired
              // data
              let response = JSON.parse(body).IRXML.StockQuotes.Stock_Quote[0].Trade;
  
  
              // Create response
              let output = "xxxx stock price is " +response;
  
              // Resolve the promise with the output text
              console.log(body);
              resolve(output,agent);
            }else{
                console.log(`Error calling the weather API: ${error}`)
                  reject("Error calling the weather API");
            }
  
  
          });
        });
      }
  </code></pre>