1

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

1 Answers1

0

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.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • How Do i store my three parameter in database for guests it is storing what will be code for storing time and sate with guests?@Prisoner – jigar desai Jan 19 '19 at 13:49
  • I'm not sure I understand your question. If this is a problem with your code, please update the original question and explain what is happening. – Prisoner Jan 19 '19 at 15:08
  • It looks like you've asked the new question at https://stackoverflow.com/q/54267643/1405634. If this answer has helped, accepting and/or upvoting it is appreciated. – Prisoner Jan 19 '19 at 15:10