Search in sources :

Example 11 with AgentName

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

the class CreateFlow method createFlow.

// Create a flow in the specified agent.
public static Flow createFlow(String displayName, String projectId, String locationId, String agentId, Map<String, String> eventsToFulfillmentMessages) throws IOException, ApiException {
    FlowsSettings.Builder flowsSettingsBuilder = FlowsSettings.newBuilder();
    if (locationId.equals("global")) {
        flowsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
    } else {
        flowsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
    }
    FlowsSettings flowsSettings = flowsSettingsBuilder.build();
    // Instantiates a client
    try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) {
        // 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 EventHandlers for the flow using the mapping from events to fulfillment messages.
        List<EventHandler> eventHandlers = new ArrayList<>();
        for (Map.Entry<String, String> item : eventsToFulfillmentMessages.entrySet()) {
            eventHandlers.add(EventHandler.newBuilder().setEvent(// Event (sys.no-match-default)
            item.getKey()).setTriggerFulfillment(Fulfillment.newBuilder().addMessages(ResponseMessage.newBuilder().setText(Text.newBuilder().addText(item.getValue()).build()).build()).build()).build());
        }
        // Build the flow.
        Flow flow = Flow.newBuilder().setDisplayName(displayName).addAllEventHandlers(eventHandlers).build();
        // Performs the create flow request.
        Flow response = flowsClient.createFlow(parent, flow);
        // TODO : Uncomment if you want to print response
        // System.out.format("Flow created: %s\n", response.toString());
        flowsClient.shutdown();
        return response;
    }
}
Also used : FlowsClient(com.google.cloud.dialogflow.cx.v3beta1.FlowsClient) FlowsSettings(com.google.cloud.dialogflow.cx.v3beta1.FlowsSettings) ArrayList(java.util.ArrayList) EventHandler(com.google.cloud.dialogflow.cx.v3beta1.EventHandler) AgentName(com.google.cloud.dialogflow.cx.v3beta1.AgentName) Map(java.util.Map) Flow(com.google.cloud.dialogflow.cx.v3beta1.Flow)

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