首页 > 解决方案 > 将 Dialogflow 连接到 Firebase 问题

问题描述

我一直在阅读,但找不到答案

我尝试了我的 Firebase,它没有存储任何数据。这是相关的内联编辑器

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

function angerEmotionCapture(agent) {

    let angryTo = agent.parameters.angryDirectedTo;
    agent.add(`love your ${angryTo},dude`);
    return db.collection('directedTo').add({directedTo: angryTo});
}

这是我的 Firebase 数据库

我的 Firebase 数据库

任何帮助将不胜感激,谢谢!

标签: firebasegoogle-cloud-firestoregoogle-cloud-functionsdialogflow-esdialogflow-es-fulfillment

解决方案


请查看以下示例代码,展示如何将 Firebase 的 Firestore 数据库连接到 Firebase 函数上的 Dialogflow 实现托管:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });

  function writeToDb (agent) {
    // Get parameter from Dialogflow with the string to add to the database
    const databaseEntry = agent.parameters.databaseEntry;

    // Get the database collection 'dialogflow' and document 'agent' and store
    // the document  {entry: "<value of database entry>"} in the 'agent' document
    const dialogflowAgentRef = db.collection('dialogflow').doc('agent');
    return db.runTransaction(t => {
      t.set(dialogflowAgentRef, {entry: databaseEntry});
      return Promise.resolve('Write complete');
    }).then(doc => {
      agent.add(`Wrote "${databaseEntry}" to the Firestore database.`);
    }).catch(err => {
      console.log(`Error writing to Firestore: ${err}`);
      agent.add(`Failed to write "${databaseEntry}" to the Firestore database.`);
    });
  }
  
  // Map from Dialogflow intent names to functions to be run when the intent is matched
  let intentMap = new Map();
  intentMap.set('WriteToFirestore', writeToDb);
  agent.handleRequest(intentMap);
});

查看Dialogflow 的 Firestore GitHub 示例


推荐阅读