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 de672f84f04..00000000000 --- 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 705c6449477..a03209a5e7f 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 d71f756c6f5..00000000000 --- 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 63c916c80f8..0a4d0f3ce9e 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 b645ec219f3..00000000000 --- 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 66a8fdf6c3f..00000000000 --- 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 39b9dece1df..b4b1199be9c 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 c2b2534f456..0cde7dce74b 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, }, }, };