Skip to content

Commit 3eb40b8

Browse files
martini9393Shabirmean
authored andcommitted
samples: add sample code to query regional Dialogflow agent (#310)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-dialogflow/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> ☕️
1 parent e56ad46 commit 3eb40b8

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2020 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 com.example.dialogflow;
18+
19+
// [START dialogflow_detect_intent_with_location]
20+
21+
import com.google.api.gax.rpc.ApiException;
22+
import com.google.cloud.dialogflow.v2beta1.DetectIntentResponse;
23+
import com.google.cloud.dialogflow.v2beta1.QueryInput;
24+
import com.google.cloud.dialogflow.v2beta1.QueryResult;
25+
import com.google.cloud.dialogflow.v2beta1.SessionName;
26+
import com.google.cloud.dialogflow.v2beta1.SessionsClient;
27+
import com.google.cloud.dialogflow.v2beta1.SessionsSettings;
28+
import com.google.cloud.dialogflow.v2beta1.TextInput;
29+
import com.google.common.collect.Maps;
30+
import java.io.IOException;
31+
import java.util.List;
32+
import java.util.Map;
33+
34+
public class DetectIntentWithLocation {
35+
36+
// DialogFlow API Detect Intent sample with text inputs.
37+
public static Map<String, QueryResult> detectIntentWithLocation(
38+
String projectId, String locationId, List<String> texts, String sessionId,
39+
String languageCode)
40+
throws IOException, ApiException {
41+
SessionsSettings sessionsSettings = SessionsSettings.newBuilder()
42+
.setEndpoint(locationId + "-dialogflow.googleapis.com:443").build();
43+
Map<String, QueryResult> queryResults = Maps.newHashMap();
44+
// Instantiates a client
45+
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
46+
// Set the session name using the projectId (my-project-id), locationId and sessionId (UUID)
47+
SessionName session = SessionName
48+
.ofProjectLocationSessionName(projectId, locationId, sessionId);
49+
System.out.println("Session Path: " + session.toString());
50+
51+
// Detect intents for each text input
52+
for (String text : texts) {
53+
// Set the text (hello) and language code (en-US) for the query
54+
TextInput.Builder textInput =
55+
TextInput.newBuilder().setText(text).setLanguageCode(languageCode);
56+
57+
// Build the query with the TextInput
58+
QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();
59+
60+
// Performs the detect intent request
61+
DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput);
62+
63+
// Display the query result
64+
QueryResult queryResult = response.getQueryResult();
65+
66+
System.out.println("====================");
67+
System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
68+
System.out.format(
69+
"Detected Intent: %s (confidence: %f)\n",
70+
queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
71+
System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText());
72+
73+
queryResults.put(text, queryResult);
74+
}
75+
}
76+
return queryResults;
77+
}
78+
}
79+
// [END dialogflow_detect_intent_with_location]

dialogflow/snippets/src/test/java/com/example/dialogflow/DetectIntentWithSentimentAndTextToSpeechIT.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232
import org.junit.runner.RunWith;
3333
import org.junit.runners.JUnit4;
3434

35-
/** Integration (system) tests for {@link DetectIntentWithSentimentAnalysis}. */
35+
/**
36+
* Integration (system) tests for {@link DetectIntentWithSentimentAnalysis}.
37+
*/
3638
@RunWith(JUnit4.class)
3739
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3840
public class DetectIntentWithSentimentAndTextToSpeechIT {
3941

4042
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
43+
private static String LOCATION_ID = "asia-northeast1";
4144
private static String SESSION_ID = UUID.randomUUID().toString();
4245
private static String LANGUAGE_CODE = "en-US";
4346
private static List<String> TEXTS =
@@ -72,6 +75,17 @@ public void testDetectIntentTexts() throws Exception {
7275
assertEquals("All set!", finalResult.getFulfillmentText());
7376
}
7477

78+
@Test
79+
public void testDetectIntentTextsWithLocation() throws Exception {
80+
Map<String, com.google.cloud.dialogflow.v2beta1.QueryResult> queryResults =
81+
DetectIntentWithLocation
82+
.detectIntentWithLocation(PROJECT_ID, LOCATION_ID, TEXTS, SESSION_ID, LANGUAGE_CODE);
83+
com.google.cloud.dialogflow.v2beta1.QueryResult finalResult =
84+
queryResults.get(TEXTS.get(TEXTS.size() - 1));
85+
assertTrue(finalResult.getAllRequiredParamsPresent());
86+
assertEquals("All set!", finalResult.getFulfillmentText());
87+
}
88+
7589
@Test
7690
public void testDetectIntentWithSentimentAnalysis() throws Exception {
7791
assertResults(

0 commit comments

Comments
 (0)