Skip to content

Commit 71bb6bb

Browse files
samples: add sample code to query regional Dialogflow CX agent (#100)
* 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
1 parent 092858e commit 71bb6bb

File tree

10 files changed

+242
-54
lines changed

10 files changed

+242
-54
lines changed

dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateFlow.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.dialogflow.cx.v3beta1.EventHandler;
2424
import com.google.cloud.dialogflow.cx.v3beta1.Flow;
2525
import com.google.cloud.dialogflow.cx.v3beta1.FlowsClient;
26+
import com.google.cloud.dialogflow.cx.v3beta1.FlowsSettings;
2627
import com.google.cloud.dialogflow.cx.v3beta1.Fulfillment;
2728
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage;
2829
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage.Text;
@@ -41,8 +42,16 @@ public static Flow createFlow(
4142
String agentId,
4243
Map<String, String> eventsToFulfillmentMessages)
4344
throws IOException, ApiException {
45+
FlowsSettings.Builder flowsSettingsBuilder = FlowsSettings.newBuilder();
46+
if (locationId.equals("global")) {
47+
flowsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
48+
} else {
49+
flowsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
50+
}
51+
FlowsSettings flowsSettings = flowsSettingsBuilder.build();
52+
4453
// Instantiates a client
45-
try (FlowsClient flowsClient = FlowsClient.create()) {
54+
try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) {
4655
// Set the project agent name using the projectID (my-project-id), locationID (global), and
4756
// agentID (UUID).
4857
AgentName parent = AgentName.of(projectId, locationId, agentId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateIntent.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase;
2525
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase.Part;
2626
import com.google.cloud.dialogflow.cx.v3beta1.IntentsClient;
27+
import com.google.cloud.dialogflow.cx.v3beta1.IntentsSettings;
2728
import java.io.IOException;
2829
import java.util.ArrayList;
2930
import java.util.List;
@@ -38,8 +39,16 @@ public static Intent createIntent(
3839
String agentId,
3940
List<String> trainingPhrasesParts)
4041
throws IOException, ApiException {
42+
IntentsSettings.Builder intentsSettingsBuilder = IntentsSettings.newBuilder();
43+
if (locationId.equals("global")) {
44+
intentsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
45+
} else {
46+
intentsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
47+
}
48+
IntentsSettings intentsSettings = intentsSettingsBuilder.build();
49+
4150
// Instantiates a client
42-
try (IntentsClient intentsClient = IntentsClient.create()) {
51+
try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
4352
// Set the project agent name using the projectID (my-project-id), locationID (global), and
4453
// agentID (UUID).
4554
AgentName parent = AgentName.of(projectId, locationId, agentId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreatePage.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.cloud.dialogflow.cx.v3beta1.Fulfillment;
2727
import com.google.cloud.dialogflow.cx.v3beta1.Page;
2828
import com.google.cloud.dialogflow.cx.v3beta1.PagesClient;
29+
import com.google.cloud.dialogflow.cx.v3beta1.PagesSettings;
2930
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage;
3031
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage.Text;
3132
import java.io.IOException;
@@ -42,8 +43,16 @@ public static Page createPage(
4243
String flowId,
4344
List<String> entryTexts)
4445
throws IOException, ApiException {
46+
PagesSettings.Builder pagesSettingsBuilder = PagesSettings.newBuilder();
47+
if (locationId.equals("global")) {
48+
pagesSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
49+
} else {
50+
pagesSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
51+
}
52+
PagesSettings pagesSettings = pagesSettingsBuilder.build();
53+
4554
// Instantiates a client
46-
try (PagesClient pagesClient = PagesClient.create()) {
55+
try (PagesClient pagesClient = PagesClient.create(pagesSettings)) {
4756
// Set the flow name using the projectID (my-project-id), locationID (global), agentID (UUID)
4857
// and flowID (UUID).
4958
FlowName parent = FlowName.of(projectId, locationId, agentId, flowId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntent.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.cloud.dialogflow.cx.v3beta1.QueryResult;
2626
import com.google.cloud.dialogflow.cx.v3beta1.SessionName;
2727
import com.google.cloud.dialogflow.cx.v3beta1.SessionsClient;
28+
import com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings;
2829
import com.google.cloud.dialogflow.cx.v3beta1.TextInput;
2930
import com.google.common.collect.Maps;
3031
import java.io.IOException;
@@ -42,9 +43,17 @@ public static Map<String, QueryResult> detectIntent(
4243
List<String> texts,
4344
String languageCode)
4445
throws IOException, ApiException {
46+
SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
47+
if (locationId.equals("global")) {
48+
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
49+
} else {
50+
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
51+
}
52+
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();
53+
4554
Map<String, QueryResult> queryResults = Maps.newHashMap();
4655
// Instantiates a client
47-
try (SessionsClient sessionsClient = SessionsClient.create()) {
56+
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
4857
// Set the session name using the projectID (my-project-id), locationID (global), agentID
4958
// (UUID), and sessionId (UUID).
5059
SessionName session = SessionName.of(projectId, locationId, agentId, sessionId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.cloud.dialogflow.cx.v3beta1.QueryResult;
2828
import com.google.cloud.dialogflow.cx.v3beta1.SessionName;
2929
import com.google.cloud.dialogflow.cx.v3beta1.SessionsClient;
30+
import com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings;
3031
import com.google.cloud.dialogflow.cx.v3beta1.StreamingDetectIntentRequest;
3132
import com.google.cloud.dialogflow.cx.v3beta1.StreamingDetectIntentResponse;
3233
import com.google.protobuf.ByteString;
@@ -39,8 +40,16 @@ public class DetectIntentStream {
3940
public static void detectIntentStream(
4041
String projectId, String locationId, String agentId, String sessionId, String audioFilePath)
4142
throws ApiException, IOException {
43+
SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
44+
if (locationId.equals("global")) {
45+
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
46+
} else {
47+
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
48+
}
49+
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();
50+
4251
// Instantiates a client
43-
try (SessionsClient sessionsClient = SessionsClient.create()) {
52+
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
4453
// Set the session name using the projectID (my-project-id), locationID (global), agentID
4554
// (UUID), and sessionId (UUID).
4655
// Using the same `sessionId` between requests allows continuation of the conversation.

dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateFlowIT.java

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020

2121
import com.google.cloud.dialogflow.cx.v3beta1.Flow;
2222
import com.google.cloud.dialogflow.cx.v3beta1.FlowsClient;
23+
import com.google.cloud.dialogflow.cx.v3beta1.FlowsSettings;
2324
import com.google.common.collect.ImmutableMap;
2425
import java.util.Map;
2526
import java.util.UUID;
26-
import org.junit.After;
27+
import org.junit.AfterClass;
2728
import org.junit.Test;
2829
import org.junit.runner.RunWith;
2930
import org.junit.runners.JUnit4;
@@ -35,30 +36,67 @@ public class CreateFlowIT {
3536

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

46-
@After
47-
public void tearDown() throws Exception {
48-
// Delete the newly created Flow.
49-
if (newFlowName != null) {
50+
private static String newFlowNameGlobal;
51+
private static String newFlowNameRegional;
52+
53+
@AfterClass
54+
public static void tearDown() throws Exception {
55+
// Delete the newly created Flow in the global location.
56+
if (newFlowNameGlobal != null) {
5057
try (FlowsClient flowsClient = FlowsClient.create()) {
51-
flowsClient.deleteFlow(newFlowName);
58+
flowsClient.deleteFlow(newFlowNameGlobal);
5259
}
5360
}
61+
62+
// Delete the newly created Flow in the regional location.
63+
if (newFlowNameRegional != null) {
64+
FlowsSettings flowsSettings =
65+
FlowsSettings.newBuilder()
66+
.setEndpoint(LOCATION_REGIONAL + "-dialogflow.googleapis.com:443")
67+
.build();
68+
try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) {
69+
flowsClient.deleteFlow(newFlowNameRegional);
70+
}
71+
}
72+
}
73+
74+
@Test
75+
public void testCreateFlowGlobal() throws Exception {
76+
Flow result =
77+
CreateFlow.createFlow(
78+
DISPLAY_NAME,
79+
PROJECT_ID,
80+
LOCATION_GLOBAL,
81+
AGENT_ID_GLOBAL,
82+
EVENT_TO_FULFILLMENT_MESSAGES);
83+
newFlowNameGlobal = result.getName();
84+
85+
assertEquals(result.getDisplayName(), DISPLAY_NAME);
86+
// Number of added event handlers + 2 default event handlers.
87+
assertEquals(result.getEventHandlersCount(), EVENT_TO_FULFILLMENT_MESSAGES.size() + 2);
5488
}
5589

5690
@Test
57-
public void testCreateFlow() throws Exception {
91+
public void testCreateFlowRegional() throws Exception {
5892
Flow result =
5993
CreateFlow.createFlow(
60-
DISPLAY_NAME, PROJECT_ID, LOCATION, AGENT_ID, EVENT_TO_FULFILLMENT_MESSAGES);
61-
newFlowName = result.getName();
94+
DISPLAY_NAME,
95+
PROJECT_ID,
96+
LOCATION_REGIONAL,
97+
AGENT_ID_REGIONAL,
98+
EVENT_TO_FULFILLMENT_MESSAGES);
99+
newFlowNameRegional = result.getName();
62100

63101
assertEquals(result.getDisplayName(), DISPLAY_NAME);
64102
// Number of added event handlers + 2 default event handlers.

dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateIntentIT.java

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import com.google.cloud.dialogflow.cx.v3beta1.Intent;
2323
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase;
2424
import com.google.cloud.dialogflow.cx.v3beta1.IntentsClient;
25+
import com.google.cloud.dialogflow.cx.v3beta1.IntentsSettings;
2526
import java.util.Arrays;
2627
import java.util.List;
2728
import java.util.UUID;
28-
import org.junit.After;
29+
import org.junit.AfterClass;
2930
import org.junit.Test;
3031
import org.junit.runner.RunWith;
3132
import org.junit.runners.JUnit4;
@@ -37,29 +38,62 @@ public class CreateIntentIT {
3738

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

47-
@After
48-
public void tearDown() throws Exception {
49-
// Delete the newly created Intent.
50-
if (newIntentName != null) {
51+
private static String newIntentNameGlobal;
52+
private static String newIntentNameRegional;
53+
54+
@AfterClass
55+
public static void tearDown() throws Exception {
56+
// Delete the newly created Intent in the global location.
57+
if (newIntentNameGlobal != null) {
5158
try (IntentsClient intentsClient = IntentsClient.create()) {
52-
intentsClient.deleteIntent(newIntentName);
59+
intentsClient.deleteIntent(newIntentNameGlobal);
60+
}
61+
}
62+
63+
// Delete the newly created Intent in the regional location.
64+
if (newIntentNameRegional != null) {
65+
IntentsSettings intentsSettings =
66+
IntentsSettings.newBuilder()
67+
.setEndpoint(LOCATION_REGIONAL + "-dialogflow.googleapis.com:443")
68+
.build();
69+
try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
70+
intentsClient.deleteIntent(newIntentNameRegional);
5371
}
5472
}
5573
}
5674

5775
@Test
58-
public void testCreateIntent() throws Exception {
76+
public void testCreateIntentGlobal() throws Exception {
77+
Intent result =
78+
CreateIntent.createIntent(
79+
DISPLAY_NAME, PROJECT_ID, LOCATION_GLOBAL, AGENT_ID_GLOBAL, TRAINING_PHRASES_PARTS);
80+
newIntentNameGlobal = result.getName();
81+
82+
assertEquals(result.getTrainingPhrasesCount(), TRAINING_PHRASES_PARTS.size());
83+
for (TrainingPhrase trainingPhrase : result.getTrainingPhrasesList()) {
84+
assertEquals(trainingPhrase.getPartsCount(), 1);
85+
String partText = trainingPhrase.getParts(0).getText();
86+
assertTrue(partText.equals("red") || partText.equals("blue") || partText.equals("green"));
87+
}
88+
}
89+
90+
@Test
91+
public void testCreateIntentRegional() throws Exception {
5992
Intent result =
6093
CreateIntent.createIntent(
61-
DISPLAY_NAME, PROJECT_ID, LOCATION, AGENT_ID, TRAINING_PHRASES_PARTS);
62-
newIntentName = result.getName();
94+
DISPLAY_NAME, PROJECT_ID, LOCATION_REGIONAL, AGENT_ID_REGIONAL, TRAINING_PHRASES_PARTS);
95+
newIntentNameRegional = result.getName();
96+
System.out.println("intent name new:" + newIntentNameRegional);
6397

6498
assertEquals(result.getTrainingPhrasesCount(), TRAINING_PHRASES_PARTS.size());
6599
for (TrainingPhrase trainingPhrase : result.getTrainingPhrasesList()) {

0 commit comments

Comments
 (0)