Skip to content

Commit

Permalink
samples: create conversation, create analysis, export data to BigQuery (
Browse files Browse the repository at this point in the history
  • Loading branch information
TrucHLe authored Sep 10, 2021
1 parent 13466b6 commit 31d410a
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 1 deletion.
50 changes: 50 additions & 0 deletions contact-center-insights/createAnalysis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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';

function main(conversationName) {
// [START contactcenterinsights_create_analysis]
/**
* TODO(developer): Uncomment this variable before running the sample.
*/
// const conversationName = 'projects/my_project_id/locations/us-central1/conversations/my_conversation_id';

// Imports the Contact Center Insights client.
const {
ContactCenterInsightsClient,
} = require('@google-cloud/contact-center-insights');

// Instantiates a client.
const client = new ContactCenterInsightsClient();

async function createAnalysis() {
const [operation] = await client.createAnalysis({
parent: conversationName,
});

// Wait for the operation to complete.
const [analysis] = await operation.promise();
console.info(`Created ${analysis.name}`);
}
createAnalysis();
// [END contactcenterinsights_create_analysis]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
62 changes: 62 additions & 0 deletions contact-center-insights/createConversation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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';

function main(
projectId,
transcriptUri = 'gs://cloud-samples-data/ccai/chat_sample.json',
audioUri = 'gs://cloud-samples-data/ccai/voice_6912.txt'
) {
// [START contactcenterinsights_create_conversation]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my_project_id';
// const transcriptUri = 'gs://cloud-samples-data/ccai/chat_sample.json';
// const audioUri = 'gs://cloud-samples-data/ccai/voice_6912.txt';

// Imports the Contact Center Insights client.
const {
ContactCenterInsightsClient,
} = require('@google-cloud/contact-center-insights');

// Instantiates a client.
const client = new ContactCenterInsightsClient();

async function createConversation() {
const [conversation] = await client.createConversation({
parent: client.locationPath(projectId, 'us-central1'),
conversation: {
dataSource: {
gcsSource: {
transcriptUri: transcriptUri,
audioUri: audioUri,
},
},
medium: 'CHAT',
},
});
console.info(`Created ${conversation.name}`);
}
createConversation();
// [END contactcenterinsights_create_conversation]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
65 changes: 65 additions & 0 deletions contact-center-insights/createConversationWithTtl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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';

function main(
projectId,
transcriptUri = 'gs://cloud-samples-data/ccai/chat_sample.json',
audioUri = 'gs://cloud-samples-data/ccai/voice_6912.txt'
) {
// [START contactcenterinsights_create_conversation_with_ttl]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my_project_id';
// const transcriptUri = 'gs://cloud-samples-data/ccai/chat_sample.json';
// const audioUri = 'gs://cloud-samples-data/ccai/voice_6912.txt';

// Imports the Contact Center Insights client.
const {
ContactCenterInsightsClient,
} = require('@google-cloud/contact-center-insights');

// Instantiates a client.
const client = new ContactCenterInsightsClient();

async function createConversationWithTtl() {
const [conversation] = await client.createConversation({
parent: client.locationPath(projectId, 'us-central1'),
conversation: {
dataSource: {
gcsSource: {
transcriptUri: transcriptUri,
audioUri: audioUri,
},
},
medium: 'CHAT',
ttl: {
seconds: 600,
},
},
});
console.info(`Created ${conversation.name}`);
}
createConversationWithTtl();
// [END contactcenterinsights_create_conversation_with_ttl]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
59 changes: 59 additions & 0 deletions contact-center-insights/exportToBigquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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';

function main(projectId, bigqueryProjectId, bigqueryDataset, bigqueryTable) {
// [START contactcenterinsights_export_to_bigquery]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my_project_id';
// const bigqueryProjectId = 'my_bigquery_project_id';
// const bigqueryDataset = 'my_bigquery_dataset';
// const bigqueryTable = 'my_bigquery_table';

// Imports the Contact Center Insights client.
const {
ContactCenterInsightsClient,
} = require('@google-cloud/contact-center-insights');

// Instantiates a client.
const client = new ContactCenterInsightsClient();

async function exportToBigquery() {
const [operation] = await client.exportInsightsData({
parent: client.locationPath(projectId, 'us-central1'),
bigQueryDestination: {
projectId: bigqueryProjectId,
dataset: bigqueryDataset,
table: bigqueryTable,
},
filter: 'agent_id="007"',
});

// Wait for the operation to complete.
await operation.promise();
console.info('Exported data to BigQuery');
}
exportToBigquery();
// [END contactcenterinsights_export_to_bigquery]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
4 changes: 3 additions & 1 deletion contact-center-insights/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"test": "c8 mocha --timeout 600000 test/*.js"
},
"dependencies": {
"@google-cloud/bigquery": "^5.7.1",
"@google-cloud/contact-center-insights": "^1.4.0"
},
"devDependencies": {
"c8": "^7.1.0",
"chai": "^4.2.0",
"mocha": "^9.0.0"
"mocha": "^9.0.0",
"uuid": "^8.3.2"
}
}
65 changes: 65 additions & 0 deletions contact-center-insights/test/createAnalysis.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 {assert} = require('chai');
const {after, before, describe, it} = require('mocha');
const cp = require('child_process');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const {
ContactCenterInsightsClient,
} = require('@google-cloud/contact-center-insights');
const client = new ContactCenterInsightsClient();

describe('CreateAnalysis', () => {
let projectId;
let conversationName;

before(async () => {
projectId = await client.getProjectId();
});

after(() => {
client.deleteConversation({
name: conversationName,
force: true,
});
});

it('should create a conversation and an analysis', async () => {
const stdoutCreateConversation = execSync(
`node ./createConversation.js ${projectId}`
);
conversationName = stdoutCreateConversation.slice(8);
assert.match(
stdoutCreateConversation,
new RegExp(
'Created projects/[0-9]+/locations/us-central1/conversations/[0-9]+'
)
);

const stdoutCreateAnalysis = execSync(
`node ./createAnalysis.js ${conversationName}`
);
assert.match(
stdoutCreateAnalysis,
new RegExp(
'Created projects/[0-9]+/locations/us-central1/conversations/[0-9]+/analyses/[0-9]+'
)
);
});
});
52 changes: 52 additions & 0 deletions contact-center-insights/test/createConversation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 {assert} = require('chai');
const {after, before, describe, it} = require('mocha');
const cp = require('child_process');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const {
ContactCenterInsightsClient,
} = require('@google-cloud/contact-center-insights');
const client = new ContactCenterInsightsClient();

describe('CreateConversation', () => {
let projectId;
let conversationName;

before(async () => {
projectId = await client.getProjectId();
});

after(() => {
client.deleteConversation({
name: conversationName,
});
});

it('should create a conversation', async () => {
const stdout = execSync(`node ./createConversation.js ${projectId}`);
conversationName = stdout.slice(8);
assert.match(
stdout,
new RegExp(
'Created projects/[0-9]+/locations/us-central1/conversations/[0-9]+'
)
);
});
});
Loading

0 comments on commit 31d410a

Please sign in to comment.