Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: migrate code from googleapis/java-dialogflow-cx #7435

Merged
merged 74 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
0a5d868
feat: initial code generation
chingor13 Aug 17, 2020
349c6b9
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Aug 26, 2020
9683553
chore: fix samples dependencies so tests pass (#11)
chingor13 Aug 26, 2020
01ee354
samples: add samples for Dialogflow CX (#15)
jessiexjiang27 Sep 11, 2020
5ada7f8
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Oct 2, 2020
ebb8906
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Oct 8, 2020
605a516
test(deps): update dependency junit:junit to v4.13.1
renovate-bot Oct 12, 2020
1656f00
test(deps): update dependency com.google.truth:truth to v1.1 (#56)
renovate-bot Oct 22, 2020
a38ea5e
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Nov 9, 2020
ea77718
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Nov 13, 2020
092858e
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Nov 19, 2020
71bb6bb
samples: add sample code to query regional Dialogflow CX agent (#100)
jessiexjiang27 Dec 10, 2020
3ab5f10
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Dec 14, 2020
09a0bd5
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Jan 7, 2021
17fbd89
test(deps): update dependency junit:junit to v4.13.2 (#140)
renovate-bot Feb 16, 2021
f6ba71a
test(deps): update dependency com.google.truth:truth to v1.1.2 (#128)
renovate-bot Feb 25, 2021
c17d188
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Feb 25, 2021
5ea77f1
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Feb 26, 2021
c0226c1
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 9, 2021
5027260
test(deps): update dependency com.google.truth:truth to v1.1.3 (#223)
renovate-bot May 26, 2021
d51efdb
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Jun 7, 2021
15ba4c3
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Jun 17, 2021
bfb34d0
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Jul 14, 2021
c8f3e2f
samples: add update intent code sample (#275)
galz10 Aug 12, 2021
c5c6e7c
samples: add create agent code sample (#279)
galz10 Aug 23, 2021
5f69721
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Sep 14, 2021
fd7d74c
samples: add page management code samples (#300)
galz10 Sep 14, 2021
faf1b04
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Sep 27, 2021
7bce2b7
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Sep 28, 2021
627b481
samples: added list testcase result sample (#315)
galz10 Oct 6, 2021
0b965f0
samples: add voice selection (#333)
galz10 Oct 14, 2021
4b25209
docs(samples): adds list training phrases sample (#336)
b-loved-dreamer Oct 15, 2021
2cf1322
samples: added webhook sample (#313)
galz10 Nov 11, 2021
153ae09
test(deps): update dependency org.mockito:mockito-core to v4 (#359)
renovate-bot Nov 15, 2021
20c3074
deps: update dependency com.google.cloud.functions:function-maven-plu…
renovate-bot Nov 15, 2021
9600f6e
deps: update dependency com.google.code.gson:gson to v2.8.9 (#357)
renovate-bot Nov 15, 2021
9ca6274
test(deps): update dependency org.mockito:mockito-core to v4.1.0 (#364)
renovate-bot Nov 19, 2021
7c5796c
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Dec 6, 2021
37be464
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Jan 12, 2022
14e2da1
test(deps): update dependency org.mockito:mockito-core to v4.3.0 (#378)
renovate-bot Jan 24, 2022
204ab0c
test(deps): update dependency org.mockito:mockito-core to v4.3.1 (#406)
renovate-bot Feb 2, 2022
4bf85d1
deps: update dependency com.google.code.gson:gson to v2.9.0 (#422)
renovate-bot Feb 15, 2022
64b294a
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Feb 17, 2022
f796db2
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Feb 18, 2022
1131f07
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Mar 1, 2022
3044fad
samples: added lro code snippet and removed webhook snippet (#408)
galz10 Mar 1, 2022
e5508c7
test(deps): update dependency org.mockito:mockito-core to v4.4.0 (#446)
renovate-bot Mar 8, 2022
76541b0
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Mar 29, 2022
ac99e05
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Apr 1, 2022
ad23f35
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Apr 21, 2022
b9758f1
test(deps): update dependency org.mockito:mockito-core to v4.5.0 (#467)
renovate-bot Apr 21, 2022
2639486
test(deps): update dependency org.mockito:mockito-core to v4.5.1 (#471)
renovate-bot Apr 22, 2022
bb6e850
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot May 26, 2022
a1bf0c3
test(deps): update dependency org.mockito:mockito-core to v4.6.1 (#486)
renovate-bot Jun 6, 2022
30e5286
test: remove extra println causing NPE in test (#495)
kweinmeister Jul 1, 2022
a0833c7
ci: use Java 11 for samples testing (#505)
kweinmeister Jul 11, 2022
5f5066d
docs: add DetectIntent samples (#500)
aribray Jul 12, 2022
7bced2c
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Jul 12, 2022
1bba4de
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Jul 13, 2022
de67b0d
docs(samples): add webhook samples (#479)
aribray Jul 16, 2022
382059f
docs(samples): add try blocks to clean up resources in samples and sa…
aribray Jul 19, 2022
04ca1d3
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Aug 2, 2022
599a65d
deps: update dependency com.google.code.gson:gson to v2.9.1 (#528)
renovate-bot Aug 2, 2022
b9c2190
test(deps): update dependency org.mockito:mockito-core to v4.7.0 (#534)
renovate-bot Aug 16, 2022
9dff496
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Aug 16, 2022
4781140
deps: update dependency com.google.cloud.functions:function-maven-plu…
renovate-bot Aug 18, 2022
e2c781d
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Sep 6, 2022
e224603
test(deps): update dependency org.mockito:mockito-core to v4.8.0 (#543)
renovate-bot Sep 8, 2022
7f35e3c
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Sep 20, 2022
84fe00f
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Sep 21, 2022
1cb9d87
chore(deps): update dependency com.google.cloud:google-cloud-dialogfl…
renovate-bot Oct 6, 2022
967453c
Merge remote-tracking branch 'migration/main' into java-dialogflow-cx…
Shabirmean Nov 16, 2022
088ca4f
chore: post migration updates - groupId, artifact url, repo references
Shabirmean Nov 16, 2022
86ce9d1
chore: remove test for non-migrated sample
Shabirmean Nov 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions dialogflow-cx/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.dialogflow-cx</groupId>
<artifactId>dialogflow-cx-snippets</artifactId>
<packaging>jar</packaging>
<name>Google Dialogflow CX Snippets</name>
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/dialogflow-cx</url>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- TODO: switch to libraries-bom after this artifact is included -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-dialogflow-cx</artifactId>
<version>0.14.7</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Binary file added dialogflow-cx/resources/book_a_room.wav
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 dialogflow.cx;

// The following snippet is used in https://cloud.google.com/dialogflow/cx/docs/concept/webhook

// [START dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required]

// TODO: Change class name to Example
// TODO: Uncomment the line below before running cloud function
// package com.example;

import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.io.BufferedWriter;

public class ConfigureWebhookToSetFormParametersAsOptionalOrRequired implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonObject parameterObject = new JsonObject();
parameterObject.addProperty("display_name", "order_number");
parameterObject.addProperty("required", "true");
parameterObject.addProperty("state", "VALID");

JsonArray parameterInfoList = new JsonArray();
parameterInfoList.add(parameterObject);

JsonObject parameterInfoObject = new JsonObject();
parameterInfoObject.add("parameter_info", parameterInfoList);

JsonObject formInfo = new JsonObject();
formInfo.add("form_info", parameterInfoObject);

// Constructs the webhook response object
JsonObject webhookResponse = new JsonObject();
webhookResponse.add("page_info", formInfo);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonResponseObject = gson.toJson(webhookResponse);

/* {
* "page_info": {
* "form_info": {
* "parameter_info": [
* {
* "display_name": "order_number",
* "required": "true",
* "state": "VALID"
* }
* ]
* }
* }
* }
*/

BufferedWriter writer = response.getWriter();

// Sends the responseObject
writer.write(jsonResponseObject.toString());
}
}
// [END dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required]
65 changes: 65 additions & 0 deletions dialogflow-cx/src/main/java/dialogflow/cx/CreateAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2021 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 dialogflow.cx;

// [START dialogflow_cx_create_agent]

import com.google.cloud.dialogflow.cx.v3.Agent;
import com.google.cloud.dialogflow.cx.v3.Agent.Builder;
import com.google.cloud.dialogflow.cx.v3.AgentsClient;
import com.google.cloud.dialogflow.cx.v3.AgentsSettings;
import java.io.IOException;

public class CreateAgent {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String displayName = "my-display-name";

createAgent(projectId, displayName);
}

public static Agent createAgent(String parent, String displayName) throws IOException {

String apiEndpoint = "global-dialogflow.googleapis.com:443";

AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
// Note: close() needs to be called on the AgentsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (AgentsClient client = AgentsClient.create(agentsSettings)) {
// Set the details of the Agent to create
Builder build = Agent.newBuilder();

build.setDefaultLanguageCode("en");
build.setDisplayName(displayName);
// Correct format for timezone is location/city
// For example America/Los_Angeles, Europe/Madrid, Asia/Tokyo
build.setTimeZone("America/Los_Angeles");

Agent agent = build.build();
String parentPath = String.format("projects/%s/locations/%s", parent, "global");

// Calls the create agent api and returns the created Agent
Agent response = client.createAgent(parentPath, agent);
System.out.println(response);
return response;
}
}
}
// [END dialogflow_cx_create_agent]
93 changes: 93 additions & 0 deletions dialogflow-cx/src/main/java/dialogflow/cx/CreateFlow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 dialogflow.cx;

// [START dialogflow_cx_create_flow]

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.cx.v3beta1.AgentName;
import com.google.cloud.dialogflow.cx.v3beta1.EventHandler;
import com.google.cloud.dialogflow.cx.v3beta1.Flow;
import com.google.cloud.dialogflow.cx.v3beta1.FlowsClient;
import com.google.cloud.dialogflow.cx.v3beta1.FlowsSettings;
import com.google.cloud.dialogflow.cx.v3beta1.Fulfillment;
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage;
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage.Text;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CreateFlow {

// Create a flow in the specified agent.
public static Flow createFlow(
String displayName,
String projectId,
String locationId,
String agentId,
Map<String, String> eventsToFulfillmentMessages)
throws IOException, ApiException {
FlowsSettings.Builder flowsSettingsBuilder = FlowsSettings.newBuilder();
if (locationId.equals("global")) {
flowsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
} else {
flowsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
}
FlowsSettings flowsSettings = flowsSettingsBuilder.build();

// Instantiates a client.
// Note: close() needs to be called on the FlowsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) {
// Set the project agent name using the projectID (my-project-id), locationID (global), and
// agentID (UUID).
AgentName parent = AgentName.of(projectId, locationId, agentId);

// Build the EventHandlers for the flow using the mapping from events to fulfillment messages.
List<EventHandler> eventHandlers = new ArrayList<>();
for (Map.Entry<String, String> item : eventsToFulfillmentMessages.entrySet()) {
eventHandlers.add(
EventHandler.newBuilder()
.setEvent(item.getKey()) // Event (sys.no-match-default)
.setTriggerFulfillment(
Fulfillment.newBuilder()
// Text ("Sorry, could you say that again?")
.addMessages(
ResponseMessage.newBuilder()
.setText(Text.newBuilder().addText(item.getValue()).build())
.build())
.build())
.build());
}

// Build the flow.
Flow flow =
Flow.newBuilder().setDisplayName(displayName).addAllEventHandlers(eventHandlers).build();

// Performs the create flow request.
Flow response = flowsClient.createFlow(parent, flow);

// TODO : Uncomment if you want to print response
// System.out.format("Flow created: %s\n", response.toString());
flowsClient.shutdown();
return response;
}
}
}
// [END dialogflow_cx_create_flow]
86 changes: 86 additions & 0 deletions dialogflow-cx/src/main/java/dialogflow/cx/CreateIntent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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 dialogflow.cx;

// [START dialogflow_cx_create_intent]

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.cx.v3beta1.AgentName;
import com.google.cloud.dialogflow.cx.v3beta1.Intent;
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase;
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase.Part;
import com.google.cloud.dialogflow.cx.v3beta1.IntentsClient;
import com.google.cloud.dialogflow.cx.v3beta1.IntentsSettings;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CreateIntent {

// Create an intent of the given intent type.
public static Intent createIntent(
String displayName,
String projectId,
String locationId,
String agentId,
List<String> trainingPhrasesParts)
throws IOException, ApiException {
IntentsSettings.Builder intentsSettingsBuilder = IntentsSettings.newBuilder();
if (locationId.equals("global")) {
intentsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
} else {
intentsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
}
IntentsSettings intentsSettings = intentsSettingsBuilder.build();

// Instantiates a client
// Note: close() needs to be called on the IntentsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
// Set the project agent name using the projectID (my-project-id), locationID (global), and
// agentID (UUID).
AgentName parent = AgentName.of(projectId, locationId, agentId);

// Build the trainingPhrases from the trainingPhrasesParts.
List<TrainingPhrase> trainingPhrases = new ArrayList<>();
for (String trainingPhrase : trainingPhrasesParts) {
trainingPhrases.add(
TrainingPhrase.newBuilder()
.addParts(Part.newBuilder().setText(trainingPhrase).build())
.setRepeatCount(1)
.build());
}

// Build the intent.
Intent intent =
Intent.newBuilder()
.setDisplayName(displayName)
.addAllTrainingPhrases(trainingPhrases)
.build();

// Performs the create intent request.
Intent response = intentsClient.createIntent(parent, intent);

// TODO : Uncomment if you want to print response
// System.out.format("Intent created: %s\n", response);

return response;
}
}
}
// [END dialogflow_cx_create_intent]
Loading