From 709dc6d88d768a9b553187787e9fb3808be8d991 Mon Sep 17 00:00:00 2001 From: aribray <45905583+aribray@users.noreply.github.com> Date: Fri, 29 Apr 2022 16:10:07 -0700 Subject: [PATCH] feat: add Webhook samples (#259) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add webhook-configure-session-parameter-enable-agent-response sample and test Change-Id: Ia8593160ed33060eb497a15723b21c1221ce55c9 * add webhook-configure-session-parameter-trigger-transition sample and test Change-Id: I118abb59182879b6969018da97d04eea0b8daeb0 * add webhook-configure-optional-or-required-form-parameters sample and test Change-Id: I7cbfeb11cece7ccf873dbc7a6dd7cff9ae264ffe * add configure-session-parameters sample and test Change-Id: Ib9f1a110473751508b50259c4696593580c64a91 * fix form parameter path Change-Id: I6411000a7d0240d7552d725c2fd1049be781a9ab * add webhook-validate-form-parameter sample and test Change-Id: I82097be3fc3f91651c88b99c7431ebb602c42c66 * add cx to region tag Change-Id: I6640604512c27a341ab8e26238bf8c3fbd1e77ef * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix test Change-Id: Icc70e18b40d8684c7909e8383b4c226fa94a162b * fix region tag Change-Id: I0fe3849c0eaf12eaf247088993898cbb47dace44 Co-authored-by: Owl Bot --- README.md | 5 + samples/README.md | 90 ++++++++++++++++ ...tional-or-required-form-parameters-test.js | 100 +++++++++++++++++ ...n-parameters-enable-agent-response-test.js | 90 ++++++++++++++++ ...bhook-configure-session-parameters-test.js | 79 ++++++++++++++ ...sion-parameters-trigger-transition-test.js | 60 +++++++++++ .../webhook-validate-form-parameter-test.js | 102 ++++++++++++++++++ ...re-optional-or-required-form-parameters.js | 71 ++++++++++++ ...ession-parameters-enable-agent-response.js | 67 ++++++++++++ ...e-session-parameters-trigger-transition.js | 53 +++++++++ .../webhook-configure-session-parameters.js | 54 ++++++++++ samples/webhook-validate-form-parameter.js | 71 ++++++++++++ 12 files changed, 842 insertions(+) create mode 100644 samples/test/webhook-configure-optional-or-required-form-parameters-test.js create mode 100644 samples/test/webhook-configure-session-parameters-enable-agent-response-test.js create mode 100644 samples/test/webhook-configure-session-parameters-test.js create mode 100644 samples/test/webhook-configure-session-parameters-trigger-transition-test.js create mode 100644 samples/test/webhook-validate-form-parameter-test.js create mode 100644 samples/webhook-configure-optional-or-required-form-parameters.js create mode 100644 samples/webhook-configure-session-parameters-enable-agent-response.js create mode 100644 samples/webhook-configure-session-parameters-trigger-transition.js create mode 100644 samples/webhook-configure-session-parameters.js create mode 100644 samples/webhook-validate-form-parameter.js diff --git a/README.md b/README.md index a4ec0239..18e23c72 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,11 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-dialogflow- | Long-running-operation | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/long-running-operation.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/long-running-operation.js,samples/README.md) | | Quickstart | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) | | Update-intent | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/update-intent.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/update-intent.js,samples/README.md) | +| Webhook-configure-optional-or-required-form-parameters | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-optional-or-required-form-parameters.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-optional-or-required-form-parameters.js,samples/README.md) | +| Webhook-configure-session-parameters-enable-agent-response | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters-enable-agent-response.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters-enable-agent-response.js,samples/README.md) | +| Webhook-configure-session-parameters-trigger-transition | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters-trigger-transition.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters-trigger-transition.js,samples/README.md) | +| Webhook-configure-session-parameters | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters.js,samples/README.md) | +| Webhook-validate-form-parameter | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-validate-form-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-validate-form-parameter.js,samples/README.md) | | Webhooks | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhooks.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhooks.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index 902ca808..61638c58 100644 --- a/samples/README.md +++ b/samples/README.md @@ -25,6 +25,11 @@ * [Long-running-operation](#long-running-operation) * [Quickstart](#quickstart) * [Update-intent](#update-intent) + * [Webhook-configure-optional-or-required-form-parameters](#webhook-configure-optional-or-required-form-parameters) + * [Webhook-configure-session-parameters-enable-agent-response](#webhook-configure-session-parameters-enable-agent-response) + * [Webhook-configure-session-parameters-trigger-transition](#webhook-configure-session-parameters-trigger-transition) + * [Webhook-configure-session-parameters](#webhook-configure-session-parameters) + * [Webhook-validate-form-parameter](#webhook-validate-form-parameter) * [Webhooks](#webhooks) ## Before you begin @@ -263,6 +268,91 @@ __Usage:__ +### Webhook-configure-optional-or-required-form-parameters + +View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-optional-or-required-form-parameters.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-optional-or-required-form-parameters.js,samples/README.md) + +__Usage:__ + + +`node samples/webhook-configure-optional-or-required-form-parameters.js` + + +----- + + + + +### Webhook-configure-session-parameters-enable-agent-response + +View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters-enable-agent-response.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters-enable-agent-response.js,samples/README.md) + +__Usage:__ + + +`node samples/webhook-configure-session-parameters-enable-agent-response.js` + + +----- + + + + +### Webhook-configure-session-parameters-trigger-transition + +View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters-trigger-transition.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters-trigger-transition.js,samples/README.md) + +__Usage:__ + + +`node samples/webhook-configure-session-parameters-trigger-transition.js` + + +----- + + + + +### Webhook-configure-session-parameters + +View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters.js,samples/README.md) + +__Usage:__ + + +`node samples/webhook-configure-session-parameters.js` + + +----- + + + + +### Webhook-validate-form-parameter + +View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-validate-form-parameter.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-validate-form-parameter.js,samples/README.md) + +__Usage:__ + + +`node samples/webhook-validate-form-parameter.js` + + +----- + + + + ### Webhooks View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhooks.js). diff --git a/samples/test/webhook-configure-optional-or-required-form-parameters-test.js b/samples/test/webhook-configure-optional-or-required-form-parameters-test.js new file mode 100644 index 00000000..90b9db0d --- /dev/null +++ b/samples/test/webhook-configure-optional-or-required-form-parameters-test.js @@ -0,0 +1,100 @@ +// 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 +// +// https://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. + +'use strict'; + +// 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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const webhook = require('../webhook-configure-optional-or-required-form-parameters'); +const number = 100; + +describe('configure optional or required form parameter', () => { + it('should test that webhook sets parameter as required', async () => { + const request = { + body: { + fulfillmentInfo: { + tag: 'required', + }, + pageInfo: { + formInfo: { + parameterInfo: [ + { + displayName: 'number', + value: number, + }, + ], + }, + }, + }, + }; + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.configureOptionalFormParam(JSON.parse(temp), res); + assert.include(response, 'This parameter is required.'); + }); + + it('should test that webhook sets parameter as optional', async () => { + const request = { + body: { + fulfillmentInfo: { + tag: 'optional', + }, + pageInfo: { + formInfo: { + parameterInfo: [ + { + displayName: 'number', + value: number, + }, + ], + }, + }, + }, + }; + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.configureOptionalFormParam(JSON.parse(temp), res); + assert.include(response, 'This parameter is optional.'); + }); +}); diff --git a/samples/test/webhook-configure-session-parameters-enable-agent-response-test.js b/samples/test/webhook-configure-session-parameters-enable-agent-response-test.js new file mode 100644 index 00000000..de672f84 --- /dev/null +++ b/samples/test/webhook-configure-session-parameters-enable-agent-response-test.js @@ -0,0 +1,90 @@ +// 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 +// +// https://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. + +'use strict'; + +// 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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const webhook = require('../webhook-configure-session-parameters-enable-agent-response'); +const number = 100; + +describe('enable agent response', () => { + it('should test webhook increases value of session parameter', async () => { + const request = { + body: { + fulfillmentInfo: { + tag: 'increase number', + }, + sessionInfo: { + parameters: { + number: number, + }, + }, + }, + }; + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.enableAgentResponse(JSON.parse(temp), res); + assert.include(response, 'increased value'); + }); + + it('should test webhook decreases value of session parameter', async () => { + const request = { + body: { + fulfillmentInfo: { + tag: 'decrease number', + }, + sessionInfo: { + parameters: { + number: number, + }, + }, + }, + }; + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.enableAgentResponse(JSON.parse(temp), res); + assert.include(response, 'decreased value'); + }); +}); diff --git a/samples/test/webhook-configure-session-parameters-test.js b/samples/test/webhook-configure-session-parameters-test.js new file mode 100644 index 00000000..705c6449 --- /dev/null +++ b/samples/test/webhook-configure-session-parameters-test.js @@ -0,0 +1,79 @@ +// 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 +// +// https://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. + +'use strict'; + +// 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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const webhook = require('../webhook-configure-session-parameters'); + +describe('configure session parameters', () => { + it('should test that webhook adds new session parameter', async () => { + const request = { + body: { + fulfillmentInfo: { + tag: 'month', + }, + }, + }; + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.configureSessionParams(JSON.parse(temp), res); + assert.include(response, 'January'); + }); + + it('should test that webhook configures session parameter', async () => { + const request = { + body: { + fulfillmentInfo: { + tag: 'year', + }, + }, + }; + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.configureSessionParams(JSON.parse(temp), res); + assert.include(response, '1999'); + }); +}); diff --git a/samples/test/webhook-configure-session-parameters-trigger-transition-test.js b/samples/test/webhook-configure-session-parameters-trigger-transition-test.js new file mode 100644 index 00000000..d71f756c --- /dev/null +++ b/samples/test/webhook-configure-session-parameters-trigger-transition-test.js @@ -0,0 +1,60 @@ +// 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 +// +// https://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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const uuid = require('uuid'); +const webhook = require('../webhook-configure-session-parameters-trigger-transition.js'); + +// variables to construct path to target page +const location = 'global'; +const projectId = process.env.GCLOUD_PROJECT; +const flowId = '00000000-0000-0000-0000-000000000000'; +const pageId = `temp_page_${uuid.v4()}`; +const agentId = '4e2cb784-012c-48b2-9d8c-a877d3be3437'; +const targetPage = `projects/${projectId}/locations/${location}/agents/${agentId}/flows/${flowId}/pages/${pageId}`; + +const number = 100; + +const request = { + body: { + targetPage: targetPage, + fulfillmentInfo: { + tag: 'configure-session-parameter-trigger-transition', + }, + sessionInfo: { + parameters: { + number: number, + }, + }, + }, +}; + +describe('trigger transition', () => { + it('should test that webhook response contains target page', async () => { + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.triggerTransition(JSON.parse(temp), res); + assert.include(response, pageId); + }); +}); diff --git a/samples/test/webhook-validate-form-parameter-test.js b/samples/test/webhook-validate-form-parameter-test.js new file mode 100644 index 00000000..63c916c8 --- /dev/null +++ b/samples/test/webhook-validate-form-parameter-test.js @@ -0,0 +1,102 @@ +// 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 +// +// https://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. + +'use strict'; + +// 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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const webhook = require('../webhook-validate-form-parameter'); + +describe('configure session parameters', () => { + it('should test that webhook validates form parameter', async () => { + const number = 4; + const request = { + body: { + fulfillmentInfo: { + tag: 'valid-parameter', + }, + pageInfo: { + formInfo: { + parameterInfo: [ + { + displayName: 'number', + value: number, + }, + ], + }, + }, + }, + }; + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.validateParameter(JSON.parse(temp), res); + assert.include(response, 'VALID'); + }); + + it('should test that webhook invalidates form parameter', async () => { + const number = 150; + const request = { + body: { + fulfillmentInfo: { + tag: 'invalid-parameter', + }, + pageInfo: { + formInfo: { + parameterInfo: [ + { + displayName: 'number', + required: true, + value: number, + }, + ], + }, + }, + }, + }; + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.validateParameter(JSON.parse(temp), res); + assert.include(response, 'INVALID'); + }); +}); diff --git a/samples/webhook-configure-optional-or-required-form-parameters.js b/samples/webhook-configure-optional-or-required-form-parameters.js new file mode 100644 index 00000000..37a7b148 --- /dev/null +++ b/samples/webhook-configure-optional-or-required-form-parameters.js @@ -0,0 +1,71 @@ +// 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 +// +// https://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. + +/** + * Configures a webhook to configure new session parameters + */ + +// [START dialogflow_cx_v3_webhook_configure_optional_or_required_form_params] + +// TODO (developer): change entry point to configureOptionalFormParam in Cloud Function + +exports.configureOptionalFormParam = (request, response) => { + const tag = request.body.fulfillmentInfo.tag; + // The value of the parameter used to enable agent response + const formParameter = request.body.pageInfo.formInfo.parameterInfo[0].value; + let isRequired; + let text = ''; + + if (tag === 'optional') { + isRequired = false; + text = 'This parameter is optional.'; + } else { + isRequired = true; + text = 'This parameter is required.'; + } + + const jsonResponse = { + fulfillment_response: { + messages: [ + { + text: { + //fulfillment text response to be sent to the agent + text: [text], + }, + }, + ], + }, + pageInfo: { + formInfo: { + parameterInfo: [ + { + displayName: formParameter, + // if required: false, the agent will not reprompt for this parameter, even if the state is 'INVALID' + required: isRequired, + state: 'VALID', + }, + ], + }, + }, + // Set session parameter to null if you want to reprompt the user to enter a required parameter + sessionInfo: { + parameterInfo: { + formParameter: formParameter, + }, + }, + }; + + response.send(jsonResponse); +}; +// [END dialogflow_cx_v3_webhook_configure_optional_or_required_form_params] diff --git a/samples/webhook-configure-session-parameters-enable-agent-response.js b/samples/webhook-configure-session-parameters-enable-agent-response.js new file mode 100644 index 00000000..7c361ee4 --- /dev/null +++ b/samples/webhook-configure-session-parameters-enable-agent-response.js @@ -0,0 +1,67 @@ +// 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 +// +// https://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. + +/** + * Configures a webhook to enable an agent response. + */ + +// [START dialogflow_cx_v3_webhook_configure_session_parameters_enable_agent_response] + +// TODO (developer): change entry point to enableAgentResponse in Cloud Function + +exports.enableAgentResponse = (request, response) => { + const tag = request.body.fulfillmentInfo.tag; + // The value of the parameter used to enable agent response + let sessionParameter = request.body.sessionInfo.parameters.number; + let text = ''; + + if (tag === 'increase number') { + sessionParameter = sessionParameter += 100; + text = `The new increased value of the number parameter is ${sessionParameter}`; + } else if (tag === 'decrease number') { + sessionParameter -= 50; + text = `The new decreased value of the number parameter is ${sessionParameter}`; + } + + const jsonResponse = { + fulfillment_response: { + messages: [ + { + text: { + //fulfillment text response to be sent to the agent + text: [text], + }, + }, + ], + }, + // Webhook returns configured session parameter value + session_info: { + parameters: { + number: sessionParameter, + }, + }, + }; + + console.log( + 'Configured Parameter: ', + jsonResponse.session_info.parameters.number + ); + // Response message returned by the agent + console.log( + 'AGENT RESPONSE: ', + jsonResponse.fulfillment_response.messages[0].text.text + ); + response.send(jsonResponse); +}; +// [END dialogflow_cx_v3_webhook_configure_session_parameters_enable_agent_response] diff --git a/samples/webhook-configure-session-parameters-trigger-transition.js b/samples/webhook-configure-session-parameters-trigger-transition.js new file mode 100644 index 00000000..e7a60166 --- /dev/null +++ b/samples/webhook-configure-session-parameters-trigger-transition.js @@ -0,0 +1,53 @@ +// 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 +// +// https://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. + +/** + * Configures a webhook to trigger a page transition. This is a simple example. + */ + +// [START dialogflow_cx_v3_webhook_configure_session_parameters_trigger_transition] + +// TODO (developer): change entry point to triggerTransition in Cloud Function + +exports.triggerTransition = (request, response) => { + // The target page to transition to. + const targetPage = request.body.targetPage; // Must be format projects//locations//agents//flows//pages/ + // The value of the parameter used to trigger transition + let sessionParameter = request.body.sessionInfo.parameters.number; + + sessionParameter = sessionParameter *= 50; + const text = `We multiplied your input - the value is now ${sessionParameter}. Let's go the the next page.`; + const jsonResponse = { + target_page: targetPage, + fulfillment_response: { + messages: [ + { + text: { + //fulfillment text response to be sent to the agent + text: [text], + }, + }, + ], + }, + // Sets new value of the session parameter + session_info: { + parameters: { + number: sessionParameter, + }, + }, + }; + + response.send(jsonResponse); +}; +// [END dialogflow_cx_v3_webhook_configure_session_parameters_trigger_transition] diff --git a/samples/webhook-configure-session-parameters.js b/samples/webhook-configure-session-parameters.js new file mode 100644 index 00000000..9c9b704e --- /dev/null +++ b/samples/webhook-configure-session-parameters.js @@ -0,0 +1,54 @@ +// 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 +// +// https://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. + +/** + * Configures a webhook to configure new session parameters + */ + +// [START dialogflow_cx_v3_webhook_configure_session_parameters] + +// TODO (developer): change entry point to configureSessionParams in Cloud Function + +exports.configureSessionParams = (request, response) => { + const tag = request.body.fulfillmentInfo.tag; + let newSessionParameter; + const text = `${newSessionParameter}. I'm a session parameter configured by the webhook. The webhook's tag is ${tag}.`; + + if (tag === 'month') { + newSessionParameter = 'January'; + } else if (tag === 'year') { + newSessionParameter = '1999'; + } + + const jsonResponse = { + fulfillment_response: { + messages: [ + { + text: { + //fulfillment text response to be sent to the agent + text: [text], + }, + }, + ], + }, + sessionInfo: { + parameters: { + newSessionParameter: newSessionParameter, + }, + }, + }; + + response.send(jsonResponse); +}; +// [END dialogflow_cx_v3_webhook_configure_session_parameters] diff --git a/samples/webhook-validate-form-parameter.js b/samples/webhook-validate-form-parameter.js new file mode 100644 index 00000000..517606dc --- /dev/null +++ b/samples/webhook-validate-form-parameter.js @@ -0,0 +1,71 @@ +// 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 +// +// https://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. + +/** + * Uses a webhook to validate or invalidate form parameters. + */ + +// [START dialogflow_cx_v3_webhook_validate_form_parameter] + +// TODO (developer): change entry point to validateParameter in Cloud Function + +exports.validateParameter = (request, response) => { + // The value of the parameter to validate + let paramToValidate = request.body.pageInfo.formInfo.parameterInfo[0].value; + let text = ''; + let paramState; + + // Webhook will validate or invalidate parameter based on conditions configured by the user + if (paramToValidate > 15) { + text = 'That is too many! Please pick another number.'; + paramState = 'INVALID'; + paramToValidate = null; + } else { + text = 'That is a number I can work with!'; + paramState = 'VALID'; + } + + const jsonResponse = { + fulfillment_response: { + messages: [ + { + text: { + //fulfillment text response to be sent to the agent + text: [text], + }, + }, + ], + }, + page_info: { + form_info: { + parameter_info: [ + { + displayName: 'paramToValidate', + required: true, + state: paramState, + }, + ], + }, + }, + sessionInfo: { + parameters: { + // Set session parameter to null if your agent needs to reprompt the user + paramToValidate: paramToValidate, + }, + }, + }; + + response.send(jsonResponse); +}; +// [END dialogflow_cx_v3_webhook_validate_form_parameter]