首页 > 解决方案 > 获取用户输入并通过 Firebase 数据库进行搜索

问题描述

我目前正在使用DialogFlowFirebase数据库做一个项目。我主要关心的是,我一直试图弄清楚很长时间是,我如何接受用户输入并在Firebase数据库中搜索它?正如您在代码中看到的那样,我现在有 if else 语句来帮助我将其过滤掉,但是随着越来越多的事件被存储在数据库中,我发现 if else 语句将不会有效和可行。所以,请告诉我如何最好地接受用户的输入并根据他们的请求搜索数据库。例如,他们可能会问“8 月 15 日有什么活动?”,然后该功能应在数据库中搜索 8 月 15 日,并告知他们当天的任何活动。希望早日收到你的消息!

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

});
}

标签: node.jsfirebasefirebase-realtime-databasedialogflow-esgoogle-home

解决方案


您只需要设计意图来捕获用户的日期。将此日期传递给 webhook 并使用类似 " select event from DB where date = userProvidedDate"
而不是 if else,您应该根据日期从数据库中获取事件。你需要寻找你将如何做到这一点。


推荐阅读