Search code examples
firebasefirebase-realtime-databasedialogflow-esgoogle-homedialogflow-es-fulfillment

How To Store DialogFlow all the Parameter Value On Realtime Firebase Database?


I am making a action for Google Home. It's about restaurant booking so I have to take some information from users like:

  1. How many guests do you want to book the table for?
  2. For which date?
  3. For which time?

Then I have to store it on Firebase real time database. For the guests parameter it is writing the value to the database but the date and time parameters are not showing on database.

what will be code for storing both time and date with guests in database?

'use strict';
 
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
 
 //initialize db connection
 const admin = require('firebase-admin');
 admin.initializeApp();
 
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 welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }
 
  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}
  function createBooking(agent) {
      
    let guests = agent.parameters.guests;
    let time = new Date(agent.parameters.time);
    let date = new Date(agent.parameters.date);
    let bookingDate = new Date(date);
    bookingDate.setHours(time.getHours());
    bookingDate.setMinutes(time.getMinutes());
    let now = new Date();
      const guestsParam = agent.parameters.guests;
     
      if (guests < 1){
        agent.add('You need to reserve a table for at least one person. Please try again!');
    } else if (bookingDate < now){
        agent.add(`You can't make a reservation in the past. Please try again!`);
    } else if (bookingDate.getFullYear() > now.getFullYear()) {
        agent.add(`You can't make a reservation for ${bookingDate.getFullYear()} yet. Please choose a date in ${now.getFullYear()}.`);
    } else {
        let timezone = parseInt(agent.parameters.time.toString().slice(19,22));
        bookingDate.setHours(bookingDate.getHours() + timezone);
        agent.add(`You have successfully booked a table for ${guests} guests on ${bookingDate.toString().slice(0,21)}`);
        agent.add('See you at the restaurant!');
    }
     return admin.database().ref('/guests').push({guests: guests}).then((snapshot) => {
    console.log('database write successful: ' + snapshot.ref.toString());
  });
  
        
 }
    
 
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('restaurant.booking.create', createBooking);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});


Solution

  • Well, the simple answer is that you don't see them in the database because you don't put them there.

    The line

    admin.database().ref('/guests').push({guests: guests})
    

    Just stores a document with the guests. You don't do anything with bookingDate.