我正在使用 Dialogflow 构建一个应用程序。用户回答一些问题,并可以稍后查看他们的答案。我的问题是构建服务器以返回用户之前的答案。
这是到目前为止的代码,其中意图是 QUESTION_1 和 QUESTION_2,参数是 GRATEFUL_1 和 GRATEFUL_2:
'use strict';
process.env.DEBUG = 'actions-on-google:*';
const App = require('actions-on-google').DialogflowApp;
const functions = require('firebase-functions');
// a. the action names from the Dialogflow intents
const QUESTION_1 = 'Question-1';
const QUESTION_2 = 'Question-2';
// b. the parameters that are parsed from the intents
const GRATEFUL_1 = 'any-grateful-1';
const GRATEFUL_2 = 'any-grateful-2';
exports.JournalBot = functions.https.onRequest((request, response) => {
const app = new App({request, response});
console.log('Request headers: ' + JSON.stringify(request.headers));
console.log('Request body: ' + JSON.stringify(request.body));
// Return the last journal entry
function reflect (app) {
let grateful_1 = app.getArgument(GRATEFUL_1);
app.tell('Here is your previous entry: ' + grateful_1);
}
// Build an action map, which maps intent names to functions
let actionMap = new Map();
actionMap.set(QUESTION_1, reflect);
app.handleRequest(actionMap);
});
我希望将“reflect”函数映射到 GRATEFUL_2 响应以及 GRATEFUL_1。我知道如何做到这一点,但我如何更改下一点以包含这两个意图:
actionMap.set(QUESTION_1, reflect);
最佳答案
如果您希望 QUESTION_2 意图也转到 reflect()
函数,您只需添加
actionMap.set(QUESTION_2, reflect);
但我不认为这是你的问题。在 reflect()
中,您需要知道是什么意图让您到达那里。
您可以使用 app.getIntent()
获取包含意图名称的字符串,然后将其与您想要给出的响应进行匹配。所以这样的事情可能会起作用:
function reflect( app ){
let intent = app.getIntent();
var grateful;
switch( intent ){
case QUESTION_1:
grateful = GRATEFUL_1;
break;
case QUESTION_2:
grateful = GRATEFUL_2;
break;
}
var response = app.getArgument( grateful );
app.tell( 'You previously said: '+response );
}
当然还有其他变体。
根本不要求您实际使用 actionMap
和 app.handleRequest()
。如果您想通过另一种方式根据意图字符串确定要提供的输出,则可以随意使用它。
关于node.js - 使用 DialogFlowApp 的 actionMap 将多个意图映射到一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472434/