From 652a6d596c510fc7bfa7dbe14724b44642573f19 Mon Sep 17 00:00:00 2001 From: aribray <45905583+aribray@users.noreply.github.com> Date: Thu, 16 Jun 2022 12:56:27 -0500 Subject: [PATCH] docs(samples): refactor webhook samples (#270) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * streamline and clarify webhook samples Change-Id: I80d80b42f60ac6ccc643afb7b2931916d6843813 * remove sessionInfo config Change-Id: I1b14852ad9e238e7cdc04dbb418ed5ed0fca2992 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * hardcode parameter Change-Id: Iea962f2ea2bf73ca86ce3ecd7852a290dd3a2fac * clarify parameter name Change-Id: I5c0a1c654be5133889f4838a118e5bdebe667303 * refactor configure-session-parameter-enable-agent-response sample Change-Id: I62af853767f9097e3448f60060f739ac8bb018b2 * refactor webhook samples Change-Id: I07f2addae8b452d89124d123e3c2be8042ee0292 * remove unnecessary samples Change-Id: Ia936ce14eafb9d44a67a314eb282fb93b5c96319 * change form parameter test Change-Id: Ia7aeb8117f6db4bee2111bdb5bcf6461b2700601 * change sample description Change-Id: I842e1e0bca1eec43d5437cd37575a43b48105bed * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- ...n-parameters-enable-agent-response-test.js | 90 ------------------- ...bhook-configure-session-parameters-test.js | 25 +----- ...sion-parameters-trigger-transition-test.js | 60 ------------- .../webhook-validate-form-parameter-test.js | 46 +--------- ...ession-parameters-enable-agent-response.js | 65 -------------- ...e-session-parameters-trigger-transition.js | 51 ----------- .../webhook-configure-session-parameters.js | 25 +----- .../webhook-validate-form-parameter.js | 35 ++------ 8 files changed, 14 insertions(+), 383 deletions(-) delete mode 100644 dialogflow-cx/test/webhook-configure-session-parameters-enable-agent-response-test.js delete mode 100644 dialogflow-cx/test/webhook-configure-session-parameters-trigger-transition-test.js delete mode 100644 dialogflow-cx/webhook-configure-session-parameters-enable-agent-response.js delete mode 100644 dialogflow-cx/webhook-configure-session-parameters-trigger-transition.js diff --git a/dialogflow-cx/test/webhook-configure-session-parameters-enable-agent-response-test.js b/dialogflow-cx/test/webhook-configure-session-parameters-enable-agent-response-test.js deleted file mode 100644 index de672f84f0..0000000000 --- a/dialogflow-cx/test/webhook-configure-session-parameters-enable-agent-response-test.js +++ /dev/null @@ -1,90 +0,0 @@ -// 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/dialogflow-cx/test/webhook-configure-session-parameters-test.js b/dialogflow-cx/test/webhook-configure-session-parameters-test.js index 705c644947..a03209a5e7 100644 --- a/dialogflow-cx/test/webhook-configure-session-parameters-test.js +++ b/dialogflow-cx/test/webhook-configure-session-parameters-test.js @@ -39,7 +39,7 @@ describe('configure session parameters', () => { const request = { body: { fulfillmentInfo: { - tag: 'month', + tag: 'configure-session-parameter', }, }, }; @@ -53,27 +53,6 @@ describe('configure session parameters', () => { }; 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'); + assert.include(response, 'orderNumber'); }); }); diff --git a/dialogflow-cx/test/webhook-configure-session-parameters-trigger-transition-test.js b/dialogflow-cx/test/webhook-configure-session-parameters-trigger-transition-test.js deleted file mode 100644 index d71f756c6f..0000000000 --- a/dialogflow-cx/test/webhook-configure-session-parameters-trigger-transition-test.js +++ /dev/null @@ -1,60 +0,0 @@ -// 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/dialogflow-cx/test/webhook-validate-form-parameter-test.js b/dialogflow-cx/test/webhook-validate-form-parameter-test.js index 63c916c80f..0a4d0f3ce9 100644 --- a/dialogflow-cx/test/webhook-validate-form-parameter-test.js +++ b/dialogflow-cx/test/webhook-validate-form-parameter-test.js @@ -35,55 +35,11 @@ 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, - }, - ], - }, + tag: 'invalidate-form-parameter', }, }, }; diff --git a/dialogflow-cx/webhook-configure-session-parameters-enable-agent-response.js b/dialogflow-cx/webhook-configure-session-parameters-enable-agent-response.js deleted file mode 100644 index b645ec219f..0000000000 --- a/dialogflow-cx/webhook-configure-session-parameters-enable-agent-response.js +++ /dev/null @@ -1,65 +0,0 @@ -// 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] - -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/dialogflow-cx/webhook-configure-session-parameters-trigger-transition.js b/dialogflow-cx/webhook-configure-session-parameters-trigger-transition.js deleted file mode 100644 index 66a8fdf6c3..0000000000 --- a/dialogflow-cx/webhook-configure-session-parameters-trigger-transition.js +++ /dev/null @@ -1,51 +0,0 @@ -// 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] - -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/dialogflow-cx/webhook-configure-session-parameters.js b/dialogflow-cx/webhook-configure-session-parameters.js index 39b9dece1d..b4b1199be9 100644 --- a/dialogflow-cx/webhook-configure-session-parameters.js +++ b/dialogflow-cx/webhook-configure-session-parameters.js @@ -13,36 +13,19 @@ // limitations under the License. /** - * Configures a webhook to configure new session parameters + * Configures a webhook to set a session parameter */ // [START dialogflow_cx_v3_webhook_configure_session_parameters] 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'; - } + // Session parameter configured by the webhook + const orderNumber = 123; const jsonResponse = { - fulfillment_response: { - messages: [ - { - text: { - //fulfillment text response to be sent to the agent - text: [text], - }, - }, - ], - }, sessionInfo: { parameters: { - newSessionParameter: newSessionParameter, + orderNumber: orderNumber, }, }, }; diff --git a/dialogflow-cx/webhook-validate-form-parameter.js b/dialogflow-cx/webhook-validate-form-parameter.js index c2b2534f45..0cde7dce74 100644 --- a/dialogflow-cx/webhook-validate-form-parameter.js +++ b/dialogflow-cx/webhook-validate-form-parameter.js @@ -19,47 +19,26 @@ // [START dialogflow_cx_v3_webhook_validate_form_parameter] 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'; - } + // Webhook will validate or invalidate parameter based on logic configured by the user. + // Access parameter values through the webhook request via `request.body.pageInfo.formInfo.parameterInfo[]` 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', + displayName: 'orderNumber', required: true, - state: paramState, + state: 'INVALID', + value: 123, }, ], }, }, sessionInfo: { parameters: { - // Set session parameter to null if your agent needs to reprompt the user - paramToValidate: paramToValidate, + // Set session parameter to null if the form parameter is 'INVALID' and your agent needs to reprompt the user + orderNumber: null, }, }, };