firebase - 将 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 的 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);
});
推荐阅读
- powershell - Powershell 5.1 捕捉注册错误
- docker - 使用 Docker 或 PM2 重新捆绑更改
- angular - 在没有 Angular CLI 的情况下使用环境变量
- c# - 调试 msft 团队机器人频道对话
- sql - PostgreSQL 中是否有与 generate_array 类似的函数?
- android - 首次通过 Play 商店安装时应用程序崩溃
- asp.net-web-api2 - 如何使用 Owin 和 WebApi 2 在 SignalR 上配置 MessagePack?
- java - 文本字段的按钮动作侦听器不起作用
- node.js - 节点 uncaughtException 处理程序中的异步代码
- google-apps-script - 如何保护 1 列不被编辑?