עדכון נתונים באמצעות FieldMask

כשמעדכנים נתוני סוכן באמצעות ה-API, אפשר לבחור להחליף את כל סוג הנתונים או להחליף רק שדות ספציפיים בסוג הנתונים. בדרך כלל עדיף להחליף שדות ספציפיים, כדי למנוע החלפה של כל הנתונים בטעות. כדי להחליף שדות ספציפיים, צריך לספק FieldMask לבקשת העדכון.

בדוגמאות הבאות אפשר לראות איך מספקים FieldMask כדי לעדכן את השם המוצג של סוג Intents.

REST

מזינים את פרמטר השאילתה של כתובת ה-URL לשיטה patch.updateMask לדוגמה:

?updateMask=displayName

Java

כדי לבצע אימות ב-Dialogflow CX, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.dialogflow.v2.Intent;
import com.google.cloud.dialogflow.v2.Intent.Builder;
import com.google.cloud.dialogflow.v2.IntentsClient;
import com.google.cloud.dialogflow.v2.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 intentId = "my-intent-id";
    String location = "my-location";
    String displayName = "my-display-name";
    updateIntent(projectId, intentId, location, displayName);
  }

  // DialogFlow API Update Intent sample.
  public static void updateIntent(
      String projectId, String intentId, String location, String displayName) throws IOException {
    try (IntentsClient client = IntentsClient.create()) {
      String intentPath =
          "projects/" + projectId + "/locations/" + location + "/agent/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

כדי לבצע אימות ב-Dialogflow CX, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

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

const intentClient = new IntentsClient();

const agentPath = intentClient.projectAgentPath(projectId);
const intentPath = agentPath + '/intents/' + intentId;

const intent = await intentClient.getIntent({name: intentPath});
intent[0].displayName = displayName;
const updateMask = {
  paths: ['display_name'],
};

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

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

Python

כדי לבצע אימות ב-Dialogflow CX, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.


from google.cloud.dialogflow_v2 import IntentsClient
from google.protobuf import field_mask_pb2


def update_intent(project_id, intent_id, display_name):
    intents_client = IntentsClient()

    intent_name = intents_client.intent_path(project_id, intent_id)
    intent = intents_client.get_intent(request={"name": intent_name})

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