importcom.google.cloud.dialogflow.v2.Intent;importcom.google.cloud.dialogflow.v2.Intent.Builder;importcom.google.cloud.dialogflow.v2.IntentsClient;importcom.google.cloud.dialogflow.v2.UpdateIntentRequest;importcom.google.protobuf.FieldMask;importjava.io.IOException;publicclassUpdateIntent{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringintentId="my-intent-id";Stringlocation="my-location";StringdisplayName="my-display-name";updateIntent(projectId,intentId,location,displayName);}// DialogFlow API Update Intent sample.publicstaticvoidupdateIntent(StringprojectId,StringintentId,Stringlocation,StringdisplayName)throwsIOException{try(IntentsClientclient=IntentsClient.create()){StringintentPath="projects/"+projectId+"/locations/"+location+"/agent/intents/"+intentId;BuilderintentBuilder=client.getIntent(intentPath).toBuilder();intentBuilder.setDisplayName(displayName);FieldMaskfieldMask=FieldMask.newBuilder().addPaths("display_name").build();Intentintent=intentBuilder.build();UpdateIntentRequestrequest=UpdateIntentRequest.newBuilder().setIntent(intent).setLanguageCode("en").setUpdateMask(fieldMask).build();// Make API request to update intent using fieldmaskIntentresponse=client.updateIntent(request);System.out.println(response);}}}
const{IntentsClient}=require('@google-cloud/dialogflow');constintentClient=newIntentsClient();constagentPath=intentClient.projectAgentPath(projectId);constintentPath=agentPath+'/intents/'+intentId;constintent=awaitintentClient.getIntent({name:intentPath});intent[0].displayName=displayName;constupdateMask={paths:['display_name'],};constupdateIntentRequest={intent:intent[0],updateMask:updateMask,languageCode:'en',};//Send the request for update the intent.constresult=awaitintentClient.updateIntent(updateIntentRequest);console.log(result);
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eWhen updating agent data via the API, you can choose to overwrite either the entire data type or specific fields within it.\u003c/p\u003e\n"],["\u003cp\u003eOverwriting specific fields using a \u003ccode\u003eFieldMask\u003c/code\u003e is generally recommended to avoid accidental data loss.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eupdateMask\u003c/code\u003e parameter, supplied either as a URL query parameter in REST or through a dedicated object in the code, specifies which fields to update.\u003c/p\u003e\n"],["\u003cp\u003eThe provided examples demonstrate how to use \u003ccode\u003eFieldMask\u003c/code\u003e to update the display name of an Intent for REST, Java, Node.js, and Python.\u003c/p\u003e\n"],["\u003cp\u003eApplication Default Credentials are required for authentication to Dialogflow, with instructions available for local development environments.\u003c/p\u003e\n"]]],[],null,["# Update data with a FieldMask\n\nWhen updating agent data with the API,\nyou can choose to overwrite the entire data type\nor to overwrite only specific fields of the data type.\nIt is usually best to overwrite specific fields,\nso you avoid accidentally overwriting all of your data.\nTo overwrite specific fields,\nsupply a\n[`FieldMask`](https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#fieldmask)\nto your update request.\n\nThe following examples show how to supply a `FieldMask`\nto update the display name for an\n[Intents](/dialogflow/docs/reference/common-types#intents)\ntype. \n\n### REST\n\n\nSupply the `updateMask`\nURL query parameter for the `patch` method.\nFor example: \n\n```\n?updateMask=displayName\n```\n\n### Java\n\n\nTo authenticate to Dialogflow, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.dialogflow.v2.https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.Intent.html;\n import com.google.cloud.dialogflow.v2.https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.Intent.html.Builder;\n import com.google.cloud.dialogflow.v2.https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.IntentsClient.html;\n import com.google.cloud.dialogflow.v2.https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.UpdateIntentRequest.html;\n import com.google.protobuf.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.html;\n import java.io.IOException;\n\n public class UpdateIntent {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"my-project-id\";\n String intentId = \"my-intent-id\";\n String location = \"my-location\";\n String displayName = \"my-display-name\";\n updateIntent(projectId, intentId, location, displayName);\n }\n\n // DialogFlow API Update Intent sample.\n public static void updateIntent(\n String projectId, String intentId, String location, String displayName) throws IOException {\n try (https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.IntentsClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.IntentsClient.html.create()) {\n String intentPath =\n \"projects/\" + projectId + \"/locations/\" + location + \"/agent/intents/\" + intentId;\n\n Builder intentBuilder = client.getIntent(intentPath).toBuilder();\n\n intentBuilder.setDisplayName(displayName);\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.html fieldMask = https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.html.newBuilder().https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.Builder.html#com_google_protobuf_FieldMask_Builder_addPaths_java_lang_String_(\"display_name\").build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.Intent.html intent = intentBuilder.build();\n https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.UpdateIntentRequest.html request =\n https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.UpdateIntentRequest.html.newBuilder()\n .setIntent(intent)\n .setLanguageCode(\"en\")\n .setUpdateMask(fieldMask)\n .build();\n\n // Make API request to update intent using fieldmask\n https://cloud.google.com/java/docs/reference/google-cloud-dialogflow/latest/com.google.cloud.dialogflow.v2.Intent.html response = client.updateIntent(request);\n System.out.println(response);\n }\n }\n }\n\n### Node.js\n\n\nTo authenticate to Dialogflow, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const {IntentsClient} = require('https://cloud.google.com/nodejs/docs/reference/dialogflow/latest/overview.html');\n\n const intentClient = new https://cloud.google.com/nodejs/docs/reference/dialogflow-cx/latest/overview.html();\n\n const agentPath = intentClient.projectAgentPath(projectId);\n const intentPath = agentPath + '/intents/' + intentId;\n\n const intent = await intentClient.getIntent({name: intentPath});\n intent[0].displayName = displayName;\n const updateMask = {\n paths: ['display_name'],\n };\n\n const updateIntentRequest = {\n intent: intent[0],\n updateMask: updateMask,\n languageCode: 'en',\n };\n\n //Send the request for update the intent.\n const result = await intentClient.updateIntent(updateIntentRequest);\n console.log(result);\n\n### Python\n\n\nTo authenticate to Dialogflow, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n from google.cloud.dialogflow_v2 import IntentsClient\n from google.protobuf import field_mask_pb2\n\n\n def update_intent(project_id, intent_id, display_name):\n intents_client = IntentsClient()\n\n intent_name = intents_client.https://cloud.google.com/python/docs/reference/dialogflow/latest/google.cloud.dialogflow_v2.services.intents.IntentsClient.html#google_cloud_dialogflow_v2_services_intents_IntentsClient_intent_path(project_id, intent_id)\n intent = intents_client.https://cloud.google.com/python/docs/reference/dialogflow/latest/google.cloud.dialogflow_v2.services.intents.IntentsClient.html#google_cloud_dialogflow_v2_services_intents_IntentsClient_get_intent(request={\"name\": intent_name})\n\n intent.display_name = display_name\n update_mask = field_mask_pb2.FieldMask(paths=[\"display_name\"])\n response = intents_client.https://cloud.google.com/python/docs/reference/dialogflow/latest/google.cloud.dialogflow_v2.services.intents.IntentsClient.html#google_cloud_dialogflow_v2_services_intents_IntentsClient_update_intent(intent=intent, update_mask=update_mask)\n return response\n\n\u003cbr /\u003e"]]