Search code examples
dialogflow-esfacebook-chatbot

dialogflow Webhookclient "request_" property


I am trying to build up a facebook messenger chatbot using Dialogflow. In the dialogflow fulfillment inline editor, I found that I can use agent.request_.body to get the body of the request. I assume "request_" is a property of WebhoodClient object? But I couldn't find any documentation elaborate that, could you please advise if my understanding is correct and where I can find the reference or documentation?

const agent = new WebhookClient({ request, response });
console.log(JSON.stringify(agent.request_.body));

Thanks


Solution

  • Google provides documentation for Dialogflow webhooks here, which include this sample webhook to inspect parameters and dynamically create slot filling prompts:

    exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    
      const agent = new WebhookClient({ request, response });
    
      function flight(agent) {
        const city = agent.parameters['geo-city'];
        const time = agent.parameters['time'];
        const gotCity = city.length > 0;
        const gotTime = time.length > 0;
    
        if(gotCity && gotTime) {
            agent.add(`Nice, you want to fly to ${city} at ${time}.`);
        } else if (gotCity && !gotTime) {
            agent.add('Let me know which time you want to fly');
        } else if (gotTime && !gotCity) {
            agent.add('Let me know which city you want to fly to');
        } else {
            agent.add('Let me know which city and time you want to fly');
        }
      }
    
      let intentMap = new Map();
      intentMap.set('flight', flight);
      agent.handleRequest(intentMap);
    });
    

    My guess would be to add

    console.log(agent);
    

    right before defining the flight function, then checking the logs to see which objects agent contains, then adding iterations of console.log(agent.fakeObjectName) until you find the information you're looking for.

    If you're following the deployment process recommended in Actions on Google's Codelabs level 2, your logs will show up in the Firebase console, like this:

    Firebase console - Functions - Logs

    Hope that helps!