如何使用Dialogflow内联编辑器查询firestore以获取信息

时间:2018-07-11 11:24:27

标签: google-cloud-firestore dialogflow

我正在Dialogflow中使用内联编辑器,目的是查询我在Firestore中创建的数据库。 简而言之,用户请求一个课程列表,我希望聊天机器人随后从数据库中获取该信息并将其显示给用户。 下面,我尝试创建一个函数来执行此操作,我想接收用户输入,说“艺术课程”,并让我的数据库返回这些结果。

到目前为止,我已经创建了一个在意图匹配时触发的函数,就像这样;

 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();
    } 
}

执行我希望实现的功能时,我需要添加什么值得注意的东西吗? 谢谢。

更新

此代码可以很好地部署,但是当我与我的机器人通信并触发CourseEnquiry意图时,cloud Functions会显示此错误:

admin.collection is not a function

虽然这似乎是自我解释,但我无法确定其含义,但我认为声明const admin = require('firebase-admin');使我可以使用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

大家好,我仍然尝试添加:

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

根据this文档,但部署时出现此错误:

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.

1 个答案:

答案 0 :(得分:1)

您不会显示您如何对结果做出回应,但是您需要确保在Promise中将其作为then()子句的一部分进行处理。由于firestore集合中的get()返回了Promise,并且您是从函数中返回的,因此您需要确保调用函数将其视为Promise,具有then()子句并发送将结果作为此子句中某些内容的一部分返回。