Skip to content

Commit

Permalink
samples: add sample code to query regional Dialogflow agent (#310)
Browse files Browse the repository at this point in the history
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> ☕️
  • Loading branch information
martini9393 authored and Shabirmean committed Nov 15, 2022
1 parent e56ad46 commit 3eb40b8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<String, QueryResult> detectIntentWithLocation(
String projectId, String locationId, List<String> texts, String sessionId,
String languageCode)
throws IOException, ApiException {
SessionsSettings sessionsSettings = SessionsSettings.newBuilder()
.setEndpoint(locationId + "-dialogflow.googleapis.com:443").build();
Map<String, QueryResult> 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]
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> TEXTS =
Expand Down Expand Up @@ -72,6 +75,17 @@ public void testDetectIntentTexts() throws Exception {
assertEquals("All set!", finalResult.getFulfillmentText());
}

@Test
public void testDetectIntentTextsWithLocation() throws Exception {
Map<String, com.google.cloud.dialogflow.v2beta1.QueryResult> 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(
Expand Down

0 comments on commit 3eb40b8

Please sign in to comment.