diff --git a/README.md b/README.md index 639f55659..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 @@ -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 ``` @@ -49,20 +49,20 @@ 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' ``` 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 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 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..c554f170d --- /dev/null +++ b/samples/snippets/src/main/java/com/example/dialogflow/AnswerRecordManagement.java @@ -0,0 +1,78 @@ +/* + * 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 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"; + + // 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 { + // 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); + AnswerFeedback answerFeedback = AnswerFeedback.newBuilder().setClicked(clicked).build(); + AnswerRecord answerRecord = + AnswerRecord.newBuilder() + .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 = + 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); + } +}