3

I want to get the name of the current intent in the fulfillment so I can deal with different response depending on different intent i'm at. But I cannot find a function for it.

function getDateAndTime(agent) {    
    date = agent.parameters.date; 
    time = agent.parameters.time;

    // Is there any function like this to help me get current intent's name?
    const intent = agent.getIntent();
}

// I have two intents are calling the same function getDateAndTime()
intentMap.set('Start Booking - get date and time', getDateAndTime);
intentMap.set('Start Cancelling - get date and time', getDateAndTime);
dylankeh
  • 31
  • 3
  • this libraray might help in getting all intents or getting an intent through its id, https://www.npmjs.com/package/dialogflow-helper I wrote this library on the top of dialogflow rest client – Inzamam Malik Feb 17 '20 at 14:06

3 Answers3

1

request.body.queryResult.intent.displayName will give the the intent name.

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });

  function getDateAndTime(agent) {
      // here you will get intent name
      const intent = request.body.queryResult.intent.displayName;
      if (intent == 'Start Booking - get date and time') {
        agent.add('booking intent');
      } else if (intent == 'Start Cancelling - get date and time'){
          agent.add('cancelling intent');
      }
  }

  let intentMap = new Map();
  intentMap.set('Start Booking - get date and time', getDateAndTime);
  intentMap.set('Start Cancelling - get date and time', getDateAndTime);
  agent.handleRequest(intentMap);
});

But it would made more sense if you use two different functions in intentMap.set

sid8491
  • 6,622
  • 6
  • 38
  • 64
  • Thank you sir. Although calling the same function is not the best way to do it, I will still remember I can use your way to get intent name. – dylankeh Dec 07 '18 at 14:00
  • @dylankeh yeah, you should put repetitive tasks in a function and call it in both intents. :) – sid8491 Dec 07 '18 at 14:03
1

There is nothing magical or special about using the intentMap or creating a single Intent Handler per intent. All the handleRequest() function does is look at action.intent to get the Intent name, get the handler with that name from the map, call it, and possibly dealing with the Promise that it returns.

But if you're going to violate the convention, you should have a very good reason for doing so. Having a single Intent Handler per Intent makes it very clear what code is being executed for each matched Intent, and that makes your code easier to maintain.

It looks like your reason for wanting to do this is because there is significant duplicate code between the two handlers. In your example, this is getting the date and time parameters, but it could be many more things as well.

If this is true, do what programmers have been doing for decades: push these tasks to a function that can be called from each handler. So your examples might look something like this:

function getParameters( agent ){
  return {
    date: agent.parameters.date,
    time: agent.parameters.time
  }
}

function bookingHandler( agent ){
  const {date, time} = getParameters( agent );
  // Then do the stuff that uses the date and time to book the appointment
  // and send an appropriate reply
}

function cancelHandler( agent ){
  const {date, time} = getParameters( agent );
  // Similarly, cancel things and reply as appropriate
}

intentMap.set( 'Start Booking', bookingHandler );
intentMap.set( 'Cancel Booking', cancelHandler );
Prisoner
  • 49,922
  • 7
  • 53
  • 105
0

you can try using "agent.intent" but it doesn't make sense to use the same function for two different intents.

Reza Nasiri
  • 1,360
  • 1
  • 6
  • 19