Search code examples
node.jsfirebasegoogle-cloud-platformxmlhttprequestactions-on-google

Google Action connected to Firebase function unable to call external api?


Currently I have a google action built using the ActionSDK with NodeJS where the fulfillment is hosted on Firebase cloud functions. All I am trying to do right now is just pass the input from google action to an external api but it seems like it is unable to send it through?

'use strict';

const {actionssdk} = require('actions-on-google');
const functions = require('firebase-functions');
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

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


app.intent('actions.intent.MAIN', (conv) => {
  conv.ask('Hello, this is Patrick.');
});

app.intent('actions.intent.TEXT', (conv, input) => {

  var xhr = new XMLHttpRequest();
  xhr.open("GET", "https://www.googleapis.com/customsearch/v1?key={apikey}&cx={searchID}&q=" + input, true);
  xhr.send();

  xhr.onreadystatechange = function () {
    conv.ask(this.readyState.toString());
  }


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

But this always gives me the response of:

My test app isn't responding right now. Try again soon.

And in the error tab it displays:

{ "error": "No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?" }

I have no idea why this happens as I'm new to this whole thing. Any help is appreciated!


Solution

  • If you are using a free tier of firebase you will not be allowed to use external APIs. You will need to enable billing and upgrade to a paid tier to access.

    UPDATE You may need to use Promise to send back a response. Check out this issue.