2018/11/01

Google Home Mini, (2) Google Actions 開發筆記 (Google Actions)


萊恩大兵的桌上有兩顆智慧音箱, 一是 Amazon Echo Dot, 另一個是 Google Home Mini. 先前萊恩大兵有花一些時間研究 Alexa Skill 的架構與程式編寫 (請參考 Amazon Echo Dot, (2) Alexa Skill 開發筆記), Google Home Mini 就放著沒在用. 最近總算等到 Google Assistant 能支援中文了, 也讓萊恩大兵有動力想了解 G陣營的語音助理對話程式, 即 Google Actions.


首先, 先把兩邊陣營的名詞或服務流程做對應比較.

A陣營的語音助理服務流程: 使用者 -> Amazon Echo -> Alexa -> Skill (intent + service endpoint [AWS Lambda])


G陣營的語音助理服務流程: 使用者 -> Google Home -> Google Assistant -> Action (intent + fulfillment [webhook- Firebase])



本篇文章是依 Codelabs 的範例做逐步的講解, 目標是為 Google Assistant 增添一個 action. 當啟動這個 action 之後, Google Assistant 會問使用者最喜歡的顏色, 然後 Google Assistant 會根據使用者的答覆產生一個幸運數字回覆給使用者.

[1] 首先, 確認你的 Google 帳號有開啟需要的權限.

- 登入 Google 帳號, 開啟 Activity Control 頁面.
- 確認 Web & App Activity (網路和應用程式活動), Device Information (裝置資訊) 和 Voice and Audio Activity (語音和音訊活動) 三項是開啟的.






[2] 在 Actions Console 新增一個專案.

- 萊恩大兵將專案取名為 actions-001, 按 CREATE PROJECT.

- 按 SKIP 跳過類別選項.


- 進入 actions-001 的專案設定頁面, 點選左邊選單裡面的 Invocation.

- 進入 Invocation 頁面, 在 Display name 輸入這個 action 的喚醒詞 (Doctor Milk), 即 invocation. 按 “Save” 儲存.

- 點選左邊選單的 Actions, 在 Actions 頁面點按 ADD YOUR FIRST ACTION 新增一個 action.

- 在 CREATE ACTION 的頁面, 選 Custom Intent, 按 Build.

- 系統自動帶入 DialogFlow 的登入網頁. 按 ACCEPT 接受服務條款.

接下來是重頭戲了:

 [3] 在 DialogFlow 編寫 intent 與設定回應的文句等.

- 按 “Create” 新增一隻 Dialogflow agent.

- 進入 Intents 設定頁面, 裡面有兩個預設的 intents. Default Fallback Intent 與 Default Welcome Intent, 其中 Default Welcome intent 是每隻 action 被啟動時第一個執行的 intent. 

簡單起見, 就利用 Default Welcome Intent 修改來做示範. 點按 Default Welcome Intent.

- , 進入 Default Welcome Intent 的設定頁面. 往下捲到 Responses, 將預設的回應文句刪掉, 再新增一則回應文句: “Welcome! What is your favorite color?”, 點按 SAVE 儲存.

接下來就可以用模擬器來測試:

[4] 透過模擬器來測試包括: 透過 invocation 啟動 action, 並跑到 Default Welcome Intent 的整段流程.

- 在 Dialogflow Console 的左邊選單, 選取 Integrations, 切換到 Integration 頁面.

- 點按 Google Assistant 裡面的 INTEGRATION SETTINGS.

- 在 Google Assistant 的設定頁面上, 點按 TEST, 系統會花一小段時間把整個專案設定與 Google Assistant 模擬器串接起來.



完成後, 會自動帶出 Google Actions Simulator.  (除了模擬器, 也可以直接在 Google Assistant 測試.)

簡單測試一下:
* 輸入 "Talk to Doctor Milk"
* 模擬器回應 “Sure. Here’s the test version of Doctor Milk. Welcome! What is your favorite color?”

接下來再複雜一點, 

[5] 讓 Google Assistant 能解析你的回覆.

- 在 Dialogflow Console 的左邊選單, 點按 Intents, 進入 Intents 頁面, 再點按 CREATE INTENT.

- 在 Intent 設定頁面, 輸入 intent name 為 favorite color, 點按 SAVE.

- 點按 ADD TRAINING PHRASES, 輸入幾組用戶可能的答覆. Dialogflow 會自行解析文句中的顏色, 並配給一個 entity 變數 (@sys.color).

- 再往下捲到 Action and parameters, Dialogflow 已新增一組參數: color | @sys.color, 將它勾選起來, 點按 Define prompts. Dialogflow 會彈出一個對話視窗, 問你對這個參數的提問語詞, 可輸入 “What is your favorite color?” 後, 按 CLOSE 關閉.

- 再往下捲到 Fulfillment, 開啟 “Enable webhook call for this intent”, 再按 “SAVE” 儲存.

這個時候 Google Assistant 已能抓出你的語句中的顏色單詞. 還需要做 Fulfillment 和 webhook 的設定, 才能讓這個 action 的動作正確的完成.

[6] Implement a webhook.

- 在 Dialogflow Console 的左邊選單, 點按 Fulfillment. 在 Filfillment 設定頁面, 啟用 Inline Editor.



- 將 Inline Editor 原有的程式碼刪除, 貼上這段範例程式.
'use strict';

// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');

// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');

// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});

// Handle the Dialogflow intent named 'favorite color'.
// The intent collects a parameter named 'color'.
app.intent('favorite color', (conv, {color}) => {
    const luckyNumber = color.length;
    // Respond with the user's lucky number and end the conversation.
    conv.close('Your lucky number is ' + luckyNumber);
});

// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);





- 這段 Java Script 主要是在處理 “favorite color” intent, 將擷取出來的參數 color, 依其長度轉換成一個數字回覆給使用者 (幸運數字) . 按 DEPLOY 佈署上去.

再回到模擬器做測試, 這回就能和 Google Assistant 對話, 跟它講一個顏色, 它會回覆你幸運數字.



[萊恩大兵的其它文章]

自製大四軸

CC2540 Bluetooth Low Energy

OpenCV 學習路徑

小惡魔 無線溫度感測器




沒有留言:

張貼留言