首页 > 解决方案 > 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.

标签: google-cloud-firestoredialogflow-es

解决方案


你没有展示你是如何用你的结果来回应用户的,但你要确保你把它作为then()Promise 子句的一部分来处理。由于get()在 firestore 集合中返回一个 Promise,并且您从函数中返回它,因此您需要确保调用函数将其视为 Promise,有一个then()子句,并将结果作为该子句内某些内容的一部分发回.


推荐阅读