使用 API 管理服務專員

通常,您會使用控制台建立及刪除代理程式。不過,在某些進階情況下,使用 API 可能會比較容易。

建立虛擬服務專員

以下範例說明如何為 Agent 類型呼叫 SetAgent 方法。這些範例是建立代理程式,但同樣的方法也可用來更新代理程式設定,例如代理程式版本。

REST

使用任何要求資料之前,請先替換以下項目:

  • PROJECT_ID:您的 Google Cloud 專案 ID

HTTP 方法和網址:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent

JSON 要求主體:

{
  "displayName": "My-display-name",
  "defaultLanguageCode": "en",
  "timeZone": "America/New_York"
}

如要傳送要求,請展開以下其中一個選項:

您應該會收到如下的 JSON 回應:

{
  "parent": "projects/PROJECT_ID",
  "displayName": "My display name",
  "defaultLanguageCode": "en",
  "timeZone": "America/New_York",
  "apiVersion": "API_VERSION_V2"
}

Java

如要向 Dialogflow 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。


import com.google.cloud.dialogflow.v2.Agent;
import com.google.cloud.dialogflow.v2.Agent.Builder;
import com.google.cloud.dialogflow.v2.AgentsClient;
import com.google.cloud.dialogflow.v2.AgentsSettings;
import java.io.IOException;

public class SetAgent {

  public static void main(String[] args) throws IOException {
    String projectId = "my-project-id";

    // The display name will set the name of your agent
    String displayName = "my-display-name";

    setAgent(projectId, displayName);
  }

  public static Agent setAgent(String parent, String displayName) throws IOException {

    AgentsSettings agentsSettings = AgentsSettings.newBuilder().build();
    try (AgentsClient client = AgentsClient.create(agentsSettings)) {
      // Set the details of the Agent to create
      Builder build = Agent.newBuilder();

      build.setDefaultLanguageCode("en");
      build.setDisplayName(displayName);

      Agent agent = build.build();

      // Make API request to create agent
      Agent response = client.setAgent(agent);
      System.out.println(response);
      return response;
    }
  }
}

Node.js

如要向 Dialogflow 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。

const {AgentsClient} = require('@google-cloud/dialogflow');

// make sure to pass projectID as the input parameter
const parent = 'projects/' + parentId + '/locations/global';

const agent = {
  parent: parent,
  displayName: displayName,
  defaultLanguageCode: 'en',
  timeZone: 'America/Los_Angeles',
};

const client = new AgentsClient();

async function setAgent() {
  const request = {
    agent,
  };

  const [response] = await client.setAgent(request);
  console.log(`response: ${JSON.stringify(response, null, 2)}`);
}
await setAgent();

Python

如要向 Dialogflow 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。

from google.cloud.dialogflow_v2 import Agent, AgentsClient, SetAgentRequest
import google.protobuf.field_mask_pb2


def set_agent(project_id, display_name):
    agents_client = AgentsClient()

    parent = agents_client.common_project_path(project_id)

    agent = Agent(
        parent=parent,
        display_name=display_name,
        default_language_code="en",
        time_zone="America/Los_Angeles",
    )

    update_mask = google.protobuf.field_mask_pb2.FieldMask()
    update_mask.FromJsonString("displayName,defaultLanguageCode,timeZone")

    request = SetAgentRequest(
        agent=agent,
        update_mask=update_mask,
    )

    return agents_client.set_agent(request=request)

刪除代理程式

以下範例說明如何為 Agent 類型呼叫 DeleteAgent 方法。

REST

使用任何要求資料之前,請先替換以下項目:

  • PROJECT_ID:您的 Google Cloud 專案 ID

HTTP 方法和網址:

DELETE https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent

如要傳送要求,請展開以下其中一個選項:

您應該會收到執行成功的狀態碼 (2xx) 和空白回應。