Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

docs(samples): add webhook samples #479

Merged
merged 84 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
27217aa
docs:add configure webhook to set form parameter as optional or requi…
aribray May 19, 2022
9ec52bf
docs: fix checkstyle
aribray May 19, 2022
47d91ad
fix checkstyle
aribray May 19, 2022
fec80c6
fix checkstyle
aribray May 19, 2022
7826893
fix multiline string
aribray May 19, 2022
b0ea884
fix checkstyle violations
aribray May 19, 2022
acd6659
fix checkstyle violations
aribray May 19, 2022
d9fb522
remove unused variable
aribray May 19, 2022
fedcc6b
fix checkstyle violations
aribray May 19, 2022
3b3d65f
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] May 19, 2022
aec26af
fix checkstyle violations
aribray Jun 8, 2022
984a4bd
fix checkstyle violations
aribray Jun 8, 2022
4428653
fix checkstyle violations
aribray Jun 8, 2022
f6d9d1d
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jun 8, 2022
006eda1
Merge remote-tracking branch 'origin/main' into aribray--webhook-samples
aribray Jun 14, 2022
7c35bf0
Merge branch 'aribray--webhook-samples' of github.com:aribray/java-di…
aribray Jun 14, 2022
fe9b212
remove unnecessary samples
aribray Jun 14, 2022
77ba085
add webhook configure session parameter sample and test
aribray Jun 14, 2022
16ab5b8
add webhook validate form parameter sample and test
aribray Jun 14, 2022
7bf63a0
fix checkstyle violations
aribray Jun 14, 2022
5176e43
fix checkstyle violations
aribray Jun 14, 2022
9d65836
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jun 14, 2022
d8c07b7
Merge remote-tracking branch 'origin/main' into aribray--webhook-samples
aribray Jun 28, 2022
8eaff7d
update to use gson
aribray Jun 28, 2022
cdc0857
update to use gson
aribray Jun 28, 2022
c0d615b
update webhook-optional-or-required to use gson
aribray Jun 29, 2022
6ceacda
update test
aribray Jun 29, 2022
56b3569
update to use gson
aribray Jun 29, 2022
34f6ca7
fix checkstyle violations
aribray Jun 29, 2022
bfe788a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jun 29, 2022
c6e5c95
v3beta1 to v3
aribray Jun 30, 2022
1012947
Merge branch 'aribray--webhook-samples' of github.com:aribray/java-di…
aribray Jun 30, 2022
a6340fe
add flag to kokoro build script
aribray Jun 30, 2022
e183c77
remove flag
aribray Jun 30, 2022
b8810da
add flag to kokoro build script
aribray Jun 30, 2022
b76ad64
remove flag
aribray Jun 30, 2022
9e45c04
Merge branch 'main' of github.com:googleapis/java-dialogflow-cx into …
aribray Jul 12, 2022
c3e19c7
add kokoro flag
aribray Jul 12, 2022
403f2b6
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 12, 2022
c0e58b5
Merge branch 'main' of github.com:googleapis/java-dialogflow-cx into …
aribray Jul 12, 2022
2543ccb
Merge branch 'aribray--webhook-samples' of github.com:aribray/java-di…
aribray Jul 12, 2022
52c3411
add kokoro flag
aribray Jul 12, 2022
7b3a3f9
fix formatting
aribray Jul 12, 2022
564673a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 12, 2022
775704b
remove extra gson
aribray Jul 12, 2022
f411016
Merge branch 'aribray--webhook-samples' of github.com:aribray/java-di…
aribray Jul 12, 2022
dada2ce
fix formatting
aribray Jul 12, 2022
94c7219
fix formatting
aribray Jul 12, 2022
105ffed
fix formatting
aribray Jul 12, 2022
abb1f30
restructure json format
aribray Jul 12, 2022
f9142a7
restructure json format
aribray Jul 12, 2022
17742f3
restructure json format
aribray Jul 12, 2022
9fdab11
restructure mockito stubbing
aribray Jul 14, 2022
cfb1a26
restructure mockito stubbing
aribray Jul 14, 2022
7876884
restructure mockito stubbing
aribray Jul 14, 2022
0f414eb
restructure mockito stubbing
aribray Jul 14, 2022
2af0833
restructure mocks and variables
aribray Jul 14, 2022
d4b84cc
restructure mocks and variables
aribray Jul 14, 2022
40b0839
restructure mocks and variables
aribray Jul 14, 2022
5c23b7b
fix variable names
aribray Jul 14, 2022
b915194
fix variable names
aribray Jul 14, 2022
ef7fce8
remove unnecessary mock
aribray Jul 14, 2022
a5f53a7
remove static
aribray Jul 14, 2022
5a02f34
add annotations
aribray Jul 14, 2022
3ffb3c7
add annotations
aribray Jul 14, 2022
a793ca4
add runwith import
aribray Jul 15, 2022
3be5357
restructure test and mocks
aribray Jul 15, 2022
313bb75
fix imports
aribray Jul 15, 2022
56633b2
fix imports
aribray Jul 15, 2022
e711e37
fix imports
aribray Jul 15, 2022
d5feb4c
rename test files
aribray Jul 15, 2022
4e73b3d
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 15, 2022
d6df8d3
fix imports
aribray Jul 15, 2022
ac00999
resolve conflict
aribray Jul 15, 2022
d791f79
Merge branch 'main' into aribray--webhook-samples
aribray Jul 15, 2022
abf222e
add variable
aribray Jul 15, 2022
fb87afe
Merge branch 'aribray--webhook-samples' of github.com:aribray/java-di…
aribray Jul 15, 2022
750a308
remove json files and fix checkstyle
aribray Jul 15, 2022
c8a95f6
make public
aribray Jul 15, 2022
3b2f158
remove print
aribray Jul 15, 2022
6ead0e3
add flag to kokoro build script
aribray Jul 16, 2022
64af3d9
add mock
aribray Jul 16, 2022
9f50683
move mocks
aribray Jul 16, 2022
323fa00
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ If you are using Maven, add this to your pom.xml file:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-dialogflow-cx</artifactId>
<version>0.14.1</version>
<version>0.14.2</version>
</dependency>
```

If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-dialogflow-cx:0.14.1'
implementation 'com.google.cloud:google-cloud-dialogflow-cx:0.14.2'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-dialogflow-cx" % "0.14.1"
libraryDependencies += "com.google.cloud" % "google-cloud-dialogflow-cx" % "0.14.2"
```

## Authentication
Expand Down Expand Up @@ -79,6 +79,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-dialogflow-cx

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Configure Webhook To Set Form Parameters As Optional Or Required | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java) |
| Create Agent | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java) |
| Create Flow | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java) |
| Create Intent | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java) |
Expand All @@ -99,6 +100,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-dialogflow-cx
| List Test Case Results | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java) |
| List Training Phrases | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java) |
| Update Intent | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java) |
| Webhook Configure Session Parameters | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java) |
| Webhook Validate Form Parameter | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java) |



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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);

System.out.println("Response Object: \n");
System.out.println(jsonResponseObject.toString());

/* {
* "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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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
// Configures a webhook to set a session parameter

// [START dialogflow_cx_v3_webhook_configure_session_parameters]

// 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.JsonObject;
import java.io.BufferedWriter;

public class WebhookConfigureSessionParameters implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonObject orderParameter = new JsonObject();
orderParameter.addProperty("order_number", "12345");

JsonObject parameterObject = new JsonObject();
parameterObject.add("parameters", orderParameter);

// Creates webhook response object
JsonObject webhookResponse = new JsonObject();
webhookResponse.add("session_info", parameterObject);

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

System.out.println("Session Parameter Info: \n");
System.out.println(jsonResponseObject.toString());

/** { "session_info": { "parameters": { "order_number": "12345" } } } */
BufferedWriter writer = response.getWriter();
// Sends the webhookResponseObject
writer.write(jsonResponseObject.toString());
}
}
// [END dialogflow_cx_v3_webhook_configure_session_parameters]
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 WebhookValidateFormParameter implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonObject sessionInfo = new JsonObject();
JsonObject sessionParameter = new JsonObject();

sessionParameter.addProperty("order_number", "null");
sessionInfo.add("parameters", sessionParameter);

JsonObject parameterObject = new JsonObject();
parameterObject.addProperty("display_name", "order_number");
parameterObject.addProperty("required", "true");
parameterObject.addProperty("state", "INVALID");
parameterObject.addProperty("value", "123");

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

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

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

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

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

System.out.println("Response Object: \n");
System.out.println(jsonResponseObject.toString());
/**
* { "page_info": { "form_info": { "parameter_info": [ { "display_name": "order_number",
* "required": "true", "state": "INVALID", "value": "123" } ] } }, "session_info": {
* "parameters": { "order_number": "null" } } }
*/
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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;

import com.google.cloud.dialogflow.cx.v3beta1.WebhookRequest;
import com.google.cloud.dialogflow.cx.v3beta1.WebhookRequest.FulfillmentInfo;
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.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT {

@Mock HttpRequest request;
@Mock HttpResponse response;

BufferedReader jsonReader;
StringReader stringReader;
BufferedWriter writerOut;
StringWriter responseOut;

@Before
public void beforeTest() throws IOException {
MockitoAnnotations.initMocks(this);

stringReader = new StringReader("{'fulfillmentInfo': {'tag': 'validate-form-parameter'}}");
jsonReader = new BufferedReader(stringReader);

responseOut = new StringWriter();
writerOut = new BufferedWriter(responseOut);

when(request.getReader()).thenReturn(jsonReader);
when(response.getWriter()).thenReturn(writerOut);
}

@Test
public void helloHttp_bodyParamsPost() throws IOException, Exception {

FulfillmentInfo fulfillmentInfo =
FulfillmentInfo.newBuilder().setTag("configure-session-parameters").build();

WebhookRequest webhookRequest =
WebhookRequest.newBuilder().setFulfillmentInfo(fulfillmentInfo).build();

new ConfigureWebhookToSetFormParametersAsOptionalOrRequired().service(request, response);
writerOut.flush();

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);

JsonObject webhookResponse = new JsonObject();
webhookResponse.add("page_info", formInfo);

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

assertThat(responseOut.toString()).isEqualTo(expectedResponse);
}
}
Loading