use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow-cx by googleapis.
the class CreateIntentIT method testCreateIntentGlobal.
@Test
public void testCreateIntentGlobal() throws Exception {
Intent result = CreateIntent.createIntent(DISPLAY_NAME, PROJECT_ID, LOCATION_GLOBAL, AGENT_ID_GLOBAL, TRAINING_PHRASES_PARTS);
newIntentNameGlobal = result.getName();
assertEquals(result.getTrainingPhrasesCount(), TRAINING_PHRASES_PARTS.size());
for (TrainingPhrase trainingPhrase : result.getTrainingPhrasesList()) {
assertEquals(trainingPhrase.getPartsCount(), 1);
String partText = trainingPhrase.getParts(0).getText();
assertTrue(partText.equals("red") || partText.equals("blue") || partText.equals("green"));
}
}
use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow by googleapis.
the class IntentManagement method createIntent.
// [END dialogflow_list_intents]
// [START dialogflow_create_intent]
/**
* Create an intent of the given intent type
*
* @param displayName The display name of the intent.
* @param projectId Project/Agent Id.
* @param trainingPhrasesParts Training phrases.
* @param messageTexts Message texts for the agent's response when the intent is detected.
* @return The created Intent.
*/
public static Intent createIntent(String displayName, String projectId, List<String> trainingPhrasesParts, List<String> messageTexts) throws ApiException, IOException {
// Instantiates a client
try (IntentsClient intentsClient = IntentsClient.create()) {
// Set the project agent name using the projectID (my-project-id)
AgentName parent = AgentName.of(projectId);
// Build the trainingPhrases from the trainingPhrasesParts
List<TrainingPhrase> trainingPhrases = new ArrayList<>();
for (String trainingPhrase : trainingPhrasesParts) {
trainingPhrases.add(TrainingPhrase.newBuilder().addParts(Part.newBuilder().setText(trainingPhrase).build()).build());
}
// Build the message texts for the agent's response
Message message = Message.newBuilder().setText(Text.newBuilder().addAllText(messageTexts).build()).build();
// Build the intent
Intent intent = Intent.newBuilder().setDisplayName(displayName).addMessages(message).addAllTrainingPhrases(trainingPhrases).build();
// Performs the create intent request
Intent response = intentsClient.createIntent(parent, intent);
System.out.format("Intent created: %s\n", response);
return response;
}
}
use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow by googleapis.
the class IntentManagement method getIntentIds.
// [END dialogflow_delete_intent]
/**
* Helper method for testing to get intentIds from displayName.
*/
public static List<String> getIntentIds(String displayName, String projectId) throws ApiException, IOException {
List<String> intentIds = new ArrayList<>();
// Instantiates a client
try (IntentsClient intentsClient = IntentsClient.create()) {
AgentName parent = AgentName.of(projectId);
for (Intent intent : intentsClient.listIntents(parent).iterateAll()) {
if (intent.getDisplayName().equals(displayName)) {
String[] splitName = intent.getName().split("/");
intentIds.add(splitName[splitName.length - 1]);
}
}
}
return intentIds;
}
use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow by googleapis.
the class IntentManagement method deleteIntent.
// [END dialogflow_create_intent]
// [START dialogflow_delete_intent]
/**
* Delete intent with the given intent type and intent value
*
* @param intentId The id of the intent.
* @param projectId Project/Agent Id.
*/
public static void deleteIntent(String intentId, String projectId) throws ApiException, IOException {
// Instantiates a client
try (IntentsClient intentsClient = IntentsClient.create()) {
IntentName name = IntentName.of(projectId, intentId);
// Performs the delete intent request
intentsClient.deleteIntent(name);
}
}
use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow by googleapis.
the class UpdateIntent method updateIntent.
// 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);
}
}
Aggregations