首页 > 解决方案 > Dialogflow 和 firebase 未连接

问题描述

嗨,我创建了一个对话流代理,它应该从用户那里捕获信息并将其存储在实时 firebase 数据库中。这是行内编辑器代码

        const functions = require('firebase-functions');
        const {WebhookClient} = require('dialogflow-fulfillment');
        const {Card, Suggestion} = require('dialogflow-fulfillment');

        // initialise DB connection
        const admin = require('firebase-admin');
        admin.initializeApp({
          credential: admin.credential.applicationDefault(),
          databaseURL: 'ws:database URL',
        });



        process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

        exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
          const agent = new WebhookClient({ request, response });
          console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
          console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

          function welcome(agent) {
            agent.add(`Welcome to my agent!`);
          }

    function handleAge(agent) {
       const age = agent.parameters.person;
         return admin.database().ref('ageInfo').once("value").then((snapshot) => {
         var averageAge = snapshot.child("runningAverage").val();
          agent.add(`Our recorded average age is ` + averageAge);
        });


      }

      let intentMap = new Map();
      intentMap.set('Default Welcome Intent', welcome);
      intentMap.set('Default Fallback Intent', fallback);
      intentMap.set('1_1_name', handleAge);
      // intentMap.set('your intent name here', yourFunctionHandler);
      // intentMap.set('your intent name here', googleAssistantHandler);
      agent.handleRequest(intentMap);
    });

我已启用数据库设置和读写权限。但是,webhook 仍然失败,唯一的错误是时间已过。我知道管理员凭据部分可能是错误的,但我不确定如何更正它。请任何帮助将不胜感激。

标签: firebasedialogflow-es

解决方案


您正在使用错误的凭据初始化与数据库的连接。试试这样:

admin.initializeApp();

它似乎admin.credential.applicationDefault()不适合你。您可以查看initializeApp函数文档以了解您可以传递哪些参数,而不是将它们默认设置为空admin.initializeApp();或使用。admin.credential.applicationDefault()

这是我使用的package.json :

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "8.13.0 "
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "2.0.0-alpha.4",
    "firebase-admin": "^5.12.1",
    "firebase-functions": "^1.0.3",
    "dialogflow": "^0.1.0",
    "dialogflow-fulfillment": "0.3.0-beta.3"
  }
}

文档

您还可以观看这些其他文档以获取有关 Node.JS firebase-SDK 的更多信息


推荐阅读