定义操作 (Dialogflow)

Action 是应用的入口点,用于定义应用的调用和发现模型。您可以在称为 Action 软件包的 JSON 文件中声明 Action,之后当要测试或提交 Actions 项目以供审批时,您会将其上传到开发者项目。Action 包是一个 JSON 文件,用于定义您的 Actions 项目中的 Action。

如需在 Action 软件包中定义 Action,您需要创建一个 intent,用于定义 Action 的调用方式,以及 intent 何时触发的相应执行方式端点。您可以创建以下类型的 Action:

  • 默认操作:每个 Actions 项目都必须有一个欢迎 intent,作为用户发起对话的入口点。当用户通过说出 Action 的名称(例如,“Hey Google, talk to ExampleAction”)明确调用 Action 时,就会触发欢迎 intent。此欢迎意图由 actions.intent.MAIN 意图名称标识。
  • 适用于深层链接的其他 Action:您可以在 Action 包中使用自己定义的 intent 创建其他 Action。这样一来,用户便可以通过说出 Action 名称及 intent 来调用特定功能(例如:“Hey Google, talk to ExampleAction to find some shoes”)。

如需详细了解这些调用模型的工作原理,请参阅意图和调用

定义默认 Action

每个 Action 软件包都必须有且只能有一个用于处理 actions.intent.MAIN intent 的 intent。当用户通过名称(例如“Hey Google, talk to ExampleAction”)调用 Action 时,就会触发此 intent。

如需生成名为 action.json 的样板 Action 软件包文件,请按以下步骤操作:

  1. 下载 gactions CLI
  2. 为 Action 项目的源文件创建本地目录。
  3. 在终端中运行以下命令:

    $ cd PROJECT_DIRECTORY
    $ gactions init

生成 Action 软件包文件后,将占位符内容替换为您的值。下面是一个包含 ExampleAction 更改的 action.json 示例:

{
  "actions": [
    {
      "description": "Default welcome intent",
      "name": "MAIN",
      "fulfillment": {
        "conversationName": "ExampleAction"
      },
      "intent": {
        "name": "actions.intent.MAIN",
        "trigger": {
          "queryPatterns": [
            "talk to ExampleAction"
          ]
        }
      }
    }
  ],
  "conversations": {
    "ExampleAction": {
      "name": "ExampleAction",
      "url": "https://www.example.com/ExampleAction"
    }
  },
  "locale": "en"
}

定义其他操作

您可以提供其他 Action 作为入口点。如此一来,用户就可以针对自己想做什么,指定更多详细信息来明确其意图(例如,“Hey Google, talk to ExampleAction to find me some shoes.”)。

如需定义其他操作,请执行以下操作:

  1. actions 数组中,为每个入口点指定一个 Action。

    例如,以下代码显示了一个额外的“buy”操作,用于定义:
    • intent 名称 com.example.ExampleAction.BUY
    • parameters,用于在触发此 intent 时从用户输入中解析。如果您需要在用户调用 Action 时来自 Action 短语的特定数据,这种做法非常有用。
    • queryPatterns,用于定义用户需要说什么才能触发 intent。查询句式可包括用于定义要解析的参数的 Schema.org 类型。
    {
      "description": "Direct access",
      "name": "BUY",
      "fulfillment": {
        "conversationName": "ExampleAction"
      },
      "intent": {
        "name": "com.example.ExampleAction.BUY",
        "parameters": [
          {
            "name": "color",
            "type": "org.schema.type.Color"
          }
        ],
        "trigger": {
          "queryPatterns": [
            "find some $org.schema.type.Color:color sneakers",
            "buy some blue suede shoes",
            "get running shoes"
          ]
        }
      }
    }
          
  2. 通过指定与 conversations 对象中的某一项对应的 conversationName,可以为此 intent 指定执行方式。

    {
      "conversations": {
        "ExampleAction": {
          "name": "ExampleAction",
          "url": "https://www.example.com/ExampleAction"
        }
      }
    }
        

下面是一个完整的 Action 软件包示例:

{
  "actions": [
    {
      "description": "Default welcome intent",
      "name": "MAIN",
      "fulfillment": {
        "conversationName": "ExampleAction"
      },
      "intent": {
        "name": "actions.intent.MAIN",
        "trigger": {
          "queryPatterns": [
            "talk to ExampleAction"
          ]
        }
      }
    },
    {
      "description": "Direct access",
      "name": "BUY",
      "fulfillment": {
        "conversationName": "ExampleAction"
      },
      "intent": {
        "name": "com.example.ExampleAction.BUY",
        "parameters": [
          {
            "name": "color",
            "type": "org.schema.type.Color"
          }
        ],
        "trigger": {
          "queryPatterns": [
            "find some $org.schema.type.Color:color sneakers",
            "buy some blue suede shoes",
            "get running shoes"
          ]
        }
      }
    }
  ],
  "conversations": {
    "ExampleAction": {
      "name": "ExampleAction",
      "url": "https://www.example.com/ExampleAction"
    }
  },
  "locale": "en"
}