Google cloud firestore 如何使用Dialogflow内联编辑器查询firestore以获取信息
我在Dialogflow中使用内联编辑器,目的是对我在Firestore中创建的数据库进行查询。 简言之,用户请求一个课程列表,我希望聊天机器人从数据库中获取该信息并将其显示给用户。 下面我尝试创建一个函数来实现这一点,我想接受用户输入,说“艺术课程”,让我的数据库返回这些结果 到目前为止,我已经创建了一个在意图匹配时触发的函数,就像这样Google cloud firestore 如何使用Dialogflow内联编辑器查询firestore以获取信息,google-cloud-firestore,dialogflow-es,Google Cloud Firestore,Dialogflow Es,我在Dialogflow中使用内联编辑器,目的是对我在Firestore中创建的数据库进行查询。 简言之,用户请求一个课程列表,我希望聊天机器人从数据库中获取该信息并将其显示给用户。 下面我尝试创建一个函数来实现这一点,我想接受用户输入,说“艺术课程”,让我的数据库返回这些结果 到目前为止,我已经创建了一个在意图匹配时触发的函数,就像这样 function getCourses(agent){ let courseRequest = agent.parameters.courseReq
function getCourses(agent){
let courseRequest = agent.parameters.courseRequest;
if (getCourses){
console.log('Here is the list you requested for ${getCourses}' + parameters.courseRequest);
return admin.firestore().collection('Course_Information').doc.where('CoureTypes').get();
}
}
为了实现我的目标,我是否需要在我的职能中添加一些值得注意的东西?
多谢各位
更新
此代码部署良好,但当我与我的bot通信并触发CourseEnquiry意图时,云函数显示此错误:
admin.collection is not a function
虽然这似乎不言自明,但我无法确定它的含义,我认为声明const admin=require('firebase-admin')代码>允许我使用管理集合
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
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 getDate(agent){
var today = new Date();
}
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function test(agent){
agent.add("The test is successful");
}
function getCourses(agent){
// Get the database collection and document
const getCourseDoc = admin.collection('Course_Information').doc('Course_Types');
return getCourseDoc.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add(doc.data().entry);
}
return Promise.resolve('Here is the information you wanted');
}).catch(() => {
agent.add('Error reading entry from the Firestore database.');
});
}
function getSubmissionDateSep(agent){
agent.add('Your next submission date is for coursework 1 is');
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Test_Test', test);
intentMap.set('CourseEnquiry', getCourses);
intentMap.set('Submission_Dates - sept', getSubmissionDateSep);
agent.handleRequest(intentMap);
});
更新#2
嘿,伙计们,我还是没有取得任何进展,我尝试添加:
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
根据文档,但我在部署时遇到以下错误:
The deployment of your Cloud Function failed:
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: Error: Firebase config variables are not available. Please use the latest version of the Firebase CLI to deploy this function.
您不会用结果向用户显示您是如何响应的,但您需要确保将其作为承诺中then()
子句的一部分来处理。由于firestore集合中的get()
返回一个承诺,并且您正在从函数中返回它,因此您需要确保调用函数将其视为一个承诺,具有then()
子句,并将结果作为此子句中某个内容的一部分发回。谢谢您的帮助,我对此(显然是新手!)所以很难找到任何提出和返回查询的指南!我将尝试实现你的意思,并用新的实现进行更新。谢谢。嘿@囚犯,在接受了你的建议后,我已经更新了问题,我现在在哪里,我在这里走对了吗?谢谢您可以更新问题吗?请澄清“不想部署”的含义。你把这个放在代码里的什么地方了?当您尝试部署时,您到底遇到了哪些错误?嘿,感谢@capture的回复,我已经更新了问题并提供了更多解释。package.json文件中“firebase admin”和“firebase functions”的条目是什么?如果您阅读了邮件的其余部分,它表明基于版本更改而使用admin.initializeApp()
或functions.config()
的方式出现了问题。@Capital,这就是我所拥有的:“firebase admin”:“^4.2.1”,“firebase函数”:“^0.5.7”
,不太确定如何检查我应该有哪个版本,如果有帮助的话,我确实安装了npm。