From 577aa3dd24b3dbad1af7573b7ed8a4bdaeecedd0 Mon Sep 17 00:00:00 2001 From: Jiaqi Liu Date: Wed, 30 Mar 2022 11:58:57 -0700 Subject: [PATCH 1/4] sample: add sample code for updating answer record --- .../dialogflow/AnswerRecordManagement.java | 75 +++++++++++ .../dialogflow/UpdateAnswerRecordTest.java | 117 ++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/dialogflow/AnswerRecordManagement.java create mode 100644 samples/snippets/src/test/java/com/example/dialogflow/UpdateAnswerRecordTest.java diff --git a/samples/snippets/src/main/java/com/example/dialogflow/AnswerRecordManagement.java b/samples/snippets/src/main/java/com/example/dialogflow/AnswerRecordManagement.java new file mode 100644 index 000000000..c1cb66814 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/dialogflow/AnswerRecordManagement.java @@ -0,0 +1,75 @@ +/* + * Copyright 2022 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_update_answer_record] +import com.google.api.gax.rpc.ApiException; +import com.google.cloud.dialogflow.v2.AnswerFeedback; +import com.google.cloud.dialogflow.v2.AnswerRecord; +import com.google.cloud.dialogflow.v2.AnswerRecordName; +import com.google.cloud.dialogflow.v2.AnswerRecordsClient; +import com.google.cloud.dialogflow.v2.UpdateAnswerRecordRequest; +import com.google.protobuf.FieldMask; +import java.io.IOException; + +public class AnswerRecordManagement { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "my-location"; + + // Set id of the answer record to be updated. + // Answer records are created when there's a human agent assistant suggestion generated. + // See details about how to generate an answer record by getting ariticle suggestion here, + // https://cloud.google.com/agent-assist/docs/article-suggestion. + String answerRecordId = "my-answer-record-id"; + + // Set the value to be updated for answer_feedback.clicked field. + boolean isClicked = true; + updateAnswerRecord(projectId, location, answerRecordId, isClicked); + } + + // Update whether the answer record was clicked. + public static void updateAnswerRecord( + String projectId, String location, String answerRecordId, boolean clicked) + throws ApiException, IOException { + try (AnswerRecordsClient answerRecordsClient = AnswerRecordsClient.create()) { + AnswerRecordName answerRecordName = + AnswerRecordName.ofProjectLocationAnswerRecordName(projectId, location, answerRecordId); + AnswerFeedback answerFeedback = AnswerFeedback.newBuilder().setClicked(clicked).build(); + AnswerRecord answerRecord = + AnswerRecord.newBuilder() + .setName(answerRecordName.toString()) + .setAnswerFeedback(answerFeedback) + .build(); + FieldMask fieldMask = FieldMask.newBuilder().addPaths("answer_feedback").build(); + + UpdateAnswerRecordRequest request = + UpdateAnswerRecordRequest.newBuilder() + .setAnswerRecord(answerRecord) + .setUpdateMask(fieldMask) + .build(); + AnswerRecord response = answerRecordsClient.updateAnswerRecord(request); + System.out.println("===================="); + System.out.format("AnswerRecord updated:\n"); + System.out.format("Name: %s\n", response.getName()); + System.out.format("Clicked: %s\n", response.getAnswerFeedback().getClicked()); + } + } +} +// [END dialogflow_update_answer_record] \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/dialogflow/UpdateAnswerRecordTest.java b/samples/snippets/src/test/java/com/example/dialogflow/UpdateAnswerRecordTest.java new file mode 100644 index 000000000..7176435e2 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/dialogflow/UpdateAnswerRecordTest.java @@ -0,0 +1,117 @@ +/* + * Copyright 2022 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.cloud.dialogflow.v2.AnswerRecord; +import com.google.cloud.dialogflow.v2.AnswerRecordName; +import com.google.cloud.dialogflow.v2.AnswerRecordsClient; +import com.google.cloud.dialogflow.v2.LocationName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.Optional; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class UpdateAnswerRecordTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION = "global"; + private static final String FIELD_PREFIX_IN_OUTPUT = "Clicked: "; + private ByteArrayOutputStream bout; + private PrintStream newOutputStream; + private PrintStream originalOutputStream; + + private static void requireEnvVar(String varName) { + assertNotNull(System.getenv(varName)); + } + + // Get one existing answer record from current project + private static Optional getOneAnswerRecord() throws IOException { + try (AnswerRecordsClient answerRecordsClient = AnswerRecordsClient.create()) { + LocationName locationName = LocationName.of(PROJECT_ID, LOCATION); + for (AnswerRecord answerRecord : + answerRecordsClient.listAnswerRecords(locationName).iterateAll()) { + return Optional.of(answerRecord); + } + return Optional.empty(); + } + } + + // Extract the value of an updated field from latest "Clicked: %s\n" in sample code output + private static String getFieldValueFromOutputString(String output) { + return output.substring( + output.lastIndexOf(FIELD_PREFIX_IN_OUTPUT) + FIELD_PREFIX_IN_OUTPUT.length(), + output.length() - 1); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + originalOutputStream = System.out; + bout = new ByteArrayOutputStream(); + newOutputStream = new PrintStream(bout); + System.setOut(newOutputStream); + } + + @After + public void tearDown() throws IOException { + System.setOut(originalOutputStream); + } + + @Test + public void testUpdateAnswerRecord() throws IOException { + // Get one answer record + Optional answerRecordOptional = getOneAnswerRecord(); + assertTrue(answerRecordOptional.isPresent()); + + AnswerRecord originalAnswerRecord = answerRecordOptional.get(); + String answerRecordId = + AnswerRecordName.parse(originalAnswerRecord.getName()).getAnswerRecord(); + boolean originalClickedValue = originalAnswerRecord.getAnswerFeedback().getClicked(); + boolean newClickedValue = !originalClickedValue; + + // Update clicked value + AnswerRecordManagement.updateAnswerRecord( + PROJECT_ID, LOCATION, answerRecordId, newClickedValue); + String output = bout.toString(); + boolean updatedClickedValue = getFieldValueFromOutputString(output).equals("true"); + assertEquals(newClickedValue, updatedClickedValue); + + // Reset clicked value + AnswerRecordManagement.updateAnswerRecord( + PROJECT_ID, LOCATION, answerRecordId, originalClickedValue); + output = bout.toString(); + boolean resetClickedValue = getFieldValueFromOutputString(output).equals("true"); + assertEquals(originalClickedValue, resetClickedValue); + } +} From 3fca35c0d756e12ad98f4dfa3cc899007449ee71 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Fri, 1 Apr 2022 18:34:28 +0200 Subject: [PATCH 2/4] chore(deps): update dependency com.google.cloud:google-cloud-dialogflow to v4.5.10 (#905) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [com.google.cloud:google-cloud-dialogflow](https://togithub.com/googleapis/java-dialogflow) | `4.5.9` -> `4.5.10` | [![age](https://badges.renovateapi.com/packages/maven/com.google.cloud:google-cloud-dialogflow/4.5.10/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/maven/com.google.cloud:google-cloud-dialogflow/4.5.10/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/maven/com.google.cloud:google-cloud-dialogflow/4.5.10/compatibility-slim/4.5.9)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/maven/com.google.cloud:google-cloud-dialogflow/4.5.10/confidence-slim/4.5.9)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes
googleapis/java-dialogflow ### [`v4.5.10`](https://togithub.com/googleapis/java-dialogflow/blob/HEAD/CHANGELOG.md#​4510-httpsgithubcomgoogleapisjava-dialogflowcomparev459v4510-2022-03-29) [Compare Source](https://togithub.com/googleapis/java-dialogflow/compare/v4.5.9...v4.5.10)
--- ### Configuration 📅 **Schedule**: At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Renovate will not automatically rebase this PR, because other commits have been found. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] If you want to rebase/retry this PR, click this checkbox. --- This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/googleapis/java-dialogflow). --- README.md | 6 +++--- samples/install-without-bom/pom.xml | 2 +- samples/snapshot/pom.xml | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 639f55659..a32fe8735 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ If you are using Maven without BOM, add this to your dependencies: com.google.cloud google-cloud-dialogflow - 4.5.9 + 4.5.10 ``` @@ -56,13 +56,13 @@ implementation 'com.google.cloud:google-cloud-dialogflow' If you are using Gradle without BOM, add this to your dependencies ```Groovy -implementation 'com.google.cloud:google-cloud-dialogflow:4.5.9' +implementation 'com.google.cloud:google-cloud-dialogflow:4.5.10' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-dialogflow" % "4.5.9" +libraryDependencies += "com.google.cloud" % "google-cloud-dialogflow" % "4.5.10" ``` ## Authentication diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index ad874ea4f..197a7e939 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -29,7 +29,7 @@ com.google.cloud google-cloud-dialogflow - 4.5.9 + 4.5.10 diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index a0a3607b3..6592f5b7e 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -28,7 +28,7 @@ com.google.cloud google-cloud-dialogflow - 4.5.9 + 4.5.10 From 8c65320e75ff11f842d58b59301cfd28a0fbf34f Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Fri, 1 Apr 2022 19:00:18 +0200 Subject: [PATCH 3/4] chore(deps): update dependency com.google.cloud:libraries-bom to v25.1.0 (#907) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [com.google.cloud:libraries-bom](https://cloud.google.com/java/docs/bom) ([source](https://togithub.com/GoogleCloudPlatform/cloud-opensource-java)) | `25.0.0` -> `25.1.0` | [![age](https://badges.renovateapi.com/packages/maven/com.google.cloud:libraries-bom/25.1.0/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/maven/com.google.cloud:libraries-bom/25.1.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/maven/com.google.cloud:libraries-bom/25.1.0/compatibility-slim/25.0.0)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/maven/com.google.cloud:libraries-bom/25.1.0/confidence-slim/25.0.0)](https://docs.renovatebot.com/merge-confidence/) | --- ### Configuration 📅 **Schedule**: At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Renovate will not automatically rebase this PR, because other commits have been found. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] If you want to rebase/retry this PR, click this checkbox. --- This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/googleapis/java-dialogflow). --- README.md | 4 ++-- samples/snippets/pom.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a32fe8735..9c20de748 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file com.google.cloud libraries-bom - 25.0.0 + 25.1.0 pom import @@ -49,7 +49,7 @@ If you are using Maven without BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies ```Groovy -implementation platform('com.google.cloud:libraries-bom:25.0.0') +implementation platform('com.google.cloud:libraries-bom:25.1.0') implementation 'com.google.cloud:google-cloud-dialogflow' ``` diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index e497ae04b..b3b2d9c09 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -30,7 +30,7 @@ com.google.cloud libraries-bom - 25.0.0 + 25.1.0 pom import From d609b6c648697873546ea3b40a1e833d81ac8b67 Mon Sep 17 00:00:00 2001 From: Jiaqi Liu Date: Mon, 4 Apr 2022 10:10:48 -0700 Subject: [PATCH 4/4] sample: update code style --- .../com/example/dialogflow/AnswerRecordManagement.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/dialogflow/AnswerRecordManagement.java b/samples/snippets/src/main/java/com/example/dialogflow/AnswerRecordManagement.java index c1cb66814..c554f170d 100644 --- a/samples/snippets/src/main/java/com/example/dialogflow/AnswerRecordManagement.java +++ b/samples/snippets/src/main/java/com/example/dialogflow/AnswerRecordManagement.java @@ -34,8 +34,8 @@ public static void main(String[] args) throws IOException { String location = "my-location"; // Set id of the answer record to be updated. - // Answer records are created when there's a human agent assistant suggestion generated. - // See details about how to generate an answer record by getting ariticle suggestion here, + // Answer records are created when a suggestion for the human agent assistant is generated. + // See details about how to generate an answer record by getting article suggestion here: // https://cloud.google.com/agent-assist/docs/article-suggestion. String answerRecordId = "my-answer-record-id"; @@ -48,6 +48,8 @@ public static void main(String[] args) throws IOException { public static void updateAnswerRecord( String projectId, String location, String answerRecordId, boolean clicked) throws ApiException, IOException { + // Initialize a client for managing AnswerRecords. This client only needs to be created + // once, and can be reused for multiple requests. try (AnswerRecordsClient answerRecordsClient = AnswerRecordsClient.create()) { AnswerRecordName answerRecordName = AnswerRecordName.ofProjectLocationAnswerRecordName(projectId, location, answerRecordId); @@ -57,6 +59,7 @@ public static void updateAnswerRecord( .setName(answerRecordName.toString()) .setAnswerFeedback(answerFeedback) .build(); + // Add a mask to control which field gets updated. FieldMask fieldMask = FieldMask.newBuilder().addPaths("answer_feedback").build(); UpdateAnswerRecordRequest request =