Dialogflow: How do I pass a parameter through in a

2019-08-01 10:59发布

问题:

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.

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

回答1:

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.

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

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

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.



回答2:

In the webhook use async call to firebase to fetch the bus information.

  • Fetch the parameter value.
  • Access Firebase DB.
  • Fetch information based on parameter using async call.
  • Use promise to reply back with the correct response. See this for responding via promise.
  • Promise would be used inside your Firebase function when it fetches the DB information.