如何使用Dialogflow内联编辑器查询firestore以获取信息 [英] How to query firestore with the Dialogflow inline editor to get information
问题描述
我正在Dialogflow中使用内联编辑器,目的是查询我在Firestore中创建的数据库。
简而言之,用户请求一个课程列表,我希望聊天机器人然后从数据库中获取该信息并将其显示给用户。
下面,我尝试创建一个函数来执行此操作,我想接受用户输入,说艺术课程,并让我的数据库返回这些结果。
I am using the inline editor within Dialogflow with the aim of making queries to the database I have created within Firestore. In short, the user requests a list of courses, I'd like the chatbot to then grab that information form the db and display that back to the user. Below I have tried to create a function that will do this, I want to take the user input, say "Art Courses" and have my db return those results.
到目前为止,我已经创建了一个在意图匹配时触发的函数,就像这样;
So far, I have created a function that is triggered when the intent is matched, like so;
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();
}
}
我需要添加一些值得注意的东西吗?功能执行我希望达到的目标?
谢谢。
Are there any notable things I need to add to my function to perform what I wish to achieve? Thank you.
更新
此代码可以很好地部署,但是当我与我的机器人通信并触发CourseEnquiry意图时,cloud Functions会显示此错误:
This code deploys fine, but when I communicate with my bot and trigger the CourseEnquiry intent, cloud Functions shows this error:
admin.collection is not a function
虽然这似乎是自我解释,但我无法确定其含义,但我想声明 const admin = require('firebase-admin');
使我可以使用 admin.collection
Whilst this seems self explanatory I can't make sure of what it means, I thought declaring const admin = require('firebase-admin');
enables me to use admin.collection
// 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
伙计们,还是没办法,我尝试添加:
Hey guys, still not got anywhere with this, I have tried adding:
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
根据此文档,但部署时出现此错误:
According to this document but I get this error when deploying:
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.
推荐答案
您没有显示自己的响应方式向用户显示您的结果,但您需要确保将其作为Promise中 then()
子句的一部分进行处理。由于firestore集合中的 get()
返回了Promise,并且您是从函数中返回它,因此需要确保调用函数将其视为Promise,有一个 then()
子句,并将结果作为该子句中某些内容的一部分发回。
You don't show how you're responding to the user with your results, but you'll want to make sure you handle that as part of the then()
clause in a Promise. Since the get()
in the firestore collection returns a Promise, and you are returning it from your function, you need to make sure that the calling function treats it as a Promise, has a then()
clause, and sends back the result as part of something inside this clause.
这篇关于如何使用Dialogflow内联编辑器查询firestore以获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!