Atualizar dados com um FieldMask

Ao atualizar os dados do agente com a API, você pode optar por substituir o tipo de dados inteiro ou substituir apenas campos específicos do tipo de dados. Em geral, é melhor substituir campos específicos, para evitar substituir acidentalmente todos os seus dados. Para substituir campos específicos, forneça um FieldMask à sua solicitação de atualização.

Os exemplos a seguir mostram como fornecer um FieldMask para atualizar o nome de exibição em um tipo Intent.

Selecione um protocolo e uma versão para a referência de intent:

Protocolo V3 V3beta1
REST Recurso de intent Recurso de intent
RPC Interface da intent Interface da intent
C++ IntentsClient Indisponível
C# IntentsClient Indisponível
Go IntentsClient Indisponível
Java IntentsClient IntentsClient
Node.js IntentsClient IntentsClient
PHP Indisponível Indisponível
Python IntentsClient IntentsClient
Ruby Indisponível Indisponível

REST

Antes de usar os dados da solicitação, faça as substituições a seguir:

  • PROJECT_ID: o ID do projeto do Google Cloud
  • AGENT_ID: seu ID de agente
  • REGION_ID: o ID da sua região
  • INTENT_ID: código da sua intent
  • DISPLAY_NAME: o nome de exibição desejado

Método HTTP e URL:

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

Corpo JSON da solicitação:

{
  "displayName": "DISPLAY_NAME"
}

Para enviar a solicitação, expanda uma destas opções:

Você receberá uma resposta JSON semelhante a esta:

{
  "name": "projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID/intents/INTENT_ID",
  "displayName": "DISPLAY_NAME",
  ...
}

Java

Para autenticar no Dialogflow, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.


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 {

    // Note: close() needs to be called on the IntentsClient object to clean up resources
    // such as threads. In the example below, try-with-resources is used,
    // which automatically calls close().
    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

Para autenticar no Dialogflow, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.


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

Para autenticar no Dialogflow, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.

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