0

To sum up the problem I was facing with Dialogflow integration with Firebase, The user input isn't publishing onto the Firebase/cloud FireStore when it should. I am intending the parameter that I named Locations to be published into the collection I named as Location.

First, I followed a tutorial in which the user enables slot filling for the intent which I have done. The Intent name is Location.

Next, on Firebase I opened up the database which corresponded with my bot project ID and created a collection with the identifier name: Location.

Next I attempted to publish the user input for the parameter (named Locations) using the inline editor

package.json Code:

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "8"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "^2.12.0",
    "firebase-admin": "^9.0.0",
    "firebase-functions": "^3.9.0",
    "dialogflow": "^1.2.0",
    "dialogflow-fulfillment": "^0.6.1"
  }
}

nodejs code:

'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  function getLocationHandler(agent) {
    let location = agent.parameters.location;
    db.collection("Location").add({ Location: location });
  }
  let intentMap = new Map();
  intentMap.set('Location', getLocationHandler);
  agent.handleRequest(intentMap);
});
Nithik
  • 1

1 Answers1

0

I think your database write code is incorrect. Writing to Firestore works like this:

const doc = db.collection('Location').doc()
const data = {
  Location: location,
}
await doc.set(data)

Also, note that you need to await the set call as it is async and returns a promise. For more information, have a look how to add data to Cloud Firestore.

You then also need to make sure your callback getLocationHandler(agent) provided in agent.handleRequest(intentMap); also awaits correctly, meaning:

To be able to use await for the Firestore write in getLocationHandler(agent), getLocationHandler needs to be async. Then you need to make sure that your cloud function functions.https.onRequest(...) does not finish before the getLocationHandler completed correctly.

Depending on the signature of agent.handleRequest (which I unfortunately do not know) you can await this call or if it only accepts a void callback, you would have to create a promise before, complete it at the end of getLocationHandler and await this promise at the end of the onRequest. See this question for details about this.

sceee
  • 1,681
  • 19
  • 34