Dialogflow es 如何获得当前意图';Dialogflow实现中的名称?
我想在实现中获得当前意图的名称,以便根据我所处的不同意图处理不同的响应。但我找不到它的函数Dialogflow es 如何获得当前意图';Dialogflow实现中的名称?,dialogflow-es,dialogflow-es-fulfillment,Dialogflow Es,Dialogflow Es 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 = a
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);
您可以尝试使用“agent.intent”,但对两个不同的意图使用相同的函数是没有意义的。
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
中使用两个不同的函数,则更有意义。使用intentMap
或为每个Intent创建单个Intent处理程序没有什么神奇或特殊之处。handleRequest()
函数所做的一切就是查看action.intent
要获取intent名称,从映射中获取具有该名称的处理程序,调用它,并可能处理它返回的承诺
但是如果你要违反公约,你应该有很好的理由这样做。每个意图都有一个单独的意图处理程序,这使得每个匹配的意图执行的代码非常清晰,并且使代码更易于维护
看起来您希望这样做的原因是因为两个处理程序之间存在大量重复代码。在您的示例中,这是获取日期
和时间
参数,但可能还有更多的内容
如果这是真的,那么就做程序员几十年来一直在做的事情:将这些任务推送到一个可以从每个处理程序调用的函数。因此,您的示例可能如下所示:
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 );
非常感谢。这正是我所关心的,重复的代码。你的回答非常有用。如果一个回答对你有用,接受和/或投票总是很感激的。谢谢你,先生。虽然调用同一个函数不是最好的方法,但我仍然记得我可以用你的方法来获取意图名称。@dylankeh是的,你应该在一个函数中放置重复的任务,并在两个意图中调用它。:)这个库可能有助于通过其id获取所有意图或意图,我在dialogflow rest客户端的顶部编写了这个库