Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 如何在(index.js)webhook for GoogleAssistant中调用RESTAPI_Firebase_Webhooks_Actions On Google_Dialogflow Es - Fatal编程技术网

Firebase 如何在(index.js)webhook for GoogleAssistant中调用RESTAPI

Firebase 如何在(index.js)webhook for GoogleAssistant中调用RESTAPI,firebase,webhooks,actions-on-google,dialogflow-es,Firebase,Webhooks,Actions On Google,Dialogflow Es,我正在使用对话流为谷歌助手制作应用程序。我的应用程序使用webhook(功能部署在firebase上)。 主要的一点是——我想调用一个返回JSON的RESTAPI URL(来自index.js),然后解析JSON响应并提取一些值。然后对该值执行一些操作,并将该值发送到GoogleAssistant 代码如下: 'use strict'; process.env.DEBUG = 'actions-on-google:*'; const App = require('actions-on

我正在使用对话流为谷歌助手制作应用程序。我的应用程序使用webhook(功能部署在firebase上)。 主要的一点是——我想调用一个返回JSON的RESTAPI URL(来自index.js),然后解析JSON响应并提取一些值。然后对该值执行一些操作,并将该值发送到GoogleAssistant

代码如下:

 '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);
});

我能得到信息“好吧,你的价值是”,但不是价值。我认为它没有调用URL。

这里可能有两个问题

首先,你需要使用Firebase的付费版本,才能在Google之外进行URL调用。您可以参加“blaze”计划,该计划确实需要信用卡,但仍有免费使用级别

第二个是,您的代码在从REST调用中获取结果的回调之外调用
app.tell()
。因此,发生的情况是,您正在拨打电话,然后在得到结果之前立即呼叫
app.tell()

要想做你想做的事,你可能更想要这样的东西:

  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);

            }

          });

        });

  }

让我检查一下。我会很快给你回复的。谢谢您还告诉我如何获取用户位置,因为我想将该坐标传递给URL。我无法获得权限弹出窗口,如果答案有帮助,请接受和/或向上投票。我们非常感谢您的回答。如果你有一个新问题,可以作为一个新问题提问。不要试图把它添加到现有的问题上。请帮我解决这个问题