Dialogflow:如何在Firebase查询中传递参数? [英] Dialogflow: How do I pass a parameter through in a Firebase query?

查看:72
本文介绍了Dialogflow:如何在Firebase查询中传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase中拥有一个实时数据库,并在Google Cloud的Dialogflow中设置了一个代理。该代理程序代理正在获取有关总线路由名称的数据。向最终用户询问公交车号,代理商根据该路线号获得相关信息。我可以呼叫数据库,但只能拨打设定的总线号。

I have a Realtime DB in Firebase and have setup an agent in Google Cloud's Dialogflow. This agent agent is fetching data about bus route names. The end user is asked for a bus number and the agent should get relevant info based on that route number. I can call the database but only for a set bus number.

因此,例如,在下面的示例中,我可以基于来获取 100 的公交信息snapshot.child 设置为 100 。但是我希望 snapshot.child 基于Dialogflow的 askBus 参数进行更改。有什么建议么?

So for example below I can pull in bus info for 100 based on having the snapshot.child set to 100. But I want the snapshot.child to change based on the askBus parameter from Dialogflow. Any suggestions?

function handleBus(agent) {
    const bus = agent.parameters.bus;

    agent.add(`Thank you...`);
    return admin.database().ref('Routes').once("value").then((snapshot) => {
      var routeInfo = snapshot.child('100/route_desc').val();
      var routeName = snapshot.child('100/route_long_name').val();
      agent.add(`Bus info is ` + routeInfo + ' and is called ' + routeName);


推荐答案

通常,最好的处理方式这是在设置查询路径时引用总线号的节点,一旦得到结果肯定可以得到它,但这意味着您要提取 lot 个数据

In general, the best way to handle this is to reference the node of the bus number as part of setting up the path to the query. Getting it once you have the result is certainly possible, but means you're pulling in a lot more data than you need to for each query.

但是有几种方法可以做到这一点。

But there are a few ways to do this.

与您现在的操作最相似的是生成一个包含路由号的字符串,该示例说明了如何使用反引号来实现此功能,最新的JavaScript中提供了反引号,或者您也可以进行字符串连接:

The one most similar to how you're doing it now is to generate a string that includes the route number. This example shows how to do it using a back-quote, which is available in the most recent JavaScript, or you can just do string concatenation:

function handleBus(agent) {
    const bus = agent.parameters.bus;

    agent.add(`Thank you...`);
    return admin.database().ref('Routes').once("value").then((snapshot) => {
      var routeInfo = snapshot.child(`${bus}/route_desc`).val();
      var routeName = snapshot.child(`${bus}/route_long_name`).val();
      agent.add(`Bus info is ` + routeInfo + ' and is called ' + routeName);

但是如果您只是从中查找信息route,您可以设置对数据库的引用以包括该路由,获取整个结果及其值,然后将其视为JavaScript对象。

But if you're just looking for the information from that route, you can setup the reference to the database to include the route, get the entire result and its value, and then treat this as a JavaScript object.

function handleBus(agent) {
    const bus = agent.parameters.bus;

    agent.add(`Thank you...`);
    return admin.database().ref('Routes').child(bus).once("value").then((snapshot) => {
      var route = snapshot.val();
      var routeInfo = route['route_desc'];
      var routeName = route['route_long_name'];
      agent.add(`Bus info is ` + routeInfo + ' and is called ' + routeName);

顺便说一句,我想指出的是,您完美地使用了Promises。这是很多人陷入的陷阱,您已经很好地通过Promise查询了值,将其作为Promise履行的一部分进行处理,并在处理程序中返回Promise。

As an aside, I want to point out that you're using Promises perfectly. That is a trap many people fall into, and you've done a good job querying the value through a Promise, handling it as part of Promise fulfillment, and returning a Promise in your handler.

这篇关于Dialogflow:如何在Firebase查询中传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆