【问题标题】:Mapping mulitiple intents to one function using actionMap for a DialogFlowApp使用 DialogFlowApp 的 actionMap 将多个意图映射到一个函数
【发布时间】:2017-11-24 11:32:19
【问题描述】:

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

这是目前为止的代码,intent是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);
});

我希望将“反射”函数映射到 GRATEFUL_2 响应以及 GRATEFUL_1。我知道如何做到这一点,但我如何改变下一点以包含两个意图:

  actionMap.set(QUESTION_1, reflect);

【问题讨论】:

    标签: node.js webhooks dialogflow-es actions-on-google


    【解决方案1】:

    如果您希望 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()。如果您有其他方法想根据意图字符串确定要提供的输出,您可以随意使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      相关资源
      最近更新 更多