Skip to content

Commit 31d410a

Browse files
authored
samples: create conversation, create analysis, export data to BigQuery (#34)
1 parent 13466b6 commit 31d410a

9 files changed

+480
-1
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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(conversationName) {
19+
// [START contactcenterinsights_create_analysis]
20+
/**
21+
* TODO(developer): Uncomment this variable before running the sample.
22+
*/
23+
// const conversationName = 'projects/my_project_id/locations/us-central1/conversations/my_conversation_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 createAnalysis() {
34+
const [operation] = await client.createAnalysis({
35+
parent: conversationName,
36+
});
37+
38+
// Wait for the operation to complete.
39+
const [analysis] = await operation.promise();
40+
console.info(`Created ${analysis.name}`);
41+
}
42+
createAnalysis();
43+
// [END contactcenterinsights_create_analysis]
44+
}
45+
46+
process.on('unhandledRejection', err => {
47+
console.error(err.message);
48+
process.exitCode = 1;
49+
});
50+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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(
19+
projectId,
20+
transcriptUri = 'gs://cloud-samples-data/ccai/chat_sample.json',
21+
audioUri = 'gs://cloud-samples-data/ccai/voice_6912.txt'
22+
) {
23+
// [START contactcenterinsights_create_conversation]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'my_project_id';
28+
// const transcriptUri = 'gs://cloud-samples-data/ccai/chat_sample.json';
29+
// const audioUri = 'gs://cloud-samples-data/ccai/voice_6912.txt';
30+
31+
// Imports the Contact Center Insights client.
32+
const {
33+
ContactCenterInsightsClient,
34+
} = require('@google-cloud/contact-center-insights');
35+
36+
// Instantiates a client.
37+
const client = new ContactCenterInsightsClient();
38+
39+
async function createConversation() {
40+
const [conversation] = await client.createConversation({
41+
parent: client.locationPath(projectId, 'us-central1'),
42+
conversation: {
43+
dataSource: {
44+
gcsSource: {
45+
transcriptUri: transcriptUri,
46+
audioUri: audioUri,
47+
},
48+
},
49+
medium: 'CHAT',
50+
},
51+
});
52+
console.info(`Created ${conversation.name}`);
53+
}
54+
createConversation();
55+
// [END contactcenterinsights_create_conversation]
56+
}
57+
58+
process.on('unhandledRejection', err => {
59+
console.error(err.message);
60+
process.exitCode = 1;
61+
});
62+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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(
19+
projectId,
20+
transcriptUri = 'gs://cloud-samples-data/ccai/chat_sample.json',
21+
audioUri = 'gs://cloud-samples-data/ccai/voice_6912.txt'
22+
) {
23+
// [START contactcenterinsights_create_conversation_with_ttl]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'my_project_id';
28+
// const transcriptUri = 'gs://cloud-samples-data/ccai/chat_sample.json';
29+
// const audioUri = 'gs://cloud-samples-data/ccai/voice_6912.txt';
30+
31+
// Imports the Contact Center Insights client.
32+
const {
33+
ContactCenterInsightsClient,
34+
} = require('@google-cloud/contact-center-insights');
35+
36+
// Instantiates a client.
37+
const client = new ContactCenterInsightsClient();
38+
39+
async function createConversationWithTtl() {
40+
const [conversation] = await client.createConversation({
41+
parent: client.locationPath(projectId, 'us-central1'),
42+
conversation: {
43+
dataSource: {
44+
gcsSource: {
45+
transcriptUri: transcriptUri,
46+
audioUri: audioUri,
47+
},
48+
},
49+
medium: 'CHAT',
50+
ttl: {
51+
seconds: 600,
52+
},
53+
},
54+
});
55+
console.info(`Created ${conversation.name}`);
56+
}
57+
createConversationWithTtl();
58+
// [END contactcenterinsights_create_conversation_with_ttl]
59+
}
60+
61+
process.on('unhandledRejection', err => {
62+
console.error(err.message);
63+
process.exitCode = 1;
64+
});
65+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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, bigqueryProjectId, bigqueryDataset, bigqueryTable) {
19+
// [START contactcenterinsights_export_to_bigquery]
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// const projectId = 'my_project_id';
24+
// const bigqueryProjectId = 'my_bigquery_project_id';
25+
// const bigqueryDataset = 'my_bigquery_dataset';
26+
// const bigqueryTable = 'my_bigquery_table';
27+
28+
// Imports the Contact Center Insights client.
29+
const {
30+
ContactCenterInsightsClient,
31+
} = require('@google-cloud/contact-center-insights');
32+
33+
// Instantiates a client.
34+
const client = new ContactCenterInsightsClient();
35+
36+
async function exportToBigquery() {
37+
const [operation] = await client.exportInsightsData({
38+
parent: client.locationPath(projectId, 'us-central1'),
39+
bigQueryDestination: {
40+
projectId: bigqueryProjectId,
41+
dataset: bigqueryDataset,
42+
table: bigqueryTable,
43+
},
44+
filter: 'agent_id="007"',
45+
});
46+
47+
// Wait for the operation to complete.
48+
await operation.promise();
49+
console.info('Exported data to BigQuery');
50+
}
51+
exportToBigquery();
52+
// [END contactcenterinsights_export_to_bigquery]
53+
}
54+
55+
process.on('unhandledRejection', err => {
56+
console.error(err.message);
57+
process.exitCode = 1;
58+
});
59+
main(...process.argv.slice(2));

contact-center-insights/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
"test": "c8 mocha --timeout 600000 test/*.js"
1414
},
1515
"dependencies": {
16+
"@google-cloud/bigquery": "^5.7.1",
1617
"@google-cloud/contact-center-insights": "^1.4.0"
1718
},
1819
"devDependencies": {
1920
"c8": "^7.1.0",
2021
"chai": "^4.2.0",
21-
"mocha": "^9.0.0"
22+
"mocha": "^9.0.0",
23+
"uuid": "^8.3.2"
2224
}
2325
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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('CreateAnalysis', () => {
29+
let projectId;
30+
let conversationName;
31+
32+
before(async () => {
33+
projectId = await client.getProjectId();
34+
});
35+
36+
after(() => {
37+
client.deleteConversation({
38+
name: conversationName,
39+
force: true,
40+
});
41+
});
42+
43+
it('should create a conversation and an analysis', async () => {
44+
const stdoutCreateConversation = execSync(
45+
`node ./createConversation.js ${projectId}`
46+
);
47+
conversationName = stdoutCreateConversation.slice(8);
48+
assert.match(
49+
stdoutCreateConversation,
50+
new RegExp(
51+
'Created projects/[0-9]+/locations/us-central1/conversations/[0-9]+'
52+
)
53+
);
54+
55+
const stdoutCreateAnalysis = execSync(
56+
`node ./createAnalysis.js ${conversationName}`
57+
);
58+
assert.match(
59+
stdoutCreateAnalysis,
60+
new RegExp(
61+
'Created projects/[0-9]+/locations/us-central1/conversations/[0-9]+/analyses/[0-9]+'
62+
)
63+
);
64+
});
65+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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('CreateConversation', () => {
29+
let projectId;
30+
let conversationName;
31+
32+
before(async () => {
33+
projectId = await client.getProjectId();
34+
});
35+
36+
after(() => {
37+
client.deleteConversation({
38+
name: conversationName,
39+
});
40+
});
41+
42+
it('should create a conversation', async () => {
43+
const stdout = execSync(`node ./createConversation.js ${projectId}`);
44+
conversationName = stdout.slice(8);
45+
assert.match(
46+
stdout,
47+
new RegExp(
48+
'Created projects/[0-9]+/locations/us-central1/conversations/[0-9]+'
49+
)
50+
);
51+
});
52+
});

0 commit comments

Comments
 (0)