under
under

Reputation: 3067

How do I call a third party Rest API from Firebase function for Actions on Google

I am trying to call a rest API from Firebase function which servers as a fulfillment for Actions on Google.

I tried the following approach:

const { dialogflow } = require('actions-on-google');
const functions = require('firebase-functions');
const http  = require('https');
const host = 'wwws.example.com';

const app = dialogflow({debug: true});

app.intent('my_intent_1', (conv, {param1}) => {

         // Call the rate API
        callApi(param1).then((output) => {
            console.log(output);
            conv.close(`I found ${output.length} items!`); 
        }).catch(() => {
            conv.close('Error occurred while trying to get vehicles. Please try again later.'); 
        });

});

function callApi (param1) {
    return new Promise((resolve, reject) => {
        // Create the path for the HTTP request to get the vehicle
        let path = '/api/' + encodeURIComponent(param1);
        console.log('API Request: ' + host + path);


        // Make the HTTP request to get the vehicle
        http.get({host: host, path: path}, (res) => {
            let body = ''; // var to store the response chunks
            res.on('data', (d) => { body += d; }); // store each response chunk
            res.on('end', () => {
                // After all the data has been received parse the JSON for desired data
                let response = JSON.parse(body);
                let output = {};

                //copy required response attributes to output here

                console.log(response.length.toString());
                resolve(output);
            });
            res.on('error', (error) => {
                console.log(`Error calling the API: ${error}`)
                reject();
            });
        }); //http.get
    });     //promise
}

exports.myFunction = functions.https.onRequest(app);

This is almost working. API is called and I get the data back. The problem is that without async/await, the function does not wait for the "callApi" to complete, and I get an error from Actions on Google that there was no response. After the error, I can see the console.log outputs in the Firebase log, so everything is working, it is just out of sync.

I tried using async/await but got an error which I think is because Firebase uses old version of node.js which does not support async.

How can I get around this?

Upvotes: 2

Views: 3774

Answers (1)

Nick Felker
Nick Felker

Reputation: 11970

Your function callApi returns a promise, but you don't return a promise in your intent handler. You should make sure you add the return so that the handler knows to wait for the response.

app.intent('my_intent_1', (conv, {param1}) => {
     // Call the rate API
    return callApi(param1).then((output) => {
        console.log(output);
        conv.close(`I found ${output.length} items!`); 
    }).catch(() => {
        conv.close('Error occurred while trying to get vehicles. Please try again later.'); 
    });
});

Upvotes: 3

Related Questions