使用 FieldMask 更新数据

使用 API 更新代理数据时,您可以选择覆盖整个数据类型或仅覆盖数据类型的特定字段。通常情况下,最好覆盖特定字段,以免因意外覆盖所有数据。如要覆盖特定字段,请为更新请求提供 FieldMask

以下示例展示了如何提供 FieldMask 以更新 Intent 类型的显示名。

为意图参考选择协议和版本

协议 V3 V3beta1
REST 意图资源 意图资源
RPC 意图界面 意图界面
C# 不可用 不可用
Go 不可用 不可用
Java IntentsClient IntentsClient
Node.js IntentsClient IntentsClient
PHP 不可用 不可用
Python IntentsClient IntentsClient
Ruby 不可用 不可用

REST 和命令行

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

  • PROJECT_ID:您的 GCP 项目 ID
  • AGENT_ID:您的代理 ID
  • REGION_ID:您的区域 ID
  • INTENT_ID:您的意图 ID
  • DISPLAY_NAME:所需的显示名

HTTP 方法和网址:

PATCH https://REGION_ID-dialogflow.googleapis.com/v3/projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID/intents/INTENT_ID?updateMask=displayName

请求 JSON 正文:

{
  "displayName": "DISPLAY_NAME"
}

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

您应会收到如下所示的 JSON 响应:

{
  "name": "projects/your-project-id/locations/us-central1/agents/c2a370ca-158a-4296-a211-d5ab6bb84324/intents/91aa7827-d69b-4077-8b21-7a66b8b59f16",
  "displayName": "your-display-name",
  ...
}

Java


import com.google.cloud.dialogflow.cx.v3.Intent;
import com.google.cloud.dialogflow.cx.v3.Intent.Builder;
import com.google.cloud.dialogflow.cx.v3.IntentsClient;
import com.google.cloud.dialogflow.cx.v3.UpdateIntentRequest;
import com.google.protobuf.FieldMask;
import java.io.IOException;

public class UpdateIntent {

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

  // DialogFlow API Update Intent sample.
  public static void updateIntent(
      String projectId, String agentId, String intentId, String location, String displayName)
      throws IOException {
    try (IntentsClient client = IntentsClient.create()) {
      String intentPath =
          "projects/"
              + projectId
              + "/locations/"
              + location
              + "/agents/"
              + agentId
              + "/intents/"
              + intentId;

      Builder intentBuilder = client.getIntent(intentPath).toBuilder();

      intentBuilder.setDisplayName(displayName);
      FieldMask fieldMask = FieldMask.newBuilder().addPaths("display_name").build();

      Intent intent = intentBuilder.build();
      UpdateIntentRequest request =
          UpdateIntentRequest.newBuilder()
              .setIntent(intent)
              .setLanguageCode("en")
              .setUpdateMask(fieldMask)
              .build();

      // Make API request to update intent using fieldmask
      Intent response = client.updateIntent(request);
      System.out.println(response);
    }
  }
}

Node.js


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

const intentClient = new IntentsClient();

//TODO(developer): Uncomment these variables before running the sample.
//  const projectId = 'your-project-id';
//  const agentId = 'your-agent-id';
//  const intentId = 'your-intent-id';
//  const location = 'your-location';
//  const displayName = 'your-display-name';

async function updateIntent() {
  const agentPath = intentClient.projectPath(projectId);
  const intentPath = `${agentPath}/locations/${location}/agents/${agentId}/intents/${intentId}`;

  //Gets the intent from intentPath
  const intent = await intentClient.getIntent({name: intentPath});
  intent[0].displayName = displayName;

  //Specifies what is being updated
  const updateMask = {
    paths: ['display_name'],
  };

  const updateIntentRequest = {
    intent: intent[0],
    updateMask,
    languageCode: 'en',
  };

  //Send the request for update the intent.
  const result = await intentClient.updateIntent(updateIntentRequest);
  console.log(result);
}

updateIntent();

Python

from google.cloud.dialogflowcx_v3.services.intents import IntentsClient
from google.protobuf import field_mask_pb2

def update_intent(project_id, agent_id, intent_id, location, displayName):

    intents_client = IntentsClient()

    intent_name = intents_client.intent_path(project_id, location, agent_id, intent_id)

    intent = intents_client.get_intent(request={"name": intent_name})

    intent.display_name = displayName
    update_mask = field_mask_pb2.FieldMask(paths=["display_name"])
    response = intents_client.update_intent(intent=intent, update_mask=update_mask)
    return response