操作是应用的入口点,用于定义调用和发现 模型。您需要在一个 JSON 文件(称为 Action 软件包)中声明 Action, 稍后在您想要测试或测试时 提交您的 Actions 项目以供审批。操作包是一种 JSON 文件 定义了 Actions 项目中的 Actions。
如需在 Action 软件包中定义 Action,可以创建一个 intent,用于定义如何 系统将调用 Action 及相应的执行端点, 意图。你可以创建以下类型的操作:
- 默认操作:每个 Actions 项目都必须有一个用于执行相应操作的欢迎 intent
作为用户发起对话的入口点欢迎意图是
当用户通过说出 Action 的名称(例如
例如“Hey Google, talk to ExampleAction”)。此欢迎意图通过
actions.intent.MAINintent 名称。 - 适用于深层链接的其他操作:您可以在以下位置创建其他操作: 和您自行定义的 intent 相关联的 Action 包。这样,用户 通过说出 Action 名称和 intent 来调用特定功能 (例如:“Hey Google, talk to ExampleAction to find some shoes”。
如需详细了解如何使用 intent 和调用,请参阅 intent 和调用 这些调用模型的工作原理。
定义默认操作
每个 Action 包必须有且仅有一个负责处理
actions.intent.MAIN intent。当用户调用您的
按名称执行的操作(例如,“Hey Google, talk to ExampleAction”。
如需生成名为 action.json 的样板操作软件包文件,请获取
操作步骤:
- 下载
gactionsCLI。 - 为 Action 项目的源文件创建本地目录。
在终端中运行以下命令:
$ 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,跟 ExampleAction 对话,为我找到一些 鞋子。”)。
要定义其他操作,请执行以下操作:
-
在
例如,以下代码显示了一个额外的“buy”定义以下内容的操作: <ph type="x-smartling-placeholder">actions数组中,为每个入口点指定一个 Action。- </ph>
com.example.ExampleAction.BUY的 intent 名称parameters,以便在触发此 intent 时从用户输入中解析。 如果您需要从“操作”语句获取特定数据, 用户调用 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" ] } } }
-
通过指定
conversationName为此 intent 指定执行方式 该对象与conversations对象中的某个项相对应。{ "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" }