Search in sources :

Example 1 with IntentsClient

use of com.google.cloud.dialogflow.cx.v3beta1.IntentsClient in project java-dialogflow-cx by googleapis.

the class CreateIntent method createIntent.

// Create an intent of the given intent type.
public static Intent createIntent(String displayName, String projectId, String locationId, String agentId, List<String> trainingPhrasesParts) throws IOException, ApiException {
    IntentsSettings.Builder intentsSettingsBuilder = IntentsSettings.newBuilder();
    if (locationId.equals("global")) {
        intentsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
    } else {
        intentsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
    }
    IntentsSettings intentsSettings = intentsSettingsBuilder.build();
    // Instantiates a client
    try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
        // Set the project agent name using the projectID (my-project-id), locationID (global), and
        // agentID (UUID).
        AgentName parent = AgentName.of(projectId, locationId, agentId);
        // 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()).setRepeatCount(1).build());
        }
        // Build the intent.
        Intent intent = Intent.newBuilder().setDisplayName(displayName).addAllTrainingPhrases(trainingPhrases).build();
        // Performs the create intent request.
        Intent response = intentsClient.createIntent(parent, intent);
        return response;
    }
}
Also used : IntentsSettings(com.google.cloud.dialogflow.cx.v3beta1.IntentsSettings) TrainingPhrase(com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase) IntentsClient(com.google.cloud.dialogflow.cx.v3beta1.IntentsClient) ArrayList(java.util.ArrayList) Intent(com.google.cloud.dialogflow.cx.v3beta1.Intent) AgentName(com.google.cloud.dialogflow.cx.v3beta1.AgentName)

Example 2 with IntentsClient

use of com.google.cloud.dialogflow.cx.v3beta1.IntentsClient in project java-dialogflow-cx by googleapis.

the class ListTrainingPhrases method listTrainingPhrases.

// DialogFlow API List Training Phrases sample.
public static void listTrainingPhrases(String projectId, String location, String agentId, String intentId) throws IOException {
    try (IntentsClient client = IntentsClient.create()) {
        // Set the intent name
        IntentName name = IntentName.of(projectId, location, agentId, intentId);
        // Compose the get-intent request
        GetIntentRequest request = GetIntentRequest.newBuilder().setName(name.toString()).build();
        // Make API request to get intent
        Intent response = client.getIntent(request);
        // Loop through the results
        for (Intent.TrainingPhrase phrase : response.getTrainingPhrasesList()) {
            System.out.println("***********************************************");
            List<Intent.TrainingPhrase.Part> parts = phrase.getPartsList();
            for (Intent.TrainingPhrase.Part part : parts) {
                System.out.println(String.format("Training Phrase: %s", part.getText()));
            }
        }
    }
}
Also used : IntentsClient(com.google.cloud.dialogflow.cx.v3.IntentsClient) IntentName(com.google.cloud.dialogflow.cx.v3.IntentName) GetIntentRequest(com.google.cloud.dialogflow.cx.v3.GetIntentRequest) Intent(com.google.cloud.dialogflow.cx.v3.Intent)

Example 3 with IntentsClient

use of com.google.cloud.dialogflow.cx.v3beta1.IntentsClient 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;
    }
}
Also used : TrainingPhrase(com.google.cloud.dialogflow.v2.Intent.TrainingPhrase) IntentsClient(com.google.cloud.dialogflow.v2.IntentsClient) Message(com.google.cloud.dialogflow.v2.Intent.Message) ArrayList(java.util.ArrayList) Intent(com.google.cloud.dialogflow.v2.Intent) AgentName(com.google.cloud.dialogflow.v2.AgentName)

Example 4 with IntentsClient

use of com.google.cloud.dialogflow.cx.v3beta1.IntentsClient 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;
}
Also used : IntentsClient(com.google.cloud.dialogflow.v2.IntentsClient) ArrayList(java.util.ArrayList) Intent(com.google.cloud.dialogflow.v2.Intent) AgentName(com.google.cloud.dialogflow.v2.AgentName)

Example 5 with IntentsClient

use of com.google.cloud.dialogflow.cx.v3beta1.IntentsClient 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);
    }
}
Also used : IntentsClient(com.google.cloud.dialogflow.v2.IntentsClient) IntentName(com.google.cloud.dialogflow.v2.IntentName)

Aggregations

IntentsClient (com.google.cloud.dialogflow.v2.IntentsClient)8 Intent (com.google.cloud.dialogflow.v2.Intent)6 Intent (com.google.cloud.dialogflow.cx.v3.Intent)3 IntentsClient (com.google.cloud.dialogflow.cx.v3.IntentsClient)3 AgentName (com.google.cloud.dialogflow.v2.AgentName)3 ArrayList (java.util.ArrayList)3 Intent (com.google.cloud.dialogflow.cx.v3beta1.Intent)2 IntentName (com.google.cloud.dialogflow.v2.IntentName)2 FieldMask (com.google.protobuf.FieldMask)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 Before (org.junit.Before)2 Agent (com.google.cloud.dialogflow.cx.v3.Agent)1 Builder (com.google.cloud.dialogflow.cx.v3.Agent.Builder)1 AgentsClient (com.google.cloud.dialogflow.cx.v3.AgentsClient)1 AgentsSettings (com.google.cloud.dialogflow.cx.v3.AgentsSettings)1 GetIntentRequest (com.google.cloud.dialogflow.cx.v3.GetIntentRequest)1 Builder (com.google.cloud.dialogflow.cx.v3.Intent.Builder)1 IntentName (com.google.cloud.dialogflow.cx.v3.IntentName)1 UpdateIntentRequest (com.google.cloud.dialogflow.cx.v3.UpdateIntentRequest)1