Skip to content

Commit

Permalink
samples: add sample code to query regional Dialogflow CX agent (#100)
Browse files Browse the repository at this point in the history
* samples: add sample code to query regional Dialogflow CX agent

Add logic to query regional Dialogflow CX endpoint for the following methods:
- CreateFlow
- CreateIntent
- CreatePage
- DetectIntent
- DetectIntentStream

* Fix: use equals instead of == for string comparison
  • Loading branch information
jessiexjiang27 committed Dec 10, 2020
1 parent 092858e commit 71bb6bb
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.dialogflow.cx.v3beta1.EventHandler;
import com.google.cloud.dialogflow.cx.v3beta1.Flow;
import com.google.cloud.dialogflow.cx.v3beta1.FlowsClient;
import com.google.cloud.dialogflow.cx.v3beta1.FlowsSettings;
import com.google.cloud.dialogflow.cx.v3beta1.Fulfillment;
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage;
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage.Text;
Expand All @@ -41,8 +42,16 @@ public static Flow createFlow(
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()) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase;
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase.Part;
import com.google.cloud.dialogflow.cx.v3beta1.IntentsClient;
import com.google.cloud.dialogflow.cx.v3beta1.IntentsSettings;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -38,8 +39,16 @@ public static Intent createIntent(
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()) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.dialogflow.cx.v3beta1.Fulfillment;
import com.google.cloud.dialogflow.cx.v3beta1.Page;
import com.google.cloud.dialogflow.cx.v3beta1.PagesClient;
import com.google.cloud.dialogflow.cx.v3beta1.PagesSettings;
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage;
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage.Text;
import java.io.IOException;
Expand All @@ -42,8 +43,16 @@ public static Page createPage(
String flowId,
List<String> entryTexts)
throws IOException, ApiException {
PagesSettings.Builder pagesSettingsBuilder = PagesSettings.newBuilder();
if (locationId.equals("global")) {
pagesSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
} else {
pagesSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
}
PagesSettings pagesSettings = pagesSettingsBuilder.build();

// Instantiates a client
try (PagesClient pagesClient = PagesClient.create()) {
try (PagesClient pagesClient = PagesClient.create(pagesSettings)) {
// Set the flow name using the projectID (my-project-id), locationID (global), agentID (UUID)
// and flowID (UUID).
FlowName parent = FlowName.of(projectId, locationId, agentId, flowId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.cloud.dialogflow.cx.v3beta1.QueryResult;
import com.google.cloud.dialogflow.cx.v3beta1.SessionName;
import com.google.cloud.dialogflow.cx.v3beta1.SessionsClient;
import com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings;
import com.google.cloud.dialogflow.cx.v3beta1.TextInput;
import com.google.common.collect.Maps;
import java.io.IOException;
Expand All @@ -42,9 +43,17 @@ public static Map<String, QueryResult> detectIntent(
List<String> texts,
String languageCode)
throws IOException, ApiException {
SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
if (locationId.equals("global")) {
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
} else {
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
}
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();

Map<String, QueryResult> queryResults = Maps.newHashMap();
// Instantiates a client
try (SessionsClient sessionsClient = SessionsClient.create()) {
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
// Set the session name using the projectID (my-project-id), locationID (global), agentID
// (UUID), and sessionId (UUID).
SessionName session = SessionName.of(projectId, locationId, agentId, sessionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.cloud.dialogflow.cx.v3beta1.QueryResult;
import com.google.cloud.dialogflow.cx.v3beta1.SessionName;
import com.google.cloud.dialogflow.cx.v3beta1.SessionsClient;
import com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings;
import com.google.cloud.dialogflow.cx.v3beta1.StreamingDetectIntentRequest;
import com.google.cloud.dialogflow.cx.v3beta1.StreamingDetectIntentResponse;
import com.google.protobuf.ByteString;
Expand All @@ -39,8 +40,16 @@ public class DetectIntentStream {
public static void detectIntentStream(
String projectId, String locationId, String agentId, String sessionId, String audioFilePath)
throws ApiException, IOException {
SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
if (locationId.equals("global")) {
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
} else {
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
}
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();

// Instantiates a client
try (SessionsClient sessionsClient = SessionsClient.create()) {
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
// Set the session name using the projectID (my-project-id), locationID (global), agentID
// (UUID), and sessionId (UUID).
// Using the same `sessionId` between requests allows continuation of the conversation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@

import com.google.cloud.dialogflow.cx.v3beta1.Flow;
import com.google.cloud.dialogflow.cx.v3beta1.FlowsClient;
import com.google.cloud.dialogflow.cx.v3beta1.FlowsSettings;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.UUID;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -35,30 +36,67 @@ public class CreateFlowIT {

private static String DISPLAY_NAME = "flow-" + UUID.randomUUID().toString();
private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static String LOCATION = "global";
private static String AGENT_ID =
private static String LOCATION_GLOBAL = "global";
private static String LOCATION_REGIONAL = "us-central1";
private static String AGENT_ID_GLOBAL =
System.getenv()
.getOrDefault("DIALOGFLOW_CX_AGENT_ID", "b8d0e85d-0741-4e6d-a66a-3671184b7b93");
.getOrDefault("DIALOGFLOW_CX_AGENT_ID_GLOBAL", "b8d0e85d-0741-4e6d-a66a-3671184b7b93");
private static String AGENT_ID_REGIONAL =
System.getenv()
.getOrDefault("DIALOGFLOW_CX_AGENT_ID_REGIONAL", "1ea2bf10-d5ef-4442-b93f-a917d1991014");
private static Map<String, String> EVENT_TO_FULFILLMENT_MESSAGES =
ImmutableMap.of("event-1", "message-1", "event-2", "message-2");
private static String newFlowName;

@After
public void tearDown() throws Exception {
// Delete the newly created Flow.
if (newFlowName != null) {
private static String newFlowNameGlobal;
private static String newFlowNameRegional;

@AfterClass
public static void tearDown() throws Exception {
// Delete the newly created Flow in the global location.
if (newFlowNameGlobal != null) {
try (FlowsClient flowsClient = FlowsClient.create()) {
flowsClient.deleteFlow(newFlowName);
flowsClient.deleteFlow(newFlowNameGlobal);
}
}

// Delete the newly created Flow in the regional location.
if (newFlowNameRegional != null) {
FlowsSettings flowsSettings =
FlowsSettings.newBuilder()
.setEndpoint(LOCATION_REGIONAL + "-dialogflow.googleapis.com:443")
.build();
try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) {
flowsClient.deleteFlow(newFlowNameRegional);
}
}
}

@Test
public void testCreateFlowGlobal() throws Exception {
Flow result =
CreateFlow.createFlow(
DISPLAY_NAME,
PROJECT_ID,
LOCATION_GLOBAL,
AGENT_ID_GLOBAL,
EVENT_TO_FULFILLMENT_MESSAGES);
newFlowNameGlobal = result.getName();

assertEquals(result.getDisplayName(), DISPLAY_NAME);
// Number of added event handlers + 2 default event handlers.
assertEquals(result.getEventHandlersCount(), EVENT_TO_FULFILLMENT_MESSAGES.size() + 2);
}

@Test
public void testCreateFlow() throws Exception {
public void testCreateFlowRegional() throws Exception {
Flow result =
CreateFlow.createFlow(
DISPLAY_NAME, PROJECT_ID, LOCATION, AGENT_ID, EVENT_TO_FULFILLMENT_MESSAGES);
newFlowName = result.getName();
DISPLAY_NAME,
PROJECT_ID,
LOCATION_REGIONAL,
AGENT_ID_REGIONAL,
EVENT_TO_FULFILLMENT_MESSAGES);
newFlowNameRegional = result.getName();

assertEquals(result.getDisplayName(), DISPLAY_NAME);
// Number of added event handlers + 2 default event handlers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import com.google.cloud.dialogflow.cx.v3beta1.Intent;
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase;
import com.google.cloud.dialogflow.cx.v3beta1.IntentsClient;
import com.google.cloud.dialogflow.cx.v3beta1.IntentsSettings;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -37,29 +38,62 @@ public class CreateIntentIT {

private static String DISPLAY_NAME = "intent-" + UUID.randomUUID().toString();
private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static String LOCATION = "global";
private static String AGENT_ID =
private static String LOCATION_GLOBAL = "global";
private static String LOCATION_REGIONAL = "us-central1";
private static String AGENT_ID_GLOBAL =
System.getenv()
.getOrDefault("DIALOGFLOW_CX_AGENT_ID", "b8d0e85d-0741-4e6d-a66a-3671184b7b93");
.getOrDefault("DIALOGFLOW_CX_AGENT_ID_GLOBAL", "b8d0e85d-0741-4e6d-a66a-3671184b7b93");
private static String AGENT_ID_REGIONAL =
System.getenv()
.getOrDefault("DIALOGFLOW_CX_AGENT_ID_REGIONAL", "1ea2bf10-d5ef-4442-b93f-a917d1991014");
private static List<String> TRAINING_PHRASES_PARTS = Arrays.asList("red", "blue", "green");
private static String newIntentName;

@After
public void tearDown() throws Exception {
// Delete the newly created Intent.
if (newIntentName != null) {
private static String newIntentNameGlobal;
private static String newIntentNameRegional;

@AfterClass
public static void tearDown() throws Exception {
// Delete the newly created Intent in the global location.
if (newIntentNameGlobal != null) {
try (IntentsClient intentsClient = IntentsClient.create()) {
intentsClient.deleteIntent(newIntentName);
intentsClient.deleteIntent(newIntentNameGlobal);
}
}

// Delete the newly created Intent in the regional location.
if (newIntentNameRegional != null) {
IntentsSettings intentsSettings =
IntentsSettings.newBuilder()
.setEndpoint(LOCATION_REGIONAL + "-dialogflow.googleapis.com:443")
.build();
try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
intentsClient.deleteIntent(newIntentNameRegional);
}
}
}

@Test
public void testCreateIntent() throws Exception {
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"));
}
}

@Test
public void testCreateIntentRegional() throws Exception {
Intent result =
CreateIntent.createIntent(
DISPLAY_NAME, PROJECT_ID, LOCATION, AGENT_ID, TRAINING_PHRASES_PARTS);
newIntentName = result.getName();
DISPLAY_NAME, PROJECT_ID, LOCATION_REGIONAL, AGENT_ID_REGIONAL, TRAINING_PHRASES_PARTS);
newIntentNameRegional = result.getName();
System.out.println("intent name new:" + newIntentNameRegional);

assertEquals(result.getTrainingPhrasesCount(), TRAINING_PHRASES_PARTS.size());
for (TrainingPhrase trainingPhrase : result.getTrainingPhrasesList()) {
Expand Down
Loading

0 comments on commit 71bb6bb

Please sign in to comment.