Skip to content

Commit ad2fb2c

Browse files
samples: enable pubsub notifications (#48)
* samples: enable pubsub notifications * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * ignore new doc link Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 31d410a commit ad2fb2c

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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, topicCreateConversation, topicCreateAnalysis) {
19+
// [START contactcenterinsights_enable_pubsub_notifications]
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// const projectId = 'my_project_id';
24+
// const topicCreateConversation = 'projects/my_project_id/topics/my_topic_id';
25+
// const topicCreateAnalysis = 'projects/my_project_id/topics/my_other_topic_id';
26+
27+
// Imports the Contact Center Insights client.
28+
const {
29+
ContactCenterInsightsClient,
30+
} = require('@google-cloud/contact-center-insights');
31+
32+
// Instantiates a client.
33+
const client = new ContactCenterInsightsClient();
34+
35+
async function enablePubSubNotifications() {
36+
const [settings] = await client.updateSettings({
37+
settings: {
38+
name: client.settingsPath(projectId, 'us-central1'),
39+
pubsubNotificationSettings: {
40+
'create-conversation': topicCreateConversation,
41+
'create-analysis': topicCreateAnalysis,
42+
},
43+
},
44+
updateMask: {
45+
paths: ['pubsub_notification_settings'],
46+
},
47+
});
48+
console.info(`Enabled Pub/Sub notifications for ${settings.name}`);
49+
}
50+
enablePubSubNotifications();
51+
// [END contactcenterinsights_enable_pubsub_notifications]
52+
}
53+
54+
process.on('unhandledRejection', err => {
55+
console.error(err.message);
56+
process.exitCode = 1;
57+
});
58+
main(...process.argv.slice(2));

contact-center-insights/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"dependencies": {
1616
"@google-cloud/bigquery": "^5.7.1",
17-
"@google-cloud/contact-center-insights": "^1.4.0"
17+
"@google-cloud/contact-center-insights": "^1.4.0",
18+
"@google-cloud/pubsub": "^2.17.0"
1819
},
1920
"devDependencies": {
2021
"c8": "^7.1.0",
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+
// 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 uuid = require('uuid');
21+
const cp = require('child_process');
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
24+
const generateUuid = () => `${uuid.v4()}`.replace(/-/gi, '_');
25+
26+
const {
27+
ContactCenterInsightsClient,
28+
} = require('@google-cloud/contact-center-insights');
29+
const client = new ContactCenterInsightsClient();
30+
31+
const {PubSub} = require('@google-cloud/pubsub');
32+
const pubSub = new PubSub();
33+
34+
const conversationTopicId = `create-conversation-${generateUuid()}`;
35+
const analysisTopicId = `create-analysis-${generateUuid()}`;
36+
37+
describe('EnablePubSubNotifications', () => {
38+
let projectId;
39+
let conversationTopic;
40+
let analysisTopic;
41+
42+
before(async () => {
43+
projectId = await client.getProjectId();
44+
conversationTopic = `projects/${projectId}/topics/${conversationTopicId}`;
45+
analysisTopic = `projects/${projectId}/topics/${analysisTopicId}`;
46+
47+
// Creates Pub/Sub topics.
48+
await pubSub.createTopic(conversationTopic);
49+
await pubSub.createTopic(analysisTopic);
50+
});
51+
52+
after(async () => {
53+
// Disables Pub/Sub notifications.
54+
await client.updateSettings({
55+
settings: {
56+
name: client.settingsPath(projectId, 'us-central1'),
57+
pubsubNotificationSettings: {},
58+
},
59+
updateMask: {
60+
paths: ['pubsub_notification_settings'],
61+
},
62+
});
63+
64+
// Deletes Pub/Sub topics.
65+
await pubSub.topic(conversationTopic).delete();
66+
await pubSub.topic(analysisTopic).delete();
67+
});
68+
69+
it('should enable Pub/Sub notifications', async () => {
70+
const stdout = execSync(`node ./enablePubSubNotifications.js \
71+
${projectId} ${conversationTopic} ${analysisTopic}`);
72+
assert.match(stdout, new RegExp('Enabled Pub/Sub notifications'));
73+
});
74+
});

0 commit comments

Comments
 (0)