google-cloud-firestore - How to query firestore with the Dialogflow inline editor to get information
问题描述
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.
UPDATE
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
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);
});
UPDATE #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.
解决方案
你没有展示你是如何用你的结果来回应用户的,但你要确保你把它作为then()
Promise 子句的一部分来处理。由于get()
在 firestore 集合中返回一个 Promise,并且您从函数中返回它,因此您需要确保调用函数将其视为 Promise,有一个then()
子句,并将结果作为该子句内某些内容的一部分发回.
推荐阅读
- nearprotocol - ERC20 代币的 NEAR 协议版本是什么?
- c# - 如何以随机顺序在 2 个列表框之间划分数据表
- c - 定义结构的问题(圆圈)
- css - 将复选框转换为具有正确版本的 Font Awesome 复选框
- python-3.x - 如何从 ftp 获取文件并存储在 odoo 数据库中
- sql - 如何在 SQL Server 中根据日期计算行数?
- amazon-web-services - 如何列出 AWS RedShift 中的所有存储过程
- django - 具有多个查询参数的动态查询集过滤
- java - Web3j 读取单个事务的所有发出的事件
- python - 转换没有循环的numpy数组