Skip to content

Commit

Permalink
docs: add page management code samples (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
galz10 authored Sep 8, 2021
1 parent 763dad5 commit 7b77bdb
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 0 deletions.
51 changes: 51 additions & 0 deletions dialogflow-cx/create-page.js
Original file line number Diff line number Diff line change
@@ -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;
});
48 changes: 48 additions & 0 deletions dialogflow-cx/delete-page.js
Original file line number Diff line number Diff line change
@@ -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;
});
47 changes: 47 additions & 0 deletions dialogflow-cx/list-page.js
Original file line number Diff line number Diff line change
@@ -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;
});
74 changes: 74 additions & 0 deletions dialogflow-cx/test/page-management.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 7b77bdb

Please sign in to comment.