|
| 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] |
0 commit comments