定義動作 (Dialogflow)

動作是應用程式中的進入點,用於定義應用程式的叫用和探索模型。您可以在 JSON 檔案 (稱為動作套件) 中宣告動作,以便在之後要測試或提交動作專案核准時上傳至開發人員專案。動作套件是一個 JSON 檔案,用於定義 Actions 專案中的動作。

如要在動作套件中定義動作,您必須建立意圖來定義動作的叫用方式,以及觸發意圖時對應的執行要求端點。您可以建立下列類型的動作:

  • 預設動作:每個動作專案都必須有一個歡迎意圖,做為使用者發起對話的進入點。當使用者說出其名稱以明確叫用動作時 (例如「Ok Google,對範例動作說話」),系統就會觸發歡迎意圖。此歡迎意圖以 actions.intent.MAIN 意圖名稱識別。
  • 用於深層連結的其他動作:您可以在動作套件中使用您自行定義的意圖建立其他動作。這樣做可以讓使用者藉由說出動作名稱與意圖來叫用特定功能 (例如:「Ok Google,對範例動作說話來尋找鞋子」)。

如要進一步瞭解這些叫用模型的運作方式,請參閱「意圖與叫用」一文。

定義預設動作

每個動作套件都必須要有一個處理 actions.intent.MAIN 意圖的意圖。當使用者以名稱叫用您的動作 (例如「Ok Google,對範例動作」) 時,就會觸發此意圖。

如要產生名為 action.json 的樣板動作套件檔案,請按照下列步驟操作:

  1. 下載 gactions CLI
  2. 為動作專案的來源檔案建立本機目錄。
  3. 在終端機中執行下列指令:

    $ cd PROJECT_DIRECTORY
    $ gactions init

產生動作套件檔案後,請將預留位置內容替換成您的值。以下是含有 ExampleAction 變更的 action.json 範例:

{
  "actions": [
    {
      "description": "Default welcome intent",
      "name": "MAIN",
      "fulfillment": {
        "conversationName": "ExampleAction"
      },
      "intent": {
        "name": "actions.intent.MAIN",
        "trigger": {
          "queryPatterns": [
            "talk to ExampleAction"
          ]
        }
      }
    }
  ],
  "conversations": {
    "ExampleAction": {
      "name": "ExampleAction",
      "url": "https://www.example.com/ExampleAction"
    }
  },
  "locale": "en"
}

定義其他動作

您可以提供其他動作做為進入點。如此一來,使用者就能針對想做的事指定更多詳細資料 (例如「Ok Google,用範例動作尋找某隻鞋子」),藉此釐清意圖。

如何定義其他動作:

  1. actions 陣列中,為每個進入點指定動作。

    舉例來說,以下程式碼顯示了另一個定義的「購買」動作:
    • com.example.ExampleAction.BUY」的意圖名稱
    • parameters,在觸發這項意圖時剖析使用者輸入內容。如果您需要在使用者叫用動作時來自「動作」詞組的特定資料,這個方法就非常實用。
    • queryPatterns 會定義使用者需要說的內容才能觸發意圖。查詢模式可包含 Schema.org 類型,定義要剖析的參數。
    {
      "description": "Direct access",
      "name": "BUY",
      "fulfillment": {
        "conversationName": "ExampleAction"
      },
      "intent": {
        "name": "com.example.ExampleAction.BUY",
        "parameters": [
          {
            "name": "color",
            "type": "org.schema.type.Color"
          }
        ],
        "trigger": {
          "queryPatterns": [
            "find some $org.schema.type.Color:color sneakers",
            "buy some blue suede shoes",
            "get running shoes"
          ]
        }
      }
    }
          
  2. 指定與 conversations 物件中的商品對應的 conversationName,藉此指定這個意圖的執行要求。

    {
      "conversations": {
        "ExampleAction": {
          "name": "ExampleAction",
          "url": "https://www.example.com/ExampleAction"
        }
      }
    }
        

以下是完整的動作套件範例:

{
  "actions": [
    {
      "description": "Default welcome intent",
      "name": "MAIN",
      "fulfillment": {
        "conversationName": "ExampleAction"
      },
      "intent": {
        "name": "actions.intent.MAIN",
        "trigger": {
          "queryPatterns": [
            "talk to ExampleAction"
          ]
        }
      }
    },
    {
      "description": "Direct access",
      "name": "BUY",
      "fulfillment": {
        "conversationName": "ExampleAction"
      },
      "intent": {
        "name": "com.example.ExampleAction.BUY",
        "parameters": [
          {
            "name": "color",
            "type": "org.schema.type.Color"
          }
        ],
        "trigger": {
          "queryPatterns": [
            "find some $org.schema.type.Color:color sneakers",
            "buy some blue suede shoes",
            "get running shoes"
          ]
        }
      }
    }
  ],
  "conversations": {
    "ExampleAction": {
      "name": "ExampleAction",
      "url": "https://www.example.com/ExampleAction"
    }
  },
  "locale": "en"
}