Shufu
Shufu

Reputation: 33

How to output the information from REST API

I want to have my agent say information which is got from a REST API. But the code as followes could not respond any messages, which means "queryResult.fulfillmentMessages.text" is empty.

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
 
  function intent1(agent) {
    callApi().then((output) => {
        //This log is correctly outputted to firebase console 
        console.log('output: ' + output);
        
        //But this method doesn't work and the Agent says nothing
        agent.add('output: ' + output);
    });
  }

  function callApi(){
    return new Promise((resolve, reject) => {
        let req = http.get('http://xxx', (res) => {
          let chunk = '';
          res.on('data', (d) => {
            chunk = d;
          });
          res.on('end', () => {
            let response = JSON.parse(chunk);
            let output = response['results'][0];
            
            resolve(output);
          });
        });
    });
  }

  let intentMap = new Map();
  intentMap.set('intent1', intent1);
  agent.handleRequest(intentMap);
});

I tried another code as follows which indicates that callback functions don't affect to "agent.add" method. So, I think the issue is caused by the API requesting process or something...

'use strict';
 
const functions = require('firebase-functions');
const App = require('actions-on-google').DialogflowApp;
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

const http = require('http');
 
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
 
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
 
  function intent1(agent) {
    callApi().then((output) => {
        //This method works and the Agent says "output: abc"
        agent.add('output:' + output);
    });
  }

  function callApi(){
    return new Promise((resolve, reject) => {
        let output = "abc";
        resolve(output);
    });
  }

  let intentMap = new Map();
  intentMap.set('intent1', intent1);
  agent.handleRequest(intentMap);
});

Does anyone know the way to resolve the issue or another way to output information from REST API?

Upvotes: 3

Views: 473

Answers (1)

Jesus Duran
Jesus Duran

Reputation: 333

Your intent1 function has to return a Promise as well, and resolve it after you have added your response to the agent.

function intent1(agent) {
    return new Promise((resolve, reject) => {
        callApi().then((output) => {
            //This method works and the Agent says "output: abc"
            agent.add('output:' + output);
            resolve();
        });
    }); 
}

Also, in the callApi function the chunk variable is being assigned a new value each time some data is received. You should add the received data to the current value of your variable (just add a '+' before the equal):

res.on('data', (d) => {
    chunk += d;
});

Upvotes: 2

Related Questions