Skip to content

Commit ab4b34d

Browse files
samples: create topic model (#49)
* samples: create topic model * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * use async * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * use filter Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent fed96eb commit ab4b34d

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
16+
'use strict';
17+
18+
function main(projectId) {
19+
// [START contactcenterinsights_create_issue_model]
20+
/**
21+
* TODO(developer): Uncomment this variable before running the sample.
22+
*/
23+
// const projectId = 'my_project_id';
24+
25+
// Imports the Contact Center Insights client.
26+
const {
27+
ContactCenterInsightsClient,
28+
} = require('@google-cloud/contact-center-insights');
29+
30+
// Instantiates a client.
31+
const client = new ContactCenterInsightsClient();
32+
33+
async function createIssueModel() {
34+
const [operation] = await client.createIssueModel({
35+
parent: client.locationPath(projectId, 'us-central1'),
36+
issueModel: {
37+
displayName: 'my-model',
38+
inputDataConfig: {
39+
filter: 'medium="CHAT"',
40+
},
41+
},
42+
});
43+
44+
// Wait for the operation to complete.
45+
const [issueModel] = await operation.promise();
46+
console.info(`Created ${issueModel.name}`);
47+
}
48+
createIssueModel();
49+
// [END contactcenterinsights_create_issue_model]
50+
}
51+
52+
process.on('unhandledRejection', err => {
53+
console.error(err.message);
54+
process.exitCode = 1;
55+
});
56+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
16+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const {after, before, describe, it} = require('mocha');
20+
const cp = require('child_process');
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
22+
23+
const {
24+
ContactCenterInsightsClient,
25+
} = require('@google-cloud/contact-center-insights');
26+
const client = new ContactCenterInsightsClient();
27+
28+
describe('CreateIssueModel', () => {
29+
const minConversationCount = 10000;
30+
const pageSize = 1000;
31+
let projectId;
32+
let conversationCount;
33+
let issueModelName;
34+
35+
before(async () => {
36+
projectId = await client.getProjectId();
37+
38+
// Check if the project has the minimum number of conversations required to create an issue model.
39+
// See https://cloud.google.com/contact-center/insights/docs/topic-model.
40+
conversationCount = 0;
41+
let pageToken = '';
42+
let listResponse;
43+
while (conversationCount < minConversationCount) {
44+
if (pageToken) {
45+
[listResponse] = await client.listConversations({
46+
parent: client.locationPath(projectId, 'us-central1'),
47+
pageSize: pageSize,
48+
pageToken: pageToken,
49+
});
50+
} else {
51+
[listResponse] = await client.listConversations({
52+
parent: client.locationPath(projectId, 'us-central1'),
53+
pageSize: pageSize,
54+
});
55+
}
56+
57+
if (!listResponse.conversations) {
58+
break;
59+
}
60+
conversationCount += listResponse.conversations.length;
61+
62+
if (!listResponse.nextPageToken) {
63+
break;
64+
}
65+
pageToken = listResponse.nextPageToken;
66+
}
67+
});
68+
69+
after(async () => {
70+
if (conversationCount >= minConversationCount) {
71+
client.deleteIssueModel({
72+
name: issueModelName,
73+
});
74+
}
75+
});
76+
77+
it('should create an issue model', async () => {
78+
if (conversationCount >= minConversationCount) {
79+
const stdout = execSync(`node ./createIssueModel.js ${projectId}`);
80+
issueModelName = stdout.slice(8);
81+
assert.match(
82+
stdout,
83+
new RegExp(
84+
'Created projects/[0-9]+/locations/us-central1/issueModels/[0-9]+'
85+
)
86+
);
87+
}
88+
});
89+
});

0 commit comments

Comments
 (0)