1

我正在尝试将我的 VUI 配置为在 Dialogflow 提示时重复一个句子。小背景故事,我正在帮助开发一个老年人社交机器人,所以重复一个句子是一个非常需要的功能。我刚刚开始这个项目,之前负责这个项目的开发人员已经走了,无法联系到,而且我也没有 Node.js 的经验。

我希望为此使用多声部。这些是我到目前为止所做的步骤:

  1. 创建了一个名为“重复”的意图。
  2. 为意图添加了训练短语
  3. 添加了“multivocal.repeat”作为动作
  4. 为此意图启用 webhook 调用
  5. 在 Node.js 中添加了 'intentMap.set('Repeat', repeat);' 到我现有的意图地图
// Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Game_Rock_Paper_Scissors - Result', game_stone_paper_scissors);
  intentMap.set('Game_Rock_Paper_Scissors - Result - yes', game_stone_paper_scissors_again);
  intentMap.set('Conversation tree', conversation_tree);
  intentMap.set('Question date', question_date);
  intentMap.set('Question time', question_time);
  intentMap.set('Music', music);
  intentMap.set('Repeat', repeat);

接下来,在“Fulfillment”选项卡下,我想在内联编辑器“index.js”选项卡中输入函数,并确保在“package.json”中安装了多声库并且可以使用。到目前为止,我的 package.json 中有一些东西

  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "8"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "^2.2.0",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.5.0"
  }
}

那么,如何安装库呢?在 Read.me 中说“您可以使用 npm install --save multivocal 安装它。” 但是我会在哪里安装呢?我是否将其放在 index.js 或 packages.js 中的某个位置?

此外,在此示例中,它显示 index.js 代码为:

const Color = require('./color.js');
Color.init();

const Multivocal = require('multivocal');
exports.webhook = Multivocal.processFirebaseWebhook;

我应该把它添加到我的 index.js 的末尾吗?还是应该将其包装在一个函数中?很抱歉在这方面没有经验,但我希望我能解释清楚。

4

1 回答 1

4

使用 Dialogflow 内联编辑器使用多声道的一些答案:

如何将其包含在package.json文件中?

使用说明npm是如果您在本地编写实现而不使用编辑器。

您需要在该dependencies部分中添加此行:

    "multivocal": "^0.14.0"

Dialogflow / Firebase Cloud Functions 将负责导入库。您不需要“actions-on-google”、“dialogflow”或“dialogflow-fulfillment”库,因此该部分可能如下所示:

  "dependencies": {
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2",
    "multivocal": "^0.14.0"
  }

如何编写我的 index.js?

简单示例假设您可以将配置和代码放在单独的文件中(示例中为“color.js”)。由于您无法使用内联编辑器执行此操作,因此代码的一般样板将如下所示:

// Import the library on the first line, so you can call methods on it to setup your configuration
const Multivocal = require('multivocal');

// Your configuration and code go here

// Setup the webhook as the final line
exports.dialogflowFirebaseFulfillment = Multivocal.processFirebaseWebhook;

Intent Handler 注册和功能都去哪里了?

与 dialogflow-fulfillment 库不同,multivocal 不使用显式的 intentMap。它自己维护一个,所以要注册一个 Intent Handler 函数,你可以使用类似的东西

Multivocal.addIntentHandler( intentName, handlerFunction );

请记住,处理函数也有些不同。

什么?它们有何不同?

Multivocal 有很多事情是通过配置而不是代码来处理的。因此,没有直接对应的agent.add()函数调用可以与 dialogflow-fulfillment 或 actions-on-google 库中的函数调用对应。

相反,您的 Intent Handler 函数应该执行任何逻辑、数据库调用或任何其他操作以获取将在响应中使用的值并将它们保存在环境中。每个 Handler 都应该返回一个包含 Environment 的 Promise。

您还应该为您的 Intent 设置配置 - 其中最常见的是设置可能的“响应”模板。最简单的响应模板只包含文本,以及从环境中插入值的位置。提示用户下一步可以做什么也是最佳实践,因此我们可能会设置一个“后缀”模板以默认使用,或者设置一个用于特定意图、操作或输出的模板。

因此,如果您有一个名为“color.favorite”的 Intent,并且在环境中有一个名为“color”的值(您的处理程序可能已从数据库加载),则此响应的英文配置可能如下所示。它还包括一个默认后缀,用于提示用户下一步可以做什么。

  const config = {
    Local: {
      en: {
        Response: {
          "Intent.color.favorite": [
              "{{color}} is one of my favorite colors as well.",
              "Oh yes, {{color}} can be quite striking.",
              "I can certainly understand why you like {{color}}."
          ]
        },
        Suffix: {
          Default: [
            "What other color do you like?"
            "Tell me another color."
          ]
        }
      }
    }
  }

你会注册这个配置

  new Multivocal.Config.Simple( config );

您可以(并且应该)注册多个配置,尽管您可以将它们组合到一个对象中。因此,Response上面的部分可以按名称包含每个 Intent 的响应部分。

好的,但是我如何处理“重复”意图?

需要做的就是提供一个 Intent,在 Dialogflow UI 中将其“Action”设置为“multivocal.repeat”,并启用 webhook。所以这样的事情会起作用:

说明在何处配置 Action 值的意图

Multivocal 已经在此基础上注册了一个处理程序和配置。

如果要更改可能的响应,可以为“multivocal.repeat”操作添加配置。这可能看起来像这样:

const enRepeat = [
  "Sorry about that, let me try again.",
  "I said:"
];

const config = {
  Local: {
    en: {
      Response: {
        "Action.multivocal.repeat": enRepeat
      }
    }
  }
}

然后将此配置与您编写的其他配置结合起来,或者像上面那样加载它。

强调一下 - 您无需为此编写任何代码,只需一些可选配置即可。

于 2020-02-05T12:23:49.637 回答