From 3c6b041825c68f8db345505c171d47e2b7bb7b70 Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Fri, 1 Oct 2021 15:52:56 -0700 Subject: [PATCH] docs: added webhook sample (#876) --- dialogflow/system-test/webhook.test.js | 46 ++++++++++++++++++ dialogflow/webhook.js | 64 ++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 dialogflow/system-test/webhook.test.js create mode 100644 dialogflow/webhook.js diff --git a/dialogflow/system-test/webhook.test.js b/dialogflow/system-test/webhook.test.js new file mode 100644 index 0000000000..d5d8bd5082 --- /dev/null +++ b/dialogflow/system-test/webhook.test.js @@ -0,0 +1,46 @@ +// 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'); + +const request = { + body: { + queryResult: { + intent: { + name: 'projects/galstarter-316823/agent/intents/00c2877d-2440-447f-8dc1-045623a55bd4', + displayName: 'Default Welcome Intent', + }, + }, + }, +}; + +describe('create agent', () => { + it('should test webhook returns correct response', async () => { + const temp = JSON.stringify(request); + let response = ''; + + const res = { + send: function (s) { + response = JSON.stringify(s); + }, + }; + + webhook.handleWebhook(JSON.parse(temp), res); + assert.include(response, 'Hello from a GCF Webhook'); + }); +}); diff --git a/dialogflow/webhook.js b/dialogflow/webhook.js new file mode 100644 index 0000000000..2548cdbf77 --- /dev/null +++ b/dialogflow/webhook.js @@ -0,0 +1,64 @@ +// 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 +// +// 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'; + +// [START dialogflow_es_webhook] + +exports.handleWebhook = (request, response) => { + const tag = request.body.queryResult.intent.displayName; + + let jsonResponse = {}; + if (tag === 'Default Welcome Intent') { + //fulfillment response to be sent to the agent if the request tag is equal to "welcome tag" + jsonResponse = { + fulfillment_messages: [ + { + text: { + //fulfillment text response to be sent to the agent + text: ['Hello from a GCF Webhook'], + }, + }, + ], + }; + } else if (tag === 'get-name') { + //fulfillment response to be sent to the agent if the request tag is equal to "welcome tag" + jsonResponse = { + fulfillment_messages: [ + { + text: { + //fulfillment text response to be sent to the agent + text: ['My name is Flowhook'], + }, + }, + ], + }; + } else { + jsonResponse = { + //fulfillment text response to be sent to the agent if there are no defined responses for the specified tag + fulfillment_messages: [ + { + text: { + ////fulfillment text response to be sent to the agent + text: [ + `There are no fulfillment responses defined for "${tag}"" tag`, + ], + }, + }, + ], + }; + } + response.send(jsonResponse); +}; +// [END dialogflow_es_webhook]