内置 intent (Dialogflow)

内置 intent 是唯一标识符,您可以指定它来告知 Google 助理您的 Action 可以执行特定类别的用户请求。例如,以下是 Google 助理与内置 intent 匹配的用户查询的一些示例:

  • “玩游戏”内置 intent“Ok Google。玩个记忆游戏”
  • “获取星座”内置 intent“Ok Google,了解我的星座运势”

操作发现阶段,Google 助理可以使用有关您的 Action 的元数据(包括您指定的内置 intent)向用户推荐您的 Action。为了尽可能减少对话往返,Google 助理还会尝试扫描用户查询中的参数,并将这些参数传递给您的 Action。

如需查看 Google 助理支持的内置 intent 的完整列表(包括其参数和用户查询示例),请参阅内置 intent 参考文档

集成内置 intent

您可以通过多种不同的方式集成内置 intent,具体取决于您构建 Action 的方式。

Dialogflow

如果您使用 Dialogflow 创建 Action,则可以通过 Dialogflow 控制台以图形方式附加内置 intent。

如需使用 Dialogflow 附加内置 intent,请按以下步骤操作:

  1. 打开 Dialogflow 控制台,选择您的代理,然后导航到意图屏幕。
  2. 创建或选择代理在收到特定的内置 intent 时触发的 intent。打开事件部分,然后点击添加事件

    图 1. 在 Dialogflow 控制台中添加 Dialogflow 事件。
  3. 事件字段中,输入代理的内置 intent 事件的名称(例如 actions_intent_PLAY_GAME)。

    图 2. 在 Dialogflow 控制台中将内置 intent 附加到代理。
  4. 点击保存

Actions SDK

如果您使用 Actions SDK 构建 Action,则必须指定内置 intent 与 Action 软件包中的 Action 之间的映射。

如需使用 Actions SDK 附加内置 intent,请按以下步骤操作:

  1. Action 定义的名称字段中指定内置 intent。
  2. 使用 gactions 工具将 Action 包上传到 Actions 项目,如 Actions SDK 概览中所述。

例如,以下代码段展示了如何添加 CHECK_AIR_QUALITY 内置 intent:

{
   "actions":[
      {
         "description":"Default Welcome Intent",
         "name":"MAIN",
         "fulfillment":{
            "conversationName":"conversation_1"
         },
         "intent":{
            "name":"actions.intent.MAIN"
         }
      },
      {
         "description":"Check Air Quality",
         "name":"CHECK_AIR_QUALITY",
         "fulfillment":{
            "conversationName":"conversation_1"
         },
         "intent":{
            "name":"actions.intent.CHECK_AIR_QUALITY"
         }
      }
   ],
   "conversations":{
      "conversation_1":{
         "name":"conversation_1",
         "url":"https://example.com/fulfillment",
         "fulfillmentApiVersion":2
      }
   }
}

处理内置 intent 参数

通过内置 intent 调用您的 Action 时,您的执行方式可能会接收其他参数。intent 架构将参数的名称及其类型定义为基元类型或 schema.org 实体。如需查看对话型 Action 内置 intent 的架构,请参阅内置 intent 参考文档

内置 intent 的参数是可选的。如果参数可以从用户对内置 intent 的调用中提取,Google 助理会使用值填充参数。

例如,actions.intent.CHECK_AIR_QUALITY 内置 intent 的架构定义了四个可选参数:

参数名称 类型
attributes 字符串值。
location schema.org/Place 对象。
temporalCoverage schema.org/Duration 对象。
timeIndicator EnumeratedDuration(Google 专用扩展程序)。

以下代码段显示了一个对话网络钩子 (JSON) 请求示例,该示例在用户通过说出“明天旧金山的空气质量如何?”调用您的 Action 时:

"inputs":[
      {
         "intent":"actions.intent.CHECK_AIR_QUALITY",
         "rawInputs":[
            {
               "inputType":"VOICE",
               "query":"what is the air quality in san francisco tomorrow"
            }
         ],
         "arguments":[
            {
               "name":"location",
               "structuredValue":{
                  "geo":{
                     "longitude":-122.41941550000001,
                     "latitude":37.7749295
                  },
                  "@context":"https://schema.org",
                  "@type":"Place",
                  "name":"san francisco"
               }
            },
            {
               "name":"temporalCoverage",
               "rawText":"2018-04-25",
               "textValue":"2018-04-25"
            }
         ]
      }
   ]

在此示例中,参数采用以下值:

  • location 参数包含“San Francisco”的 schema.org/Place 值。
  • temporalCoverage 参数包含表示明天日期相对于调用时间的 schema.org/Duration 值。
  • attributestimeIndicator 参数没有值,因为用户的调用短语不包含此类信息。

如果您使用的是 Node.js 版 Actions on Google 客户端库,则可以检索参数值,如以下代码段所示:

app.intent('actions.intent.CHECK_AIR_QUALITY', (conv) => {
  const attributes = conv.arguments.get('attributes');
  const location = conv.arguments.get('location');
  const temporal_coverage = conv.arguments.get('temporalCoverage');
  Const time_indicator = conv.arguments.get('timeIndicator')

  // Your Action logic. If you need to use any of the parameter values,
  // you should check first that it is defined. Arguments.get returns
  // undefined if it can't find a value for a parameter.

});

测试与内置 intent 的集成

请按照以下步骤测试您的集成:

  1. 打开已启用测试 Action 的 Actions 模拟器,或在设备上打开 Google 助理。
  2. 说出或输入与该内置 intent 关联的查询。例如,“我想玩游戏。”
  3. 从显示的应用选择对话框中,找到您的 Action。
  4. 选择要向您的应用发送 intent 的应用。
图 3. 从内置 intent 短语生成
的操作选择对话框。
图 4. 调用附加到内置 intent 的 Action。

使用内置 intent 的最佳实践

使用内置 intent 时,您应遵循以下最佳实践:

  • 将内置 intent 映射到特定操作:当特定的内置 intent 触发您的 Action 时,应尽可能顺畅地将用户转到 Action 中的特定 intent 和功能。例如,如果您的 Action 支持 PLAY_GAME 内置 intent 并接收该 intent,您应立即将用户引导至该 Action 的游戏功能。避免再次询问用户是否要玩游戏。
  • 处理内置 intent 参数:请务必使用 Google 助理发送到您的执行方式的内置 intent 参数值。避免重新提示用户输入这些值。