使用 Actions SDK 為 Google 助理建構動作 (第 2 級)

1. 總覽

Google 助理開發人員平台可讓您建立軟體,在超過 10 億部裝置上擴充 Google 助理 (虛擬個人助理) 的功能,包括智慧音箱、手機、汽車、電視、耳機等。使用者可以與 Google 助理對話,完成購物或預約車輛等事項。開發人員可以透過 Google 助理開發人員平台,輕鬆建立及管理使用者與第三方服務之間,令人愉悅且有效的對話體驗。

本程式碼研究室涵蓋使用 Google 助理開發的中級概念,並以「使用 Actions SDK 建構 Google 助理適用的動作 (第 1 級)」程式碼研究室中建立的動作為基礎。強烈建議您先完成第 1 級程式碼研究室,再開始進行本程式碼研究室。

在本程式碼研究室中,您建立的動作會根據使用者選擇的輔助工具,告訴他們在神話世界 Gryffinberg 冒險的命運。

建構項目

在本程式碼研究室中,您將建構具有下列功能的進階對話式動作:

  • 收集使用者資料,並視值而定修改對話提示。
  • 提出後續問題,進一步延續對話。
  • 建立遊戲迴圈,讓使用者在收到幸運籤餅後,可以再次與動作互動。

開始建構前,請在支援 Google 助理的裝置上說出「Ok Google,跟命運與財富對話」,與即時 Action 互動。回訪使用者透過這項動作的預設路徑如下:

dd6f5c61296b8b50.png

eba043f546aa8c51.png

課程內容

  • 如何使用位置收集使用者資料
  • 如何使用條件為場景新增邏輯
  • 如何新增遊戲迴圈
  • 如何新增支援路徑

軟硬體需求

本程式碼研究室的先決條件包括:

強烈建議您熟悉 JavaScript (ES6),雖然這不是必要條件,但有助於瞭解本程式碼研究室的完成程式碼。

非必要步驟:取得程式碼範例

您可以選擇從 Actions Builder 程式碼研究室第 1 級 GitHub 存放區取得第 1 級專案的完整程式碼,以便跟著本程式碼研究室操作。您也可以在這個 GitHub 存放區中,查看第 2 級專案的完整程式碼。

2. 繼續建構對話式介面

在第一個程式碼研究室中,您建立了一個簡單的對話動作,其中包含單一場景 Start

在本程式碼研究室中,您將擴充動作的對話。在以下各節中,您將設定動作執行下列操作:

  • 在使用者想聽取運勢時,轉換至新的 Fortune 場景
  • 詢問使用者想在旅程中選擇哪種輔助工具
  • 根據使用者的選擇提供客製化運勢

切換至並建立Fortune場景

在本節中,執行以下操作:

  • Start 場景中移除現有提示,該提示會回應使用者並結束對話
  • 定義從 Start 場景到 Fortune 場景的轉場效果
  • 建立 Fortune 場景

如要修改 Start 場景並在 Fortune 場景中新增轉場效果,請按照下列步驟操作:

  1. 在文字編輯器中開啟第 1 級程式碼研究室的 Actions 專案。
  2. 開啟 custom/scenes/Start.yaml 檔案。
  3. 更新 yes 意圖的 handler,讓程式碼與下列程式碼片段相符:

Start.yaml

intentEvents:
- intent: "yes"
  transitionToScene: Fortune
- handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech: I understand, stranger. Best of luck on your quest! Farewell.
  intent: "no"
  transitionToScene: actions.scene.END_CONVERSATION
  1. 儲存檔案。

如要建立名為「Fortune」的新場景,請按照下列步驟操作:

  1. 在終端機中前往程式碼研究室第 1 級的 Actions 專案。
  2. scenes 目錄中建立名為 Fortune.yaml 的新檔案:
touch custom/scenes/Fortune.yaml

您將在下一節編輯這個檔案。

定義 Fortune 場景的對話邏輯

在本程式碼研究室中,您將設定 Fortune 場景,向使用者詢問「What do you choose to help you on your quest, a dragon, a translator, or a compass?」(您會選擇哪項工具來協助完成任務?是龍、翻譯機還是指南針?)您可以使用「運算單元填充」功能,在繼續操作前收集使用者的必要資訊。

你的動作會為三種輔助工具提供運勢:龍、翻譯機和指南針。如要設定動作,讓系統在使用者輸入內容中識別這三種選項,您必須建立新的型別

您可以在場景的填寫空位階段使用型別,定義您想從使用者取得的資訊。當 NLU 引擎在使用者輸入內容中偵測到相符的 slot 時,會將該 slot 擷取為型別參數,方便您在場景中執行相關邏輯。

建立 available_options 類型

在本節中,您將建立名為 available_options 的新型別,指定使用者可選擇的三個選項 (dragon、translator 和 compass),以回應提示。您也可以為這些選項定義幾個同義詞,以防使用者說出類似的內容。在後續章節中,您會在 slot 中新增 available_options 型別,指定要取得使用者的選擇。

如要建立 available_options 類型,請按照下列步驟操作:

  1. 建立名為 types 的新目錄:
mkdir custom/types
  1. types 目錄中建立名為 available_options.yaml 的新檔案:
touch custom/types/available_options.yaml
  1. 在文字編輯器中開啟 custom/types/available_options.yaml

類型會設定為資訊的鍵/值組合,其中是類型名稱,則是該鍵的同義字。定義鍵時,系統會自動將其新增為值。使用 Actions SDK 時,您會以 entities 表示鍵,並以 synonyms 表示值。

如要新增使用者可選擇的三個選項,請按照下列步驟操作:

  1. available_options.yaml 檔案中新增下列 entitiessynonyms

available_options.yaml

synonym:
  entities:
    dragon:
      synonyms:
      - dragon
      - hydra
      - lizard
    translator:
      synonyms:
      - translator
      - communicator
      - machine
      - decoder
      - translate
    compass:
      synonyms:
      - compass
      - direction
      - guide
      - navigator
  matchType: EXACT_MATCH
  1. 儲存檔案。

現在,動作會將 available_options 視為龍、翻譯人員和指南針,並可辨識幾個對應的同義字。

設定運算單元填充

接著,您需要在 Fortune 場景中設定填寫欄位。如要設定填滿空位的邏輯,請按照下列步驟操作:

  1. 在文字編輯器中開啟 custom/scenes/Fortune.yaml
  2. 將下列 slots 資料新增至 Fortune.yaml 檔案:

Fortune.yaml

slots:
- commitBehavior:
    writeSessionParam: chosenOptions
  name: chosenOptions
  promptSettings:
    initialPrompt:
      staticPrompt:
        candidates:
        - promptResponse:
            firstSimple:
              variants:
              - speech: What do you choose to help you on your quest, a dragon, a translator, or a compass?
            suggestions:
            - title: Dragon
            - title: Translator
            - title: Compass
  required: true
  type:
    name: available_options
  1. 儲存檔案。

您已將 available_options 類型新增至該時段,這會告知動作您需要從使用者收集的資訊 (他們選擇的輔助工具),然後才能繼續。您也在該位置設定了提示,使用者到達場景的填寫位置階段時,系統會將提示加入提示佇列。

命名位置 chosenOptions 時,writeSessionsParam 欄位會更新為相同名稱 ($session.params.chosenOptions)。您可以在提示和透過用戶端程式庫執行的動作中,依該名稱存取這個參數。

新增條件

您已新增需要使用者選擇輔具的意圖,現在可以新增條件,檢查是否已取得意圖資料,使用者才能繼續對話。

在本節中,您將新增 scene.slots.status == "FINAL" 條件,檢查是否已填滿所有空位。填滿所有位置後,條件會將提示 (You picked $session.params.chosenOptions.) 新增至提示佇列。

如要設定 scene.slots.status == "FINAL" 條件,請按照下列步驟操作:

  1. 在文字編輯器中開啟 custom/scenes/Fortune.yaml
  2. Fortune.yaml 檔案頂端新增 conditionalEvents 資料:

Fortune.yaml

conditionalEvents:
- condition: scene.slots.status == "FINAL"
  handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech: You picked $session.params.chosenOptions.
  1. 儲存檔案。

在模擬工具中測試動作

此時,您已定義使用者應選取哪些選項來填入該時段。從使用者取得這項資訊後,動作應提供參照使用者所選特定選項的提示。

如要測試動作,請按照下列步驟操作:

  1. 在終端機執行下列指令:
gactions deploy preview

輸出內容應如下所示:

✔ Done. You can now test your changes in Simulator with this URL: http://console.actions.google.com/project/{project-id}/simulator?disableAutoPreview
  1. 複製提供的網址並貼到瀏覽器中。
  2. 在「Input」(輸入) 欄位中點按或輸入 Talk to my test app,然後按下 Enter 鍵。
  3. 在「Input」欄位中輸入 Yes,然後按下 Enter 鍵。或者,您也可以按一下「是」建議方塊。

a899d45c542668f6.png

  1. 按一下、輸入或說出 dragon。系統應會提示「You picked dragon」(你選了龍)。

在下一節中,您可以為使用者可能選取的每個輔助工具自訂提示。

使用條件自訂提示

在這個部分中,您可以為使用者可選擇的每個選項新增條件,並為每個條件新增自訂提示。

自訂 dragon 幸運籤餅

如要更新條件,並在使用者選擇「dragon」時自訂提示,請按照下列步驟操作:

  1. 在文字編輯器中開啟 custom/scenes/Fortune.yaml
  2. Fortune.yaml 檔案中,將 conditionalEvents 資料替換為下列程式碼片段:

Fortune.yaml

conditionalEvents:
- condition: scene.slots.status == "FINAL" && session.params.chosenOptions == "dragon"
  handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech: The people of Gryffinberg will be awestruck by the beauty and
                power of the ancient dragon. Much to your dismay, the townspeople
                fall into dispute over who will receive the honor of riding the dragon
                first. You return home from your quest without everlasting glory or
                a dragon.
  1. 儲存檔案。

現在,當使用者說出「龍」或類似的詞彙時,你的動作會根據該選取項目提供運勢。接著新增其餘兩個選項。

自訂 translatorcompass 幸運籤餅

如要新增條件,並自訂使用者說出「翻譯」或「指南針」時的提示,請按照下列步驟操作:

  1. custom/scenes/Fortune.yaml 檔案中,於 dragon 條件下方新增另外兩個條件:

Fortune.yaml

- condition: scene.slots.status == "FINAL" && session.params.chosenOptions == "translator"
  handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech: With the help of the translator, the rival factions in Gryffinberg
                are finally able to communicate with each other and resolve their
                disputes. You will complete your quest to restore peace in the town.
                The translator will be used on many other journeys across the
                earth. After its work is done, it retires honorably to a premier location
                in the Gryffinberg History Museum.
- condition: scene.slots.status == "FINAL" && session.params.chosenOptions == "compass"
  handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech: The compass will help you find the mystical and ancient Library
                of Gryffinberg. Among its infinite stacks of dusty books, you find
                one entitled "Wisdom of the Ages". By the time you've read the 50,000-page
                tome, the townspeople have forgotten their problems. You will write
                a second edition of "Wisdom of the Ages", but have limited commercial
                success.
  1. 儲存檔案。

在模擬工具中測試動作

此時,您的動作應根據使用者選取的選項,提供客製化運勢。

如要測試動作,請按照下列步驟操作:

  1. 在終端機執行下列指令:
gactions deploy preview

輸出內容應如下所示:

✔ Done. You can now test your changes in Simulator with this URL: http://console.actions.google.com/project/{project-id}/simulator?disableAutoPreview
  1. 複製提供的網址並貼到瀏覽器中。
  2. 在「輸入」欄位中輸入 Talk to my test app,然後按下 Enter 鍵。
  3. 在「Input」(輸入) 欄位中輸入「Yes」,然後按下 Enter 鍵。或者,按一下「是」建議方塊。
  4. 按一下、輸入或說出 Translator

29e17f950bd0dd71.png

您應該會收到「翻譯人員」選項的相應運勢。

3. 新增遊戲迴圈

在本節中,您將設定動作,讓使用者選取其他選項,並在選取後聽到不同的運勢。這項變更類似於遊戲結束時顯示的「要再玩一次嗎?」訊息。如要建構這個迴圈,您可以重複使用先前建立的 yesno 意圖,並將其新增至名為 Again 的新場景。

建立Again場景

在本節中,您會建立新的 Again 場景,並新增提示,詢問使用者是否要選取其他選項。

如要建立及設定 Again 場景,請按照下列步驟操作:

  1. scenes 目錄中建立名為 Again.yaml 的新檔案:
touch custom/scenes/Again.yaml
  1. 在文字編輯器中開啟 custom/scenes/Again.yaml
  2. Again.yaml 中新增下列 onEnter 資料:

Again.yaml

onEnter:
  staticPrompt:
    candidates:
    - promptResponse:
        firstSimple:
          variants:
          - speech: That is what I see for you. Would you like to choose a different option and explore another future?
        suggestions:
        - title: "Yes"
        - title: "No"
  1. 儲存檔案。

Fortune 轉場到 Again 場景

使用者收到運勢後,對話必須轉換至新的 Again 場景。

如要從 Fortune 場景新增轉場效果至 Again 場景,請按照下列步驟操作:

  1. 在文字編輯器中開啟 custom/scenes/Fortune.yaml
  2. 在每個條件中新增 transitionToScene: Again,如以下程式碼片段所示:

Fortune.yaml

conditionalEvents:
- condition: scene.slots.status == "FINAL" && session.params.chosenOptions == "dragon"
  handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech: The people of Gryffinberg will be awestruck by the beauty and
                power of the ancient dragon. Much to your dismay, the townspeople
                fall into dispute over who will receive the honor of riding the dragon
                first. You return home from your quest without everlasting glory or
                a dragon.
  transitionToScene: Again
- condition: scene.slots.status == "FINAL" && session.params.chosenOptions == "translator"
  handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech: With the help of the translator, the rival factions in Gryffinberg
                are finally able to communicate with each other and resolve their
                disputes. You will complete your quest to restore peace in the town.
                The translator will be used on many other journeys across the
                earth. After its work is done, it retires honorably to a premier location
                in the Gryffinberg History Museum.
  transitionToScene: Again
- condition: scene.slots.status == "FINAL" && session.params.chosenOptions == "compass"
  handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech: The compass will help you find the mystical and ancient Library
                of Gryffinberg. Among its infinite stacks of dusty books, you find
                one entitled "Wisdom of the Ages". By the time you've read the 50,000-page
                tome, the townspeople have forgotten their problems. You will write
                a second edition of "Wisdom of the Ages", but have limited commercial
                success.
  transitionToScene: Again
  1. 儲存檔案。

在模擬工具中測試動作

此時,Action 應在使用者收到運勢後,向他們顯示下列提示:「這就是我為你看到的運勢。你想選擇其他選項,探索另一個未來嗎?」

如要測試動作,請按照下列步驟操作:

  1. 在終端機執行下列指令:
gactions deploy preview

輸出內容應如下所示:

✔ Done. You can now test your changes in Simulator with this URL: http://console.actions.google.com/project/{project-id}/simulator?disableAutoPreview
  1. 複製提供的網址並貼到瀏覽器中。
  2. 在「輸入」欄位中輸入 Talk to my test app,然後按下 Enter 鍵。
  3. 在「Input」欄位中輸入 Yes,然後按下 Enter 鍵。或者,按一下「是」建議方塊。
  4. 按一下、輸入或說出 dragon

b299e9fed9aedb69.png

您應該會收到龍選項的運勢和 Again 提示。

新增意圖並轉換至 Again 場景

在本節中,您會將 yesno 意圖新增至 Again 場景,讓動作瞭解使用者是否要選擇新選項。您也為 yesno 意圖新增適當的轉場效果。yes 意圖會轉換為 Fortune 場景,而 no 意圖則會轉換為系統場景 End conversation

如要將意圖和轉場效果新增至 Again 場景,請按照下列步驟操作:

  1. 在文字編輯器中開啟 custom/scenes/Again.yaml
  2. Again.yaml 檔案頂端,於 OnEnter 上方新增 intentEvents 資料:

Again.yaml

intentEvents:
- intent: "yes"
  transitionToScene: Fortune
- handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech: It pleases me that you are satisfied with your choice. Best
                of luck on your quest. Farewell.
  intent: "no"
  transitionToScene: actions.scene.END_CONVERSATION
  1. 儲存檔案。

在模擬工具中測試動作

現在,動作應該可以瞭解使用者是否要選擇新選項或結束對話。

如要測試 yes 意圖,請按照下列步驟操作:

  1. 在終端機執行下列指令:
gactions deploy preview

輸出內容應如下所示:

✔ Done. You can now test your changes in Simulator with this URL: http://console.actions.google.com/project/{project-id}/simulator?disableAutoPreview
  1. 複製提供的網址並貼到瀏覽器中。
  2. 在「輸入」欄位中輸入 Talk to my test app,然後按下 Enter 鍵。
  3. 在「Input」欄位中輸入 Yes,然後按下 Enter 鍵。或者,按一下「是」建議方塊。
  4. 點按、輸入或說出其中一個選項。
  5. 在「輸入」欄位中輸入 Yes,然後按下 Enter 鍵

5d0690332efe2e29.png

系統應會顯示提示:「你希望在冒險旅程中獲得哪種協助?是龍、翻譯員還是指南針?」

如要測試 no 意圖,請按照下列步驟操作:

  1. 點按、輸入或說出其中一個選項。
  2. 在輸入欄位中輸入 No,然後按下 Enter 鍵。

系統應會顯示「很高興你對自己的選擇感到滿意。End conversation祝你順利完成任務。Farewell.」(瞭解,我已將您的預約排定於 8 月 1 日星期三下午 3 點。期待您的光臨,再見。) 回應中也同樣含有使用者話語內容提及的時間和日期資訊。

4. 新增支援路徑

您已建構 Action 中大多數使用者採取的路徑。不過,使用者可能會在 Fortune 場景中,對「你選擇哪項工具來協助完成任務?是龍、翻譯機還是指南針?」提示做出回應,但選擇的工具並非提供的選項。

在本節中,您將設定 Action,瞭解使用者何時說出「magic」、「money」、「horse」或「phone」,並在使用者選擇其中一個選項時,重新提示使用者從原始的三個選項中選取一個。如要設定這項邏輯,您需要建立新的 type,其中包含這些其他選項,以及新的意圖 other_option,當使用者說出其中一個選項時,系統就會比對這個意圖。您也需要在 other_option 意圖中註解訓練詞組,以識別及擷取意圖參數

當 Google 助理的自然語言處理引擎在使用者輸入內容中偵測到相符的參數時,就會將該值擷取為已輸入的參數,方便您在場景中執行相關邏輯。在本程式碼研究室中,您會設定動作,擷取使用者選擇的輔助工具,並在提示中參照該選擇。

建立 unavailable_options 類型

您現在可以建立 unavailable_options 型別,其中包含各種不同選項,讓動作能識別使用者輸入內容中的資料。

如要建立 unavailable_options 類型,請按照下列步驟操作:

  1. types 目錄中建立名為 unavailable_options.yaml 的新檔案:
touch custom/types/unavailable_options.yaml
  1. 在文字編輯器中開啟 custom/types/unavailable_options.yaml
  2. 將下列 synonyms 資料新增至 unavailable_options.yaml 檔案:

unavailable_options.yaml

synonym:
  entities:
    money:
      synonyms:
      - money
      - cash
      - gold
    horse:
      synonyms:
      - horse
      - stallion
      - steed
    magic:
      synonyms:
      - magic
      - enchanted
      - spells
    phone:
      synonyms:
      - phone
      - cell
      - apps
  matchType: EXACT_MATCH
  1. 儲存檔案。

建立 other_option 意圖

接著,建立名為 other_option 的意圖,並加入包含 unavailable_options 類型選項的訓練詞組。當使用者選取 unavailable_options 類型中包含的選項時,系統就會比對這個意圖。

如要建立及設定 other_option 意圖,請按照下列步驟操作:

  1. intents 目錄中建立名為 other_option.yaml 的新檔案:
touch custom/intents/other_option.yaml
  1. 在文字編輯器中開啟 custom/intents/other_option.yaml
  2. 將下列 parameters 資料和 trainingPhrases 資料新增至 other_option.yaml 檔案:

other_option.yaml

parameters:
- name: chosenUnavailableOption
  type:
    name: unavailable_options
trainingPhrases:
- I want to use ($chosenUnavailableOption 'spells' auto=true)
- I really really want to use a ($chosenUnavailableOption 'phone' auto=true)
- ($chosenUnavailableOption 'magic' auto=true)!
- ($chosenUnavailableOption 'cash' auto=true)
- I want to ride a ($chosenUnavailableOption 'horse' auto=true)

在這裡,您會手動為訓練詞組加上註解,並使用上一節中指定的不可用選項。意圖參數 chosenUnavailableOption 可讓您擷取選項名稱,並在提示中使用該選項,這部分會在下一節說明。

  1. 儲存檔案。

other_option 意圖新增至 Fortune 場景

您現在有一個意圖 (other_option),可處理使用者指定非原始選項的選項。在本節中,您會將 other_option 意圖新增至 Fortune 場景。您可以根據使用者的輸入內容,使用意圖參數自訂提示。

如要將 other_option 意圖新增至 Fortune 場景,請按照下列步驟操作:

  1. 在文字編輯器中開啟 custom/scenes/Fortune.yaml
  2. conditionalEvents 資料和 slots 資料之間新增下列 intentEvents 資料:

Fortune.yaml

intentEvents:
- handler:
    staticPrompt:
      candidates:
      - promptResponse:
          firstSimple:
            variants:
            - speech:  I have seen the future and a $intent.params.chosenUnavailableOption.original will not aid you on your journey.
  intent: other_option
  1. 儲存檔案。

運算式 $intent.params.chosenUnavailableOption 是指意圖參數物件,而 $intent.params.chosenUnavailableOption.original 則是指該物件的值。original 屬性是指使用者指定的原始輸入內容。

當使用者在 Fortune 場景中說出 unavailable_options 類型列出的選項時,系統會比對 other_option 意圖,並將提示詞加入提示詞佇列。由於未指定任何轉場效果,場景執行迴圈會繼續重新評估條件階段。chosenOptions 片段接著會將提示新增至提示佇列,並將提示佇列傳送給使用者。

在模擬工具中測試動作

現在,當使用者選取 unavailable_options 類型中列出的其中一個選項時,動作應會適當回應,並指定使用者選取的輔助工具。接著,你的動作應會再次提示使用者選擇其中一個原始選項 (龍、翻譯器或指南針)。

如要在模擬器中測試動作,請按照下列步驟操作:

  1. 在終端機執行下列指令:
gactions deploy preview

輸出內容應如下所示:

✔ Done. You can now test your changes in Simulator with this URL: http://console.actions.google.com/project/{project-id}/simulator?disableAutoPreview
  1. 複製提供的網址並貼到瀏覽器中。
  2. 在「輸入」欄位中輸入 Talk to my test app,然後按下 Enter 鍵。
  3. 在「Input」欄位中輸入 Yes,然後按下 Enter 鍵。或者,按一下「是」建議方塊。
  4. 在「輸入」欄位中輸入 magic,然後按下 Enter 鍵。

3a42c33eca435f32.png

使用者選擇「magic」時,您可能會發現提示的發音不正確,因為「magic」前面有冠詞「a」。您將在後續章節中解決這個問題。

新增 unavailable_options 處理常式

如要在 unavailable_options 類型中適當的選項前加上「a」冠詞,您可以在出貨邏輯中設定事件處理常式,檢查使用者選擇的選項是否需要加上「a」。首先,您需要設定動作,從 Fortune 場景呼叫處理常式。

如要將 unavailable_options 處理常式新增至 Fortune 場景,請按照下列步驟操作:

  1. 在文字編輯器中開啟 custom/scenes/Fortune.yaml
  2. 使用下列 intentEvents 資料更新 Fortune.yaml 檔案:

Fortune.yaml

intentEvents:
- handler:
    webhookHandler: unavailable_options
  intent: other_option
  1. 儲存檔案。

更新並部署完成

您已設定動作來呼叫 unavailable_options 事件處理常式,現在可以更新執行要求中的處理常式並部署。

如要更新執行要求,請按照下列步驟操作:

  1. 在文字編輯器中開啟 webhooks/ActionsOnGoogleFulfillment/index.js
  2. greeting 處理常式下方的 index.js 中新增下列程式碼:

index.js

app.handle('unavailable_options', conv => {
  const option = conv.intent.params.chosenUnavailableOption.original;
  const optionKey = conv.intent.params.chosenUnavailableOption.resolved;
  let message = 'I have seen the future and ';
  if(optionsNeedA.has(optionKey)){
    message = message + 'a ';
  }
  message = message + `${option} will not aid you on your journey. `;
  conv.add(message);
});
  1. const app = conversation({debug:true}); 下方新增下列程式碼:

index.js

const optionsNeedA = new Set();
optionsNeedA.add('horse').add('phone');
  1. 儲存檔案。

瞭解程式碼

unavailable_options 處理常式會執行下列動作:

  • conv 物件取得 option 資料,並將 option 指派給 original 屬性,這是使用者的原始輸入內容
  • optionKey 指派給 resolved 屬性,這是 unavailable_options 類型的鍵
  • 檢查 optionKey 是否為需要「a」的選項之一;如果是,則建構訊息時會加入「a」
  • 透過 conv.add(message) 新增訊息

更新處理常式

如要允許動作使用 unavailable_options,請將 unavailable_options 處理常式新增至 webhooks/ActionsOnGoogleFulfillment.yaml

  1. unavailable_options 處理常式名稱新增至 ActionsOnGoogleFulfillment.yaml

ActionsOnGoogleFulfillment.yaml

handlers:
- name: greeting
- name: unavailable_options
inlineCloudFunction:
  executeFunction: ActionsOnGoogleFulfillment
  1. 儲存檔案。

在模擬工具中測試動作

現在,Action 應會根據使用者從 unavailable_options 類型中選擇的項目,調整提示是否需要加上「a」冠詞。

如要測試動作,請按照下列步驟操作:

  1. 在終端機執行下列指令:
gactions deploy preview

輸出內容應如下所示:

✔ Done. You can now test your changes in Simulator with this URL: http://console.actions.google.com/project/{project-id}/simulator?disableAutoPreview
  1. 複製提供的網址並貼到瀏覽器中。
  2. 在「Input」(輸入) 欄位中點按或輸入 Talk to my test app,然後按下 Enter 鍵。
  3. 在「Input」欄位中輸入 Yes,然後按下 Enter 鍵。或者,按一下「是」建議方塊。
  4. 在「Input」欄位中輸入 magic,然後按下 Enter 鍵。接著,在「輸入」欄位中輸入 horse,然後按下 Enter 鍵

54ee24c5c3c56e.png

您的動作應在「horse」選項前加上「a」冠詞,同時建構提示,但「magic」選項前不加「a」冠詞。

5. 在 Actions 主控台中查看動作

Actions SDK 可與整合至 Actions 管理中心的網頁式 IDE (稱為 Actions Builder) 互通。您可以使用 gactions push 指令,將本機檔案系統推送至控制台中的動作草稿。Actions 控制台會以圖像化方式呈現動作的設定。在開發期間,以視覺化方式查看動作對應關係很有幫助,且不會影響用於測試的動作版本。

如要推送 Actions 專案並在 Actions 控制台中查看,請按照下列步驟操作:

  1. 在終端機中執行下列指令,將專案推送至 Actions 控制台:
gactions push

輸出內容應如下所示:

✔ Done. Files were pushed to Actions Console, and you can now view your project with this URL: https://console.actions.google.com/project/{project-id}/overview. If you want to test your changes, run "gactions deploy preview", or navigate to the Test section in the Console.
  1. 複製提供的網址並貼到瀏覽器中。
  2. Actions 控制台中,按一下頂端導覽列中的「開發」
  3. 按一下「場景」旁的下拉式箭頭,然後按一下「開始」。您應該會看到 Action Start 場景的視覺化呈現方式,如下方螢幕截圖所示:

cae526c647f8d40f.png

清除專案所用資源 (建議)

為避免產生可能費用,建議您移除不打算使用的專案。如要刪除您在本程式碼研究室中建立的專案,請按照下列步驟操作:

  1. 如要刪除 Cloud 專案和資源,請完成「關閉 (刪除) 專案」一節列出的步驟。
  1. 選用:如要立即從 Actions 管理中心移除專案,請完成「刪除專案」一節列出的步驟。如果未完成這個步驟,系統會在約 30 天後自動移除專案。

6. 恭喜!

您現在已具備中階技能,可使用 Actions SDK 建構 Google 助理動作。

涵蓋範圍

  • 如何使用 Node.js 執行要求資料庫開發對話式動作
  • 如何使用「位置」收集使用者資料
  • 如何使用條件在場景中新增邏輯
  • 如何新增遊戲迴圈
  • 如何新增支援路徑

瞭解詳情

如要瞭解如何建構 Google 助理動作,請參閱下列資源:

在 Twitter 上追蹤 @ActionsOnGoogle,隨時掌握最新公告,並在推文中加上 #AoGDevs,分享您開發的內容!

意見回饋問卷調查

離開前,請填寫簡短的問卷調查,分享你的體驗。