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 22 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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 @@ -92,6 +93,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,58 @@
/*
* 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.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedWriter;

public class ConfigureWebhookToSetFormParametersAsOptionalOrRequired implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonParser parser = new JsonParser();
JsonObject responseObject = null;

// Constructing the response jsonObject
responseObject =
parser
.parse(
"{\"page_info\":{\"form_info\":{\"parameter_info\":"
+ "[{\"display_name\":\"order-number\",\"required\":"
+ "\"true\",\"state\":\"VALID\"}"
+ "]}}}")
aribray marked this conversation as resolved.
Show resolved Hide resolved
.getAsJsonObject();
BufferedWriter writer = response.getWriter();

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

// Sends the responseObject
writer.write(responseObject.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,55 @@
/*
* 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.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedWriter;

public class WebhookConfigureSessionParameters implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonParser parser = new JsonParser();
JsonObject responseObject = null;

// Constructing the response jsonObject
responseObject =
parser
.parse("{\"session_info\":{\"parameters\":" + "{\"order-number\":\"12345\"}" + "}}")
aribray marked this conversation as resolved.
Show resolved Hide resolved
.getAsJsonObject();
BufferedWriter writer = response.getWriter();

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

// Sends the responseObject
writer.write(responseObject.toString());
}
}
// [END dialogflow_cx_v3_webhook_configure_session_parameters]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
// * 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_webhook_validate_form_parameter]

// TODO: Change class name to Example
// TODO: Uncomment the line bellow 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.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedWriter;

public class WebhookValidateFormParameter implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonParser parser = new JsonParser();
JsonObject responseObject = null;

// Constructing the response jsonObject
responseObject =
parser
.parse(
"{\"page_info\":{\"form_info\":{\"parameter_info\":"
+ "[{\"display_name\":\"order-number\",\"required\":"
+ "\"true\",\"state\":\"INVALID\",\"value\":\"123\"}"
+ "]}}}")
aribray marked this conversation as resolved.
Show resolved Hide resolved
.getAsJsonObject();

BufferedWriter writer = response.getWriter();

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

// Sends the responseObject
writer.write(responseObject.toString());
}
}
// [END dialogflow_cx_v3_webhook_validate_form_parameter]
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson;
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 ConfigureWebhookToSetFormParametersAsOptionalOrRequiredTest {

@Mock private HttpRequest request;
@Mock private HttpResponse response;

private BufferedWriter writerOut;
private StringWriter responseOut;
private static final Gson gson = new Gson();

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

// use an empty string as the default request content
BufferedReader reader = new BufferedReader(new StringReader(""));
when(request.getReader()).thenReturn(reader);

responseOut = new StringWriter();
writerOut = new BufferedWriter(responseOut);
when(response.getWriter()).thenReturn(writerOut);
}

@Test
public void helloHttp_bodyParamsPost() throws IOException, Exception {
String jsonString = "{'fulfillmentInfo': {'tag': 'optional-or-required'}}";

BufferedReader jsonReader = new BufferedReader(new StringReader(jsonString));

when(request.getReader()).thenReturn(jsonReader);

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

String expectedResponse =
"{\"page_info\":{\"form_info\":{\"parameter_info\":"
+ "[{\"display_name\":\"order-number\",\"required\":\"true\",\"state\":\"VALID\"}]}}}";
aribray marked this conversation as resolved.
Show resolved Hide resolved

assertThat(responseOut.toString()).isEqualTo(expectedResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
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 WebhookConfigureSessionParametersTest {

@Mock private HttpRequest request;
@Mock private HttpResponse response;

private BufferedWriter writerOut;
private StringWriter responseOut;

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

// use an empty string as the default request content
BufferedReader reader = new BufferedReader(new StringReader(""));
when(request.getReader()).thenReturn(reader);

responseOut = new StringWriter();
writerOut = new BufferedWriter(responseOut);
when(response.getWriter()).thenReturn(writerOut);
}

@Test
public void helloHttp_bodyParamsPost() throws IOException, Exception {
String jsonString = "{'fulfillmentInfo': {'tag': 'configure-session-parameter'}}";
aribray marked this conversation as resolved.
Show resolved Hide resolved

BufferedReader jsonReader = new BufferedReader(new StringReader(jsonString));

when(request.getReader()).thenReturn(jsonReader);

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

String expectedResponse =
"{\"session_info\":{\"parameters\":" + "{\"order-number\":\"12345\"}" + "}}";
aribray marked this conversation as resolved.
Show resolved Hide resolved

System.out.println(responseOut.toString());
assertThat(responseOut.toString()).isEqualTo(expectedResponse);
}
}
Loading