2

I am making app for google assistant using dialog flow. My app uses the webhook (function is deployed on firebase). The Main point is -- I want to call a REST API URL(from index.js) that returns JSON, then parse the JSON Response and extract some value. Then do some operation on that value and send the value to google assistant.

The code is below:

 'use strict';

  process.env.DEBUG = 'actions-on-google:*';
  const App = require('actions-on-google').DialogflowApp;

  const functions = require('firebase-functions');


  // a. the action name from the make_name Dialogflow intent
  const SOME_ACTION = 'some_action';

  //----global variables-----
  const http = require('https');
  var body = "";
  var value="";

  exports.addressMaker = functions.https.onRequest((request, response) => {
  const app = new App({request, response});
  console.log('Request headers: ' + JSON.stringify(request.headers));
  console.log('Request body: ' + JSON.stringify(request.body));


  function makeMessage (app) {

    var req = http.get("https://some_url_of_API", function(res)
         {
          res.writeHead(200, {"Content-Type": "application/json"});
          res.on("data", function(chunk){ body += chunk; });

          res.on('end', function()
           {
            if (res.statusCode === 200) {
                try {
                    var data = JSON.parse(body);

                   value=data.word; //----getting the value----

                } catch (e) {
                    console.log('Status:', res.statusCode);
                    console.log('Error parsing JSON!');
                }
            } else {
                console.log('Status:', res.statusCode);

            }

             });

        });



    app.tell('Alright, your value is '+value);
  }

  let actionMap = new Map();
  actionMap.set(SOME_ACTION, makeMessage);


app.handleRequest(actionMap);
});

I am able get the message "Alright, your value is", but not the value. I think it is not calling the URL.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
akash verma
  • 159
  • 1
  • 15

1 Answers1

1

You have two possible issues here.

The first is that you need to be on one of the paid versions of Firebase to make URL calls outside of Google. You can be on the "blaze" plan, which does require a credit card, but still has a free level of usage.

The second is that your code calls app.tell() outside of the callback that gets the results from your REST call. So what is happening is that you are making the call, and then immediately calling app.tell() before you get the results.

To do what you want, you probably want something more like this:

  function makeMessage (app) {

    var req = http.get("https://some_url_of_API", function(res)
         {
          var body = '';
          res.writeHead(200, {"Content-Type": "application/json"});
          res.on("data", function(chunk){ body += chunk; });

          res.on('end', function()
           {
            if (res.statusCode === 200) {
                try {
                   var data = JSON.parse(body);

                   value=data.word; //----getting the value----

                   // Send the value to the user
                   app.tell('Alright, your value is '+value);

                } catch (e) {
                    console.log('Status:', res.statusCode);
                    console.log('Error parsing JSON!');
                }
            } else {
                console.log('Status:', res.statusCode);

            }

          });

        });

  }
Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Let me check this. I will get back to you soon . Thanks – akash verma Mar 30 '18 at 13:12
  • 1
    Can you also tell me how to take user location, because I want to pass that coordinates to URL – akash verma Apr 01 '18 at 06:35
  • I am not able to get permission popup, https://developers.google.com/actions/assistant/helpers – akash verma Apr 01 '18 at 06:42
  • If the answer helped, accepting and/or upvoting the answer is always appreciated. If you have a **new** question, feel free to ask it as a new question. don't try to add it onto an existing question. – Prisoner Apr 01 '18 at 10:49
  • Please help me out for this one – akash verma Apr 01 '18 at 11:14
  • We're happy to help - but you changed the question pretty dramatically (from making a REST call to getting location permission). Please ask this as a new question (just create the question and cut/paste what you're written) so we can keep the previous question/answer to help others out who may face similar issues. – Prisoner Apr 01 '18 at 11:21
  • I've reverted the question back to the one that matches the answer. You've posted the new question about permissions at https://stackoverflow.com/questions/49598149/how-to-get-user-confirmation-for-location-and-the-geo-coordinates-in-dialogflow – Prisoner Apr 02 '18 at 10:42