使用 API 管理代理

通常,您可以使用控制台创建和删除代理。 不过,在某些高级场景中 您可能会发现使用 API 更轻松

创建代理

以下示例展示了如何为 Agent 类型调用 Create 方法。

为代理参考选择协议和版本

协议 V3 V3beta1
REST 代理资源 代理资源
RPC 代理接口 代理接口
C++ AgentsClient 不可用
C# AgentsClient 不可用
Go AgentsClient 不可用
Java AgentsClient AgentsClient
Node.js AgentsClient AgentsClient
PHP 不可用 不可用
Python AgentsClient AgentsClient
Ruby 不可用 不可用

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 Google Cloud 项目 ID
  • REGION_ID:您的区域 ID

HTTP 方法和网址:

POST https://REGION_ID-dialogflow.googleapis.com/v3/projects/PROJECT_ID/locations/REGION_ID/agents

请求 JSON 正文:

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

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "name": "projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID",
  "displayName": "My display name",
  "defaultLanguageCode": "en",
  "timeZone": "America/New_York",
  "startFlow": "projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID/flows/00000000-0000-0000-0000-000000000000",
  "advancedSettings": {
    "loggingSettings": {}
  }
}

Java

如需向 Dialogflow 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


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

public class CreateAgent {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String displayName = "my-display-name";

    createAgent(projectId, displayName);
  }

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

    String apiEndpoint = "global-dialogflow.googleapis.com:443";

    AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
    // Note: close() needs to be called on the AgentsClient object to clean up resources
    // such as threads. In the example below, try-with-resources is used,
    // which automatically calls close().
    try (AgentsClient client = AgentsClient.create(agentsSettings)) {
      // Set the details of the Agent to create
      Builder build = Agent.newBuilder();

      build.setDefaultLanguageCode("en");
      build.setDisplayName(displayName);
      // Correct format for timezone is location/city
      // For example America/Los_Angeles, Europe/Madrid, Asia/Tokyo
      build.setTimeZone("America/Los_Angeles");

      Agent agent = build.build();
      String parentPath = String.format("projects/%s/locations/%s", parent, "global");

      // Calls the create agent api and returns the created Agent
      Agent response = client.createAgent(parentPath, agent);
      System.out.println(response);
      return response;
    }
  }
}

Node.js

如需向 Dialogflow 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


const parent = 'projects/' + projectId + '/locations/global';

const api_endpoint = 'global-dialogflow.googleapis.com';

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

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

const client = new AgentsClient({apiEndpoint: api_endpoint});

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

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

  // Delete created agent resource
  client.deleteAgent({name: response.name});
}
await setAgentSample();

Python

如需向 Dialogflow 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
from google.cloud.dialogflowcx_v3.types.agent import Agent


def create_agent(project_id, display_name):
    parent = "projects/" + project_id + "/locations/global"

    agents_client = AgentsClient()

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

    response = agents_client.create_agent(request={"agent": agent, "parent": parent})

    return response

删除代理

以下示例展示了如何为 Agent 类型调用 Delete 方法。

为代理参考选择协议和版本

协议 V3 V3beta1
REST 代理资源 代理资源
RPC 代理接口 代理接口
C++ AgentsClient 不可用
C# AgentsClient 不可用
Go AgentsClient 不可用
Java AgentsClient AgentsClient
Node.js AgentsClient AgentsClient
PHP 不可用 不可用
Python AgentsClient AgentsClient
Ruby 不可用 不可用

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 Google Cloud 项目 ID
  • REGION_ID:您的区域 ID
  • AGENT_ID:您的代理 ID,可在代理创建响应中找到

HTTP 方法和网址:

DELETE https://REGION_ID-dialogflow.googleapis.com/v3/projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID

如需发送您的请求,请展开以下选项之一:

您应该会收到一个成功的状态代码 (2xx) 和一个空响应。