Skip to content

Commit 3044fad

Browse files
samples: added lro code snippet and removed webhook snippet (#408)
* samples: added lro code snippet and removed webhook snippet * changed intent id for list intent id due to deletion * moved then random test name creation into each test * tried to solve nullExceptionPointer * revert testing changes * reverted pom changes * lint fix * lint fix * added shutdowns * fixed lint * commented print statements * uncommented print code * Commented Print Statements * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1131f07 commit 3044fad

File tree

9 files changed

+182
-150
lines changed

9 files changed

+182
-150
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ public static Flow createFlow(
7979

8080
// Performs the create flow request.
8181
Flow response = flowsClient.createFlow(parent, flow);
82-
System.out.format("Flow created: %s\n", response);
8382

83+
// TODO : Uncomment if you want to print response
84+
// System.out.format("Flow created: %s\n", response.toString());
85+
flowsClient.shutdown();
8486
return response;
8587
}
8688
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public static Intent createIntent(
7272

7373
// Performs the create intent request.
7474
Intent response = intentsClient.createIntent(parent, intent);
75-
System.out.format("Intent created: %s\n", response);
75+
76+
// TODO : Uncomment if you want to print response
77+
// System.out.format("Intent created: %s\n", response);
7678

7779
return response;
7880
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,11 @@ public static Page createPage(
101101

102102
// Performs the create page request.
103103
Page response = pagesClient.createPage(parent, page);
104-
System.out.format("Page created: %s\n", response);
105104

105+
// TODO : Uncomment if you want to print response
106+
// System.out.format("Page created: %s\n", response.toString());
107+
108+
pagesClient.shutdown();
106109
return response;
107110
}
108111
}

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ public static Map<String, QueryResult> detectIntent(
5656
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
5757
// Set the session name using the projectID (my-project-id), locationID (global), agentID
5858
// (UUID), and sessionId (UUID).
59-
SessionName session = SessionName.of(projectId, locationId, agentId, sessionId);
60-
System.out.println("Session Path: " + session.toString());
59+
SessionName session =
60+
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
61+
62+
// TODO : Uncomment if you want to print session path
63+
// System.out.println("Session Path: " + session.toString());
6164

6265
// Detect intents for each text input.
6366
for (String text : texts) {
@@ -81,11 +84,13 @@ public static Map<String, QueryResult> detectIntent(
8184
// Display the query result.
8285
QueryResult queryResult = response.getQueryResult();
8386

84-
System.out.println("====================");
85-
System.out.format("Query Text: '%s'\n", queryResult.getText());
86-
System.out.format(
87-
"Detected Intent: %s (confidence: %f)\n",
88-
queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
87+
// TODO : Uncomment if you want to print queryResult
88+
// System.out.println("====================");
89+
// System.out.format("Query Text: '%s'\n", queryResult.getText());
90+
// System.out.format(
91+
// "Detected Intent: %s (confidence: %f)\n",
92+
// queryResult.getIntent().getDisplayName(),
93+
// queryResult.getIntentDetectionConfidence());
8994

9095
queryResults.put(text, queryResult);
9196
}

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

-67
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
// [START dialogflow_cx_export_agent]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.dialogflow.cx.v3.AgentName;
23+
import com.google.cloud.dialogflow.cx.v3.AgentsClient;
24+
import com.google.cloud.dialogflow.cx.v3.AgentsSettings;
25+
import com.google.cloud.dialogflow.cx.v3.ExportAgentRequest;
26+
import com.google.cloud.dialogflow.cx.v3.ExportAgentResponse;
27+
import com.google.protobuf.Struct;
28+
import java.io.IOException;
29+
import java.util.concurrent.ExecutionException;
30+
31+
public class ExportAgent {
32+
33+
public static void main(String[] args)
34+
throws IOException, InterruptedException, ExecutionException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "my-project-id";
37+
String agentId = "my-agent-id";
38+
String location = "my-location";
39+
40+
exportAgent(projectId, agentId, location);
41+
}
42+
43+
public static void exportAgent(String projectId, String agentId, String location)
44+
throws IOException, InterruptedException, ExecutionException {
45+
46+
// Sets the api endpoint to specified location
47+
String apiEndpoint = String.format("%s-dialogflow.googleapis.com:443", location);
48+
49+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
50+
try (AgentsClient agentsClient = AgentsClient.create(agentsSettings)) {
51+
ExportAgentRequest request =
52+
ExportAgentRequest.newBuilder()
53+
.setName(AgentName.of(projectId, location, agentId).toString())
54+
.build();
55+
56+
// Returns a future of the operation
57+
OperationFuture<ExportAgentResponse, Struct> future =
58+
agentsClient.exportAgentOperationCallable().futureCall(request);
59+
60+
// get the export agent response after the operation is completed
61+
ExportAgentResponse response = future.get();
62+
System.out.println(response);
63+
}
64+
}
65+
}
66+
// [END dialogflow_cx_export_agent]

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

-72
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.dialogflow.cx.v3.Agent;
22+
import com.google.cloud.dialogflow.cx.v3.Agent.Builder;
23+
import com.google.cloud.dialogflow.cx.v3.AgentsClient;
24+
import com.google.cloud.dialogflow.cx.v3.AgentsSettings;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.PrintStream;
28+
import java.util.UUID;
29+
import java.util.concurrent.ExecutionException;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
35+
public class ExportAgentIT {
36+
37+
private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
38+
private static String parent = "";
39+
private static String agentPath = "";
40+
private static String agentID = "";
41+
42+
private ByteArrayOutputStream stdOut;
43+
44+
@BeforeClass
45+
public static void beforeAll() {
46+
assertThat(PROJECT_ID).isNotNull();
47+
}
48+
49+
@Before
50+
public void setUp() throws IOException {
51+
stdOut = new ByteArrayOutputStream();
52+
System.setOut(new PrintStream(stdOut));
53+
54+
Builder build = Agent.newBuilder();
55+
build.setDefaultLanguageCode("en");
56+
build.setDisplayName("temp_agent_" + UUID.randomUUID().toString());
57+
build.setTimeZone("America/Los_Angeles");
58+
59+
Agent agent = build.build();
60+
61+
String apiEndpoint = "global-dialogflow.googleapis.com:443";
62+
String parentPath = "projects/" + PROJECT_ID + "/locations/global";
63+
64+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
65+
AgentsClient client = AgentsClient.create(agentsSettings);
66+
67+
parent = client.createAgent(parentPath, agent).getName();
68+
ExportAgentIT.agentPath = parent;
69+
ExportAgentIT.agentID = parent.split("/")[5];
70+
client.close();
71+
}
72+
73+
@After
74+
public void tearDown() throws IOException {
75+
stdOut = null;
76+
System.setOut(null);
77+
String apiEndpoint = "global-dialogflow.googleapis.com:443";
78+
79+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
80+
AgentsClient client = AgentsClient.create(agentsSettings);
81+
82+
client.deleteAgent(ExportAgentIT.agentPath);
83+
client.close();
84+
}
85+
86+
@Test
87+
public void testUpdateExportAgent() throws IOException, InterruptedException, ExecutionException {
88+
89+
ExportAgent.exportAgent(PROJECT_ID, ExportAgentIT.agentID, "global");
90+
91+
assertThat(stdOut.toString()).contains(ExportAgentIT.agentID);
92+
}
93+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ListTrainingPhrasesTest {
2929
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
3030
private static String LOCATION = "global";
3131
private static String AGENT_ID = "b8d0e85d-0741-4e6d-a66a-3671184b7b93";
32-
private static String INTENT_ID = "45974f75-9412-445a-9863-47bfdfa3d96d";
32+
private static String INTENT_ID = "e2d688a6-c8b4-448e-a7d6-208f147ae689";
3333

3434
private ByteArrayOutputStream stdOut;
3535

0 commit comments

Comments
 (0)