部署執行要求 (Dialogflow)

建構 Webhook 執行要求後,請將其部署至實際工作環境,並連結至動作。下列步驟說明如何設定環境,以便部署至實際工作環境託管的 Cloud Functions for Firebase。但您可以選擇任何支援 HTTPS 要求與回應的網站代管平台,以代管執行要求。

如果您已將 Webhook 部署至網站代管平台,可以略過「將 Webhook 連結至動作」一節。請注意,以下程序的最終步驟會因您使用的是 Dialogflow 或 Actions SDK 而有所不同。

部署至 Cloud Functions for Firebase

  1. 下載並安裝 Node.js
  2. 設定並初始化 Firebase CLI。如果下列指令失敗並顯示 EACCES 錯誤,您可能需要變更 npm 權限

    npm install -g firebase-tools
    
  3. 使用 Google 帳戶驗證 Firebase 工具:

    firebase login
    
  4. 前往動作專案目錄並初始化 Firebase。系統會請您選取要為 Actions 專案設定哪些 Firebase CLI 功能。選擇 Functions 和其他您可能會要使用的功能,例如 Firestore,然後按下 Enter 鍵確認並繼續操作:

    cd <cloud_function_dir>
    firebase init
    
  5. 如要將 Firebase 工具與您的 Actions 專案建立關聯,請使用方向鍵選取專案清單:

    」指令來建立關聯。
  6. 選擇專案後,Firebase 工具會啟動函式設定,詢問您想使用的語言。使用方向鍵選取並按下 Enter 鍵繼續。

    === Functions Setup
    A functions directory will be created in your project with a Node.js package pre-configured. Functions can be deployed with firebase deploy. ? What language would you like to use to write Cloud Functions? (Use arrow keys) > JavaScript TypeScript
  7. 選擇是否要使用 ESLint 來偵測可能發生的錯誤,並強制執行 YN 樣式輸入:

    ? Do you want to use ESLint to catch probable bugs and enforce style? (Y/n)
  8. 在提示中輸入 Y,取得專案依附元件:

    ? Do you want to install dependencies with npm now? (Y/n)

    設定完成後,畫面會顯示類似以下的輸出內容:

    ✔  Firebase initialization complete!
  9. 安裝 actions-on-google 依附元件:

    cd <cloud_function_dir>/functions
    npm install actions-on-google
    
  10. 取得執行要求依附元件,並部署執行要求函式:

    npm install
    firebase deploy --only functions
    

    部署作業需要幾分鐘時間。完成後,畫面會顯示類似以下的輸出內容。您需要函式網址才能輸入 Dialogflow。

    ✔  Deploy complete!
    Project Console: https://console.firebase.google.com/project/exampleproject-123/overview Function URL (cloudFunctionName): https://us-central1-exampleproject-123.cloudfunctions.net/cloudFunctionName

將 Webhook 連結至動作

如果您使用 Dialogflow:請在 Dialogflow 主控台中,前往「Fulfillment」,將「Webhook」按鈕切換為「ENABLED」,然後將「URL」欄位中的網址替換為函式網址

如果使用的是 Actions SDK:請在 conversations 物件內建立物件,以在動作套件中宣告執行要求:

{
  "actions": [
    {
      "description": "Default Welcome Intent",
      "name": "MAIN",
      "fulfillment": {
        "conversationName": "myFulfillmentFunction"
      },
      "intent": {
        "name": "actions.intent.MAIN",
        "trigger": {
          "queryPatterns": [
            "talk to myFulfillmentFunction"
          ]
        }
      }
    }
  ],
  "conversations": {
    "myFulfillmentFunction": {
      "name": "myFulfillmentFunction",
      "url": "https://us-central1-myprojectname-ab123.cloudfunctions.net/cloudFunctionName"
    }
  },
  "locale": "en"
}