Search in sources :

Example 1 with AgentName

use of com.google.cloud.dialogflow.v2.AgentName 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 AgentName

use of com.google.cloud.dialogflow.v2.AgentName 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 3 with AgentName

use of com.google.cloud.dialogflow.v2.AgentName 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 4 with AgentName

use of com.google.cloud.dialogflow.v2.AgentName in project java-dialogflow by googleapis.

the class EntityTypesClientTest method listEntityTypesTest3.

@Test
public void listEntityTypesTest3() throws Exception {
    EntityType responsesElement = EntityType.newBuilder().build();
    ListEntityTypesResponse expectedResponse = ListEntityTypesResponse.newBuilder().setNextPageToken("").addAllEntityTypes(Arrays.asList(responsesElement)).build();
    mockEntityTypes.addResponse(expectedResponse);
    AgentName parent = AgentName.ofProjectName("[PROJECT]");
    String languageCode = "languageCode-2092349083";
    ListEntityTypesPagedResponse pagedListResponse = client.listEntityTypes(parent, languageCode);
    List<EntityType> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getEntityTypesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockEntityTypes.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListEntityTypesRequest actualRequest = ((ListEntityTypesRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertEquals(languageCode, actualRequest.getLanguageCode());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListEntityTypesPagedResponse(com.google.cloud.dialogflow.v2.EntityTypesClient.ListEntityTypesPagedResponse) Test(org.junit.Test)

Example 5 with AgentName

use of com.google.cloud.dialogflow.v2.AgentName in project java-dialogflow by googleapis.

the class EntityTypesClientTest method listEntityTypesTest.

@Test
public void listEntityTypesTest() throws Exception {
    EntityType responsesElement = EntityType.newBuilder().build();
    ListEntityTypesResponse expectedResponse = ListEntityTypesResponse.newBuilder().setNextPageToken("").addAllEntityTypes(Arrays.asList(responsesElement)).build();
    mockEntityTypes.addResponse(expectedResponse);
    AgentName parent = AgentName.ofProjectName("[PROJECT]");
    ListEntityTypesPagedResponse pagedListResponse = client.listEntityTypes(parent);
    List<EntityType> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getEntityTypesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockEntityTypes.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListEntityTypesRequest actualRequest = ((ListEntityTypesRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListEntityTypesPagedResponse(com.google.cloud.dialogflow.v2.EntityTypesClient.ListEntityTypesPagedResponse) Test(org.junit.Test)

Aggregations

AbstractMessage (com.google.protobuf.AbstractMessage)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 AgentName (com.google.cloud.dialogflow.v2.AgentName)3 Intent (com.google.cloud.dialogflow.v2.Intent)3 IntentsClient (com.google.cloud.dialogflow.v2.IntentsClient)3 AgentName (com.google.cloud.dialogflow.cx.v3beta1.AgentName)2 ListEntityTypesPagedResponse (com.google.cloud.dialogflow.v2.EntityTypesClient.ListEntityTypesPagedResponse)2 ListIntentsPagedResponse (com.google.cloud.dialogflow.v2.IntentsClient.ListIntentsPagedResponse)2 EventHandler (com.google.cloud.dialogflow.cx.v3beta1.EventHandler)1 Flow (com.google.cloud.dialogflow.cx.v3beta1.Flow)1 FlowsClient (com.google.cloud.dialogflow.cx.v3beta1.FlowsClient)1 FlowsSettings (com.google.cloud.dialogflow.cx.v3beta1.FlowsSettings)1 Intent (com.google.cloud.dialogflow.cx.v3beta1.Intent)1 TrainingPhrase (com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase)1 IntentsClient (com.google.cloud.dialogflow.cx.v3beta1.IntentsClient)1 IntentsSettings (com.google.cloud.dialogflow.cx.v3beta1.IntentsSettings)1 Context (com.google.cloud.dialogflow.v2.Context)1 ListEnvironmentsPagedResponse (com.google.cloud.dialogflow.v2.EnvironmentsClient.ListEnvironmentsPagedResponse)1 Message (com.google.cloud.dialogflow.v2.Intent.Message)1