0

I am currently doing a project using DialogFlow and Firebase Database. My main concern, and I have been trying to figure it out for a long time is, how do I take a user input and search through the Firebase database for it? As you can see in the code, I have the if else statement to help me filter it out for now, however as more events are being stored the database,I figured out that the if else statement will not be efficient and viable. So, do advise me on how to best take in a user's input and search the database per their request. For example, they might ask "What events are there on 15th August?", the function should then search the database for 15th August, and inform them of any events on that day. Hope to hear from you soon!

function askDate(agent) {
const date1 = agent.parameters.date;

return admin.database().ref('date').transaction((date)=>
{
    let askdate = date.askDate;
    let activity = date.activity;

    let askdate1 = date.twelfthAugust.askDate;
    let activity1 = date.twelfthAugust.activity;

    let askdate2 = date.fourthAugust.askDate;
    let activity2 = date.fourthAugust.activity;

    let askdate3 = date.ninthAugust.askDate;
    let activity3 = date.ninthAugust.activity;
    if (date1 == askdate)
    {
        agent.add("We have " + activity);
    }
    else if (date1 == askdate1)
    {
        agent.add("We have " + activity1);
    }
    else if (date1 == askdate2)
    {
        agent.add("We have " + activity2);
    }
    else if (date1 == askdate3)
    {
        agent.add("We have " + activity3);
    }
    else 
    {
        agent.add("We do not have any events on this day!");
    }
    return date;
},
function (error, isSuccess) {
    console.log('Success: ' + isSuccess);

});
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
JuzChoco
  • 53
  • 1
  • 1
  • 9
  • please share the use case. – Abhinav Tyagi Jul 20 '18 at 10:41
  • @AbhinavTyagi hi there, basically, there are a few things a user can ask the google home, such as events and courses. For this instance, the user wishes to ask Google Home if there is an event on 5th August 2018, for example. The dialogflow will then reference the database, and search for the date stated by the user, and inform them is there is an event on that date. My concern is that if I keep using the if else statement, it will not be very efficient as there will be more events to be added. As such, is there a way to take simplify the if-else statement? – JuzChoco Jul 21 '18 at 05:28

1 Answers1

0

You just need to design intent to capture a date from the user. Pass this date to webhook and use something like "select event from DB where date = userProvidedDate"
Instead of if else, you should fetch the event from the DB based on the date. You need to look for how you will be doing that.

Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60