Skip to content

Commit 7b77bdb

Browse files
authored
docs: add page management code samples (#174)
1 parent 763dad5 commit 7b77bdb

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed

dialogflow-cx/create-page.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {PagesClient} = require('@google-cloud/dialogflow-cx');
18+
19+
async function main(projectId, agentId, flowId, location, displayName) {
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// const projectId = 'my-project';
24+
// const agentId = 'my-agent';
25+
// const flowId = 'my-flow';
26+
// const displayName = 'my-display-name';
27+
// const location = 'global';
28+
29+
// [START dialogflow_cx_create_page_sample]
30+
async function createPage(projectId, agentId, flowId, location, displayName) {
31+
const pagesClient = new PagesClient();
32+
33+
const createPageRequest = {
34+
parent: `projects/${projectId}/locations/${location}/agents/${agentId}/flows/${flowId}`,
35+
page: {
36+
displayName: displayName,
37+
},
38+
};
39+
40+
const response = await pagesClient.createPage(createPageRequest);
41+
console.log(response);
42+
}
43+
// [END dialogflow_cx_create_page_sample]
44+
45+
await createPage(projectId, agentId, flowId, location, displayName);
46+
}
47+
48+
main(...process.argv.slice(2)).catch(err => {
49+
console.error(err);
50+
process.exitCode = 1;
51+
});

dialogflow-cx/delete-page.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {PagesClient} = require('@google-cloud/dialogflow-cx');
18+
19+
async function main(projectId, agentId, flowId, pageId, location) {
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// const projectId = 'my-project';
24+
// const agentId = 'my-agent';
25+
// const flowId = 'my-flow';
26+
// const pageId = 'my-page';
27+
// const location = 'global';
28+
29+
// [START dialogflow_cx_delete_page_sample]
30+
async function deletePage(projectId, agentId, flowId, pageId, location) {
31+
const pagesClient = new PagesClient();
32+
33+
const req = {
34+
name: `projects/${projectId}/locations/${location}/agents/${agentId}/flows/${flowId}/pages/${pageId}`,
35+
};
36+
37+
const response = await pagesClient.deletePage(req);
38+
console.log(response);
39+
}
40+
// [END dialogflow_cx_delete_page_sample]
41+
42+
await deletePage(projectId, agentId, flowId, pageId, location);
43+
}
44+
45+
main(...process.argv.slice(2)).catch(err => {
46+
console.error(err);
47+
process.exitCode = 1;
48+
});

dialogflow-cx/list-page.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {PagesClient} = require('@google-cloud/dialogflow-cx');
18+
19+
async function main(projectId, agentId, flowId, location) {
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// const projectId = 'my-project';
24+
// const agentId = 'my-agent';
25+
// const flowId = 'my-flow';
26+
// const location = 'global';
27+
28+
// [START dialogflow_cx_list_page_sample]
29+
async function listPages(projectId, agentId, flowId, location) {
30+
const pagesClient = new PagesClient();
31+
const listPageRequest = {
32+
parent: `projects/${projectId}/locations/${location}/agents/${agentId}/flows/${flowId}`,
33+
languageCode: 'en',
34+
};
35+
36+
const response = await pagesClient.listPages(listPageRequest);
37+
console.log(response);
38+
}
39+
// [END dialogflow_cx_list_page_sample]
40+
41+
await listPages(projectId, agentId, flowId, location);
42+
}
43+
44+
main(...process.argv.slice(2)).catch(err => {
45+
console.error(err);
46+
process.exitCode = 1;
47+
});
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the 'License');
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an 'AS IS' BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const uuid = require('uuid');
20+
const execSync = require('child_process').execSync;
21+
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
22+
23+
describe('should test page management functions', async () => {
24+
const location = 'global';
25+
const projectId = process.env.GCLOUD_PROJECT;
26+
const flowId = '00000000-0000-0000-0000-000000000000';
27+
const pageName = `temp_page_${uuid.v4()}`;
28+
const agentID = '4e2cb784-012c-48b2-9d8c-a877d3be3437';
29+
let pageID = '';
30+
31+
it('should create a page', async () => {
32+
const cmd = 'node create-page.js';
33+
const temp = `${cmd} ${projectId} ${agentID} ${flowId} ${location} ${pageName}`;
34+
const output = exec(temp);
35+
assert.include(output, pageName);
36+
});
37+
38+
it('should list pages', async () => {
39+
const cmd = 'node list-page.js';
40+
const output = exec(`${cmd} ${projectId} ${agentID} ${flowId} global`);
41+
assert.include(output, pageName);
42+
});
43+
44+
it('should delete a page', async () => {
45+
const {PagesClient, protos} = require('@google-cloud/dialogflow-cx');
46+
const pagesClient = new PagesClient();
47+
const listPageRequest =
48+
new protos.google.cloud.dialogflow.cx.v3.ListPagesRequest();
49+
50+
listPageRequest.parent =
51+
'projects/' +
52+
projectId +
53+
'/locations/' +
54+
location +
55+
'/agents/' +
56+
agentID +
57+
'/flows/' +
58+
flowId;
59+
listPageRequest.languageCode = 'en';
60+
61+
const response = await pagesClient.listPages(listPageRequest);
62+
63+
for (let i = 0; i < response[0].length; i++) {
64+
if (response[0][i].displayName === pageName) {
65+
pageID = response[0][i].name.split('/')[9];
66+
}
67+
}
68+
69+
const cmd = 'node delete-page.js';
70+
const temp = `${cmd} ${projectId} ${agentID} ${flowId} ${pageID} global`;
71+
const output = exec(temp);
72+
assert.strictEqual(output.includes('['), true);
73+
});
74+
});

0 commit comments

Comments
 (0)