From 7b77bdbe442241a20cd91e98eb217fed612320c8 Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Wed, 8 Sep 2021 09:06:19 -0700 Subject: [PATCH] docs: add page management code samples (#174) --- dialogflow-cx/create-page.js | 51 +++++++++++++++ dialogflow-cx/delete-page.js | 48 ++++++++++++++ dialogflow-cx/list-page.js | 47 ++++++++++++++ dialogflow-cx/test/page-management.test.js | 74 ++++++++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 dialogflow-cx/create-page.js create mode 100644 dialogflow-cx/delete-page.js create mode 100644 dialogflow-cx/list-page.js create mode 100644 dialogflow-cx/test/page-management.test.js diff --git a/dialogflow-cx/create-page.js b/dialogflow-cx/create-page.js new file mode 100644 index 00000000000..f4b0dd2c0f0 --- /dev/null +++ b/dialogflow-cx/create-page.js @@ -0,0 +1,51 @@ +// 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'; + +const {PagesClient} = require('@google-cloud/dialogflow-cx'); + +async function main(projectId, agentId, flowId, location, displayName) { + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const agentId = 'my-agent'; + // const flowId = 'my-flow'; + // const displayName = 'my-display-name'; + // const location = 'global'; + + // [START dialogflow_cx_create_page_sample] + async function createPage(projectId, agentId, flowId, location, displayName) { + const pagesClient = new PagesClient(); + + const createPageRequest = { + parent: `projects/${projectId}/locations/${location}/agents/${agentId}/flows/${flowId}`, + page: { + displayName: displayName, + }, + }; + + const response = await pagesClient.createPage(createPageRequest); + console.log(response); + } + // [END dialogflow_cx_create_page_sample] + + await createPage(projectId, agentId, flowId, location, displayName); +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/dialogflow-cx/delete-page.js b/dialogflow-cx/delete-page.js new file mode 100644 index 00000000000..a7dcaa46f14 --- /dev/null +++ b/dialogflow-cx/delete-page.js @@ -0,0 +1,48 @@ +// 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'; + +const {PagesClient} = require('@google-cloud/dialogflow-cx'); + +async function main(projectId, agentId, flowId, pageId, location) { + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const agentId = 'my-agent'; + // const flowId = 'my-flow'; + // const pageId = 'my-page'; + // const location = 'global'; + + // [START dialogflow_cx_delete_page_sample] + async function deletePage(projectId, agentId, flowId, pageId, location) { + const pagesClient = new PagesClient(); + + const req = { + name: `projects/${projectId}/locations/${location}/agents/${agentId}/flows/${flowId}/pages/${pageId}`, + }; + + const response = await pagesClient.deletePage(req); + console.log(response); + } + // [END dialogflow_cx_delete_page_sample] + + await deletePage(projectId, agentId, flowId, pageId, location); +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/dialogflow-cx/list-page.js b/dialogflow-cx/list-page.js new file mode 100644 index 00000000000..3dbe8292aff --- /dev/null +++ b/dialogflow-cx/list-page.js @@ -0,0 +1,47 @@ +// 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'; + +const {PagesClient} = require('@google-cloud/dialogflow-cx'); + +async function main(projectId, agentId, flowId, location) { + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const agentId = 'my-agent'; + // const flowId = 'my-flow'; + // const location = 'global'; + + // [START dialogflow_cx_list_page_sample] + async function listPages(projectId, agentId, flowId, location) { + const pagesClient = new PagesClient(); + const listPageRequest = { + parent: `projects/${projectId}/locations/${location}/agents/${agentId}/flows/${flowId}`, + languageCode: 'en', + }; + + const response = await pagesClient.listPages(listPageRequest); + console.log(response); + } + // [END dialogflow_cx_list_page_sample] + + await listPages(projectId, agentId, flowId, location); +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/dialogflow-cx/test/page-management.test.js b/dialogflow-cx/test/page-management.test.js new file mode 100644 index 00000000000..34e16c8e1f5 --- /dev/null +++ b/dialogflow-cx/test/page-management.test.js @@ -0,0 +1,74 @@ +// 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 uuid = require('uuid'); +const execSync = require('child_process').execSync; +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); + +describe('should test page management functions', async () => { + const location = 'global'; + const projectId = process.env.GCLOUD_PROJECT; + const flowId = '00000000-0000-0000-0000-000000000000'; + const pageName = `temp_page_${uuid.v4()}`; + const agentID = '4e2cb784-012c-48b2-9d8c-a877d3be3437'; + let pageID = ''; + + it('should create a page', async () => { + const cmd = 'node create-page.js'; + const temp = `${cmd} ${projectId} ${agentID} ${flowId} ${location} ${pageName}`; + const output = exec(temp); + assert.include(output, pageName); + }); + + it('should list pages', async () => { + const cmd = 'node list-page.js'; + const output = exec(`${cmd} ${projectId} ${agentID} ${flowId} global`); + assert.include(output, pageName); + }); + + it('should delete a page', async () => { + const {PagesClient, protos} = require('@google-cloud/dialogflow-cx'); + const pagesClient = new PagesClient(); + const listPageRequest = + new protos.google.cloud.dialogflow.cx.v3.ListPagesRequest(); + + listPageRequest.parent = + 'projects/' + + projectId + + '/locations/' + + location + + '/agents/' + + agentID + + '/flows/' + + flowId; + listPageRequest.languageCode = 'en'; + + const response = await pagesClient.listPages(listPageRequest); + + for (let i = 0; i < response[0].length; i++) { + if (response[0][i].displayName === pageName) { + pageID = response[0][i].name.split('/')[9]; + } + } + + const cmd = 'node delete-page.js'; + const temp = `${cmd} ${projectId} ${agentID} ${flowId} ${pageID} global`; + const output = exec(temp); + assert.strictEqual(output.includes('['), true); + }); +});