使用 DialogFlowApp 的 actionMap 将多个意图映射到一个函数

Mapping mulitiple intents to one function using actionMap for a DialogFlowApp

我正在使用 Dialogflow 构建应用程序。用户回答了一些问题,稍后可以查看他们的回答。我的问题是将服务器构建为 return 用户之前的答案。

这是到目前为止的代码,其中意图是 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 );
}

当然还有其他变体。

根本不需要您实际使用 actionMapapp.handleRequest()。如果你有其他方式想要根据意图字符串来确定你想要给出哪个输出,你可以随意使用它。