建構專案

動作專案會將所有動作封裝至單一容器。個人中心 將這項專案發布到 Actions on Google,讓 Google 助理瞭解如何探索 並體驗你的對話式服務

動作專案元件
圖 1. 動作專案結構

您可以使用下列低階元件建構動作專案:

  • 設定和資源 - 定義專案中繼資料 以及專案圖示等資源Google 會使用這些資訊發布 將動作套用至 Google 助理目錄,方便使用者探索及叫用 具體做法是指示 Kubernetes 建立並維護 一或多個代表這些 Pod 的物件

  • 意圖代表要執行的工作,例如 使用者輸入內容或需要處理的系統事件最常見的是 要採用的意圖是使用者意圖這些意圖可讓您宣告 由 NLU (自然語言理解) 自然延伸的詞組 加入更多類似的詞組NLU 使用 這些詞組即可訓練 Google 助理用來比對使用者的語言模型 。在對話期間,若部分使用者輸入內容與意圖的語言相符 Google 助理執行階段會將意圖傳送至您的動作, 並回覆使用者

  • 類型可讓您從使用者輸入內容中擷取結構化資料。變更者: 為訓練詞組加上類型註解,NLU 可從中擷取相關且結構化的 因此您不必剖析開放式輸入。

  • Scenes 會處理意圖,是 你的動作。例如運算單元填充、評估條件邏輯、傳回 提示,甚至是呼叫外部網路服務來執行 商業邏輯場景搭配意圖是絕佳的 偵測特定使用者輸入內容或系統事件,並執行相應的 邏輯。

  • 提示會定義靜態或動態回應, 回覆使用者

  • Webhook 可讓您將額外工作委派給網路服務 (執行要求),例如驗證資料或產生提示。您的動作 透過基於 JSON 的 Webhook 通訊協定與執行要求通訊。

  • 互動式畫布:可讓您建立豐富內容 透過採用 HTML、CSS 和 JavaScript 的網頁應用程式,打造身歷其境的體驗。

建立專案

請先在 Actions 控制台建立專案,才能進行開發 「Google 助理」。建立專案:

  1. 前往動作控制台
  2. 按一下 [新專案]
  3. 輸入專案名稱,然後按一下「建立專案」。
  4. 在「您想建構哪種動作?」畫面中,選取 選擇最能代表專案的類別,然後點選「Next」(下一步)
  5. 在「How you want to build it」畫面中,選擇建構方式 然後點選「開始建構」。舉例來說,您可以先從一個空白欄位 或樣本
,瞭解如何調查及移除這項存取權。

建立本機 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 會使用這個 以將專案部署並發布至 Google 助理 目錄

名稱 說明
目錄資訊 提供資訊,讓 Actions on Google 能夠發布 複製到 Google 助理目錄包含中繼資料和說明 。
指定地區 設定動作可使用的語言代碼。
途徑功能 設定可使用動作的介面。
公司詳細資料 指定貴公司的聯絡資訊。
品牌驗證 連結您擁有的網站或 Android 應用程式,即可享有額外福利 ,例如保留的叫用名稱和動作中的網站連結。
版本 為您的動作設定不同的測試和正式版。 進行測試和正式版。
Google 助理連結 允許使用者從網站資源叫用你的動作。

如何定義專案資訊:

  1. 為「sdk/settings/settings.yaml」中的動作專案定義全域設定。 請參閱設定參考資料 支援值的說明文件。

    以下程式碼片段為 sdk/settings/settings.yaml 檔案範例:

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

  2. 定義可能會因使用者的語言代碼而改變的設定 (例如, 不同語言的叫用詞組) 來初始化 sdk/settings/<locale>/settings.yaml 檔案、 將語言代碼替換成您的目標語言代碼。

    請參閱 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
    ...
    

新增資源

您可以將圖片檔、音訊檔案和字串等資源儲存在 動作專案並從設定檔參照這些項目 (例如 定義或條件) 使用系統定義的 $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 主控台會提供模擬工具,讓您預覽動作的實際效果。 模擬工具可讓您查看偵錯資訊、設定裝置功能、模擬 語言代碼等等

圖 3. 模擬工具的主要區域:(1) 使用者輸入內容 (2) 裝置檢視畫面、(3) 選項和設定,以及 (4) 對話記錄。

測試專案的步驟如下:

  1. 執行 gactions deploy preview,將動作部署至「預覽」並啟用 進行測試。
  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