首页 > 解决方案 > 从firebase函数调用json文件

问题描述

我在每个组中有 3 个组( html )是四个人,这些人在所有 3 个组中都是相同的,但只是不同的数字(投票/学分等等),我有一个 json 文件,其中包含组内这些人的值。我的 HTML 文件可以毫无问题地读取我的 json 文件。

我正在使用 Dialogflows 内联编辑器与 Google 助理一起工作。我想要的是与我的 html( javascript ) 从 json 文件中读取这些值的方式相同,我也希望能够加载该person.json文件。我已经编辑了很多次无法调用 person.json url。

例如:

“嘿谷歌,告诉我亚历克斯学分”
<这里应该从我的 json 文件中读取,它是 73>

以下是代码:person1.html

    var response = await fetch("laurel.json");
    var arr = await response.json();
    var laurel= arr[1];

            var dflt = {
              min: 0,
              max: 100,
           //   donut: true,
              gaugeWidthScale: 1.1,
              counter: true,
              hideInnerShadow: true
            }

            var ee1 = new r({
              id: 'ee1',
              value: laurel['Jennifer'],
              title: 'Jennifer ',
              defaults: dflt
            });

            var ee2 = new r({
              id: 'ee2',
              value: laurel['Peter'],
              title: 'Peter',
              defaults: dflt
            });
        	
        	    var ee3 = new r({
              id: 'ee3',
              value: laurel['Justin'],
              title: 'Justin',
              defaults: dflt
            });
        	
        	    var ee4 = new r({
              id: 'ee4',
              value: laurel['Alex'],
              title: 'Alex',
              defaults: dflt
            });

          });

内联编辑器 index.js :

 

 
  intentMap.set('persons1', someFunction);
function someFunction(agent) {
    agent.add(`Alex credits are 73 `);
}
  // // below to get this function to be run when a Dialogflow intent is matched
  // function yourFunctionHandler(agent) {
  //   agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
  //   agent.add(new Card({
  //       title: `Title: this is a card title`,
  //       imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
  //       text: `This is the body text of a card.  You can even use line\n  breaks and emoji! `,
  //       buttonText: 'This is a button',
  //       buttonUrl: 'https://assistant.google.com/'
  //     })
  //   );
  //   agent.add(new Suggestion(`Quick Reply`));
  //   agent.add(new Suggestion(`Suggestion`));
  //   agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
  // }

  // // Uncomment and edit to make your own Google Assistant intent handler
  // // uncomment `intentMap.set('your intent name here', googleAssistantHandler);`
  // // below to get this function to be run when a Dialogflow intent is matched
  // function googleAssistantHandler(agent) {
  //   let conv = agent.conv(); // Get Actions on Google library conv instance
  //   conv.ask('Hello from the Actions on Google client library!') // Use Actions on Google library
  //   agent.add(conv); // Add Actions on Google library responses to your agent's response
  // }
  // // See https://github.com/dialogflow/dialogflow-fulfillment-nodejs/tree/master/samples/actions-on-google
  // // for a complete Dialogflow fulfillment library Actions on Google client library v2 integration sample

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

标签: javascriptnode.jsjsondialogflow-esactions-on-google

解决方案


你在这里有几个选择。您可以将您的计划切换到 Blaze,并对托管在前面提到的公共 URL 上的 JSON 数据发出网络请求。

或者,您也可以将 JSON 数据放入 Firebase 实时数据库,因为如果您将数据库规则设置为 public readable,它也可以作为 JSON 文档在公共 URL 上访问。

数据库.rules.json

{
  "rules": {
    "persons": {
      ".read": true
    }
  }
}

之后,您可以通过此 URL 在 Firebase 中访问您的 JSON 数据:https://<your-project-id>.firebaseio.com/persons.json

对于在您的网络应用程序中访问,这实际上应该是相同的。

对于您的 AoG 实现,您只需使用 Firebase SDK 来访问数据

firebase.database().ref('persons').child('personId');


推荐阅读