代码之家  ›  专栏  ›  技术社区  ›  akash verma

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

  •  2
  • akash verma  · 技术社区  · 7 年前

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

    代码如下:

     '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。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Prisoner    7 年前

    这里有两个可能的问题。

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

    第二,您的代码调用 app.tell() 在从REST调用获取结果的回调之外。所以现在发生的是,你正在打电话,然后立即打电话 应用程序。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);
    
                }
    
              });
    
            });
    
      }