构建项目

<ph type="x-smartling-placeholder">
</ph> <ph type="x-smartling-placeholder"></ph>

Actions 项目会将您的所有 Action 打包到一个容器中。您 将此项目发布到 Actions on Google,以便 Google 助理了解如何发现 并调用您的对话体验。

actions 项目组件
图 1. Actions 项目结构

您可以使用以下低级组件来构建 Actions 项目:

  • 设置和资源用于定义项目元数据 以及项目图标等资源Google 会利用这些信息 将 Action 发布到 Google 助理目录中,以便用户发现和调用 。

  • 意图表示要执行的任务,例如 需要处理的用户输入或系统事件。最常见的 intent 就是用户意图借助这些 intent,你可以声明训练 由 NLU(自然语言理解)自然扩展的短语 包含更多类似的词组。NLU 使用 来训练 Google 助理用来匹配用户 输入。在对话期间,如果某些用户输入与 intent 的语言匹配 模型,Google 助理运行时会将 intent 发送给 Action,以便它能够 进行处理并响应用户

  • 类型可让您从用户输入中提取结构化数据。修改者 为训练短语添加类型注解,NLU 可以提取相关的结构化 数据,因此您无需解析开放式输入。

  • Scene 功能可以处理 intent,并且是 Google Cloud 上的 您的 Action。它们可以执行槽位填充、评估条件逻辑、返回 提示用户,甚至可以调用外部 Web 服务来执行 业务逻辑与 intent 结合使用时,场景可以让您 检测特定用户输入或系统事件, 逻辑。

  • 提示定义了用于执行以下任务的静态或动态响应: 回复用户

  • 利用 Webhook,您可以将额外的工作委托给网络服务 (执行方式),例如验证数据或生成提示。你的操作 它通过基于 JSON 的 webhook 协议与您的 fulfillment 进行通信。

  • 借助 Interactive Canvas, 以及运用 HTML、CSS 和 JavaScript 的 Web 应用打造的沉浸式体验。

创建项目

您必须先在 Actions 控制台中创建一个项目,然后才能针对 Google 助理。如需创建项目,请执行以下操作:

  1. 转到 Actions 控制台
  2. 点击 New project
  3. 为你的项目输入名称,然后点击 Create Project
  4. What kind of Action do you want to build? 屏幕中,选择 选择一个最能代表您的项目的类别,然后点击下一步
  5. How do you like to build it 屏幕中,选择一种构建方式 然后点击 Start building。例如,您可以先使用一个空白的 项目或示例。
。 <ph type="x-smartling-placeholder">

创建本地 Actions SDK 项目

在 Actions 控制台中创建 Actions 项目后,你可以 在本地开发环境中初始化项目。

如需通过现有 Actions 项目初始化 Actions SDK 项目,请按照下列步骤操作: 具体步骤:

  1. 在本地文件系统上为 Actions 项目创建一个空目录。
  2. 在此空目录中,创建一个 sdk 目录。
  3. 在终端中将工作目录更改为 sdk 目录。

从空项目开始

如果您想从您刚刚在控制台中创建的空项目开始, 运行 gactions pull --project-id <my-project-id>

$ mkdir myAction
$ cd myAction
$ mkdir sdk
$ cd sdk
$ gactions pull --project-id my-project-id
Pulling your project files from Draft for a project id: "my-project-id"
✔ Done. You should see the files written in path/to/myAction/sdk

从示例项目开始

如果要从示例项目开始,请运行 gactions init <sample name>

$ mkdir actions-test
$ cd actions-test
$ mkdir sdk
$ cd sdk
$ gactions init question
Writing sample files for question.
✔ Done. Please checkout the following documentation - https://developers.google.com/assistant/conversational/build on the next steps on how to get started.

定义项目信息

项目的设置和资源定义了项目的相关信息,例如 功能和界面支持、支持的语言区域、显示名称、说明 徽标等等。下表介绍了主要设置和资源 。Actions on Google 会使用此项 部署和发布项目 目录

名称 说明
目录信息 提供相关信息,以便 Actions on Google 能够发布您的 导出到 Google 助理目录。包含元数据和删减内容 有关徽标和横幅图片的项目和图片资源。
地理位置定位 配置支持您的 Action 的语言区域。
Surface 功能 配置您的 Action 可以在哪些 surface 上可用。
公司详细信息 指定贵公司的联系信息。
品牌验证 关联你拥有的网站或 Android 应用,获享额外福利 例如在您的 Action 中使用预留的调用名称和网站链接。
版本 为您的 Action 配置不同的测试版本和正式版 进行测试和生产。
Google 助理链接 让用户通过您的网络媒体资源调用您的 Action。

如需定义项目信息,请执行以下操作:

  1. sdk/settings/settings.yaml 中为 Actions 项目定义全局设置。 请参阅设置参考。 有关支持的值的文档。

    以下代码段显示了一个示例 sdk/settings/settings.yaml 文件:

    accountLinking:
      enableAccountCreation: true
      linkingType: GOOGLE_SIGN_IN
    category: GAMES_AND_TRIVIA
    projectId: my-project-id
    ...
    

    <ph type="x-smartling-placeholder">
  2. 根据用户的语言区域,定义不同的设置(例如, 不同语言的调用短语)放在 sdk/settings/<locale>/settings.yaml 文件中, 将 locale 替换为您的目标语言区域。

    请参阅 LocalizedSettings 参考文档。

    以下代码段是英语设置在 sdk/settings/en/settings.yaml 文件:

    localizedSettings:
      developerEmail: developer@developers.com
      developerName: Developer Name
      displayName: My Display Name
      fullDescription: full description of the action
      largeBannerImage: https://path/to/large/banner
      privacyPolicyUrl: http://path/to/privacy/policy
      sampleInvocations:
      - Talk to My Display Name
      shortDescription: short description of the action
      smallLogoImage: https://path/to/small/logo
      voice: female_1
    ...
    

添加资源

您可以将图片文件、音频文件和字符串等资源 Actions 项目并从配置文件(例如 定义或条件)。$resources

项目资源存储在 resources/ 下,每种资源类型分别是 在目录中分配了一个文件夹。

您可以在资源中创建特定于语言区域的文件夹来本地化资源 例如,你可以在文件夹中存储西班牙语版本的字符串 resources/strings/es/<filename>.yaml

图片

图片文件存储在 resources/images/ 中,您可引用它们 和$resources.images.<name of the image file without file extension>。 允许的文件扩展名包括:

  • gif
  • png
  • jpg
  • jpeg

例如,如果将英文版小徽标保存在 resources/images/en/square.png,而英语版大型横幅则是 已分别保存在resources/images/en/landscape.jpg中 上一个示例中的 sdk/settings/en/settings.yaml 将变为:

localizedSettings:
  developerEmail: developer@developers.com
  developerName: Developer Name
  displayName: My Display Name
  fullDescription: full description of the action

  largeBannerImage: $resources.images.landscape
  privacyPolicyUrl: http://path/to/privacy/policy
  sampleInvocations:
  - Talk to My Display Name
  shortDescription: short description of the action

  smallLogoImage: $resources.images.square
  voice: female_1
...

音频文件

音频文件存储在 resources/audio/ 中,供您引用 和$resources.audio.<name of the audio file without file extension>

允许的文件扩展名包括:

  • mp3
  • mpeg

例如,您可以在提问时引用录音:

candidates:
  - first_simple:
      variants:
        - speech: your speech response
    content:
      media:
        mediaType: audio
        mediaObjects:
          - name: media name
            description: media description
            url: $resources.audio.intro

字符串

字符串以 .yaml 文件的形式存储在 resources/strings/ 中。每个文件均包含 字符串键和相关值的映射,可以是单个字符串或列表 字符串。您可以使用 $resources.strings.<name of the image file without file extension>.<key> 或从列表中获取随机值;以及 $resources.strings.<name of the image file without file extension>.<key>.<numerical index>

例如,使用资源字符串进行字符串本地化, 上一个示例中的 sdk/settings/en/settings.yaml 可能变为:

localizedSettings:
  developerEmail: developer@developers.com
  developerName: $resources.strings.appinfo.developerName
  displayName: $resources.strings.appinfo.displayName
  fullDescription: $resources.strings.appinfo.fullDescription
  largeBannerImage: $resources.images.landscape
  privacyPolicyUrl: $resources.strings.appinfo.privacyPolicyUrl
  sampleInvocations:
  - $resources.strings.invocations.sample
  shortDescription: $resources.strings.appinfo.shortDescription
  smallLogoImage: $resources.images.square
  voice: female_1
...

在模拟器中测试项目

Actions 控制台提供了一个模拟器,供您预览 Action。通过 通过模拟器,您可以查看调试信息、设置设备功能、 语言区域等。

图 3. 模拟器的主要方面:(1) 用户输入、 (2) 设备视图,(3) 选项和设置,以及 (4) 对话日志。

如需测试项目,请执行以下操作:

  1. 运行 gactions deploy preview 以将 Action 部署到“预览”并启用 在模拟器中进行测试
  2. 在命令输出中打开该网址以访问模拟器。
$ gactions deploy preview
Deploying your project files to your Actions console preview for a project id: "my-project". This may take a few minutes.
Sending configuration files
Waiting for server to respond.
✔ Done. You can now navigate to the Actions Console simulator to test your changes: http://console.actions.google.com/project/my-project/simulator?disableAutoPreview