如何在Dialogflow实现中获取当前意图的名称?

时间:2018-12-06 16:27:00

标签: dialogflow dialogflow-fulfillment

我想获取实现中当前意图的名称,以便根据我所处的不同意图处理不同的响应。但是我找不到它的功能。

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

3 个答案:

答案 0 :(得分:1)

request.body.queryResult.intent.displayName将给出意图名称。

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

但是如果您在intentMap.set中使用两个不同的功能,则更有意义

答案 1 :(得分:1)

使用intentMap或为每个Intent创建单个Intent Handler并没有什么魔力或特殊之处。 handleRequest()函数所做的全部工作就是查看action.intent以获取Intent名称,从地图中获取具有该名称的处理程序,调用它,并可能处理它返回的Promise。

但是,如果您要违反约定,则应该有很好的理由。每个Intent都有一个Intent处理程序,可以很清楚地为每个匹配的Intent执行什么代码,这使您的代码更易于维护。

您似乎要执行此操作的原因似乎是因为两个处理程序之间存在大量重复的代码。在您的示例中,这是在获取datetime参数,但也可能还有很多其他东西。

如果这是真的,那么请执行程序员几十年来的工作:将这些任务推送到可以从每个处理程序调用的函数中。因此,您的示例可能看起来像这样:

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

答案 2 :(得分:-1)

您可以尝试使用“ agent.intent”,但对两个不同的intent使用相同的功能就没有意义。