diff --git a/dialogflow/snippets/src/main/java/com/example/dialogflow/DetectIntentWithLocation.java b/dialogflow/snippets/src/main/java/com/example/dialogflow/DetectIntentWithLocation.java new file mode 100644 index 00000000000..5ef3237b6dd --- /dev/null +++ b/dialogflow/snippets/src/main/java/com/example/dialogflow/DetectIntentWithLocation.java @@ -0,0 +1,79 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.dialogflow; + +// [START dialogflow_detect_intent_with_location] + +import com.google.api.gax.rpc.ApiException; +import com.google.cloud.dialogflow.v2beta1.DetectIntentResponse; +import com.google.cloud.dialogflow.v2beta1.QueryInput; +import com.google.cloud.dialogflow.v2beta1.QueryResult; +import com.google.cloud.dialogflow.v2beta1.SessionName; +import com.google.cloud.dialogflow.v2beta1.SessionsClient; +import com.google.cloud.dialogflow.v2beta1.SessionsSettings; +import com.google.cloud.dialogflow.v2beta1.TextInput; +import com.google.common.collect.Maps; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public class DetectIntentWithLocation { + + // DialogFlow API Detect Intent sample with text inputs. + public static Map detectIntentWithLocation( + String projectId, String locationId, List texts, String sessionId, + String languageCode) + throws IOException, ApiException { + SessionsSettings sessionsSettings = SessionsSettings.newBuilder() + .setEndpoint(locationId + "-dialogflow.googleapis.com:443").build(); + Map queryResults = Maps.newHashMap(); + // Instantiates a client + try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { + // Set the session name using the projectId (my-project-id), locationId and sessionId (UUID) + SessionName session = SessionName + .ofProjectLocationSessionName(projectId, locationId, sessionId); + System.out.println("Session Path: " + session.toString()); + + // Detect intents for each text input + for (String text : texts) { + // Set the text (hello) and language code (en-US) for the query + TextInput.Builder textInput = + TextInput.newBuilder().setText(text).setLanguageCode(languageCode); + + // Build the query with the TextInput + QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build(); + + // Performs the detect intent request + DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput); + + // Display the query result + QueryResult queryResult = response.getQueryResult(); + + System.out.println("===================="); + System.out.format("Query Text: '%s'\n", queryResult.getQueryText()); + System.out.format( + "Detected Intent: %s (confidence: %f)\n", + queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence()); + System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText()); + + queryResults.put(text, queryResult); + } + } + return queryResults; + } +} +// [END dialogflow_detect_intent_with_location] diff --git a/dialogflow/snippets/src/test/java/com/example/dialogflow/DetectIntentWithSentimentAndTextToSpeechIT.java b/dialogflow/snippets/src/test/java/com/example/dialogflow/DetectIntentWithSentimentAndTextToSpeechIT.java index 0e902d6c497..c0584150efc 100644 --- a/dialogflow/snippets/src/test/java/com/example/dialogflow/DetectIntentWithSentimentAndTextToSpeechIT.java +++ b/dialogflow/snippets/src/test/java/com/example/dialogflow/DetectIntentWithSentimentAndTextToSpeechIT.java @@ -32,12 +32,15 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Integration (system) tests for {@link DetectIntentWithSentimentAnalysis}. */ +/** + * Integration (system) tests for {@link DetectIntentWithSentimentAnalysis}. + */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class DetectIntentWithSentimentAndTextToSpeechIT { private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String LOCATION_ID = "asia-northeast1"; private static String SESSION_ID = UUID.randomUUID().toString(); private static String LANGUAGE_CODE = "en-US"; private static List TEXTS = @@ -72,6 +75,17 @@ public void testDetectIntentTexts() throws Exception { assertEquals("All set!", finalResult.getFulfillmentText()); } + @Test + public void testDetectIntentTextsWithLocation() throws Exception { + Map queryResults = + DetectIntentWithLocation + .detectIntentWithLocation(PROJECT_ID, LOCATION_ID, TEXTS, SESSION_ID, LANGUAGE_CODE); + com.google.cloud.dialogflow.v2beta1.QueryResult finalResult = + queryResults.get(TEXTS.get(TEXTS.size() - 1)); + assertTrue(finalResult.getAllRequiredParamsPresent()); + assertEquals("All set!", finalResult.getFulfillmentText()); + } + @Test public void testDetectIntentWithSentimentAnalysis() throws Exception { assertResults(