Skip to content

Commit d06d9e4

Browse files
TrucHLeShabirmean
authored andcommitted
samples: create analysis (#89)
1 parent f1feb12 commit d06d9e4

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.contactcenterinsights;
18+
19+
// [START contactcenterinsights_create_analysis]
20+
21+
import com.google.cloud.contactcenterinsights.v1.Analysis;
22+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
23+
import java.io.IOException;
24+
25+
public class CreateAnalysis {
26+
27+
public static void main(String[] args) throws Exception, IOException {
28+
// TODO(developer): Replace this variable before running the sample.
29+
String conversationName =
30+
"projects/my_project_id/locations/us-central1/conversations/my_conversation_id";
31+
32+
createAnalysis(conversationName);
33+
}
34+
35+
public static Analysis createAnalysis(String conversationName) throws Exception, IOException {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
40+
// Construct an analysis.
41+
Analysis analysis = Analysis.newBuilder().build();
42+
43+
// Call the Insights client to create an analysis.
44+
Analysis response = client.createAnalysisAsync(conversationName, analysis).get();
45+
System.out.printf("Created %s%n", response.getName());
46+
return response;
47+
}
48+
}
49+
}
50+
51+
// [END contactcenterinsights_create_analysis]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.contactcenterinsights;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.contactcenterinsights.v1.Analysis;
23+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
24+
import com.google.cloud.contactcenterinsights.v1.Conversation;
25+
import com.google.cloud.contactcenterinsights.v1.ConversationDataSource;
26+
import com.google.cloud.contactcenterinsights.v1.CreateConversationRequest;
27+
import com.google.cloud.contactcenterinsights.v1.DeleteConversationRequest;
28+
import com.google.cloud.contactcenterinsights.v1.GcsSource;
29+
import com.google.cloud.contactcenterinsights.v1.LocationName;
30+
import java.io.ByteArrayOutputStream;
31+
import java.io.IOException;
32+
import java.io.PrintStream;
33+
import org.junit.After;
34+
import org.junit.Before;
35+
import org.junit.BeforeClass;
36+
import org.junit.Test;
37+
import org.junit.runner.RunWith;
38+
import org.junit.runners.JUnit4;
39+
40+
@RunWith(JUnit4.class)
41+
public class CreateAnalysisIT {
42+
43+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
44+
private static final String TRANSCRIPT_URI = "gs://cloud-samples-data/ccai/chat_sample.json";
45+
private static final String AUDIO_URI = "gs://cloud-samples-data/ccai/voice_6912.txt";
46+
private ByteArrayOutputStream bout;
47+
private PrintStream out;
48+
private String conversationName;
49+
50+
private static void requireEnvVar(String varName) {
51+
assertNotNull(String.format(varName), String.format(varName));
52+
}
53+
54+
@BeforeClass
55+
public static void checkRequirements() {
56+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
57+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
58+
}
59+
60+
@Before
61+
public void setUp() throws IOException {
62+
bout = new ByteArrayOutputStream();
63+
out = new PrintStream(bout);
64+
System.setOut(out);
65+
66+
// Create a conversation.
67+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
68+
LocationName parent = LocationName.of(PROJECT_ID, "us-central1");
69+
70+
Conversation conversation =
71+
Conversation.newBuilder()
72+
.setDataSource(
73+
ConversationDataSource.newBuilder()
74+
.setGcsSource(
75+
GcsSource.newBuilder()
76+
.setTranscriptUri(TRANSCRIPT_URI)
77+
.setAudioUri(AUDIO_URI)
78+
.build())
79+
.build())
80+
.setMedium(Conversation.Medium.CHAT)
81+
.build();
82+
83+
CreateConversationRequest request =
84+
CreateConversationRequest.newBuilder()
85+
.setParent(parent.toString())
86+
.setConversation(conversation)
87+
.build();
88+
89+
Conversation response = client.createConversation(request);
90+
conversationName = response.getName();
91+
}
92+
}
93+
94+
@After
95+
public void tearDown() throws Exception, IOException {
96+
// Delete the conversation.
97+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
98+
DeleteConversationRequest request =
99+
DeleteConversationRequest.newBuilder().setName(conversationName).setForce(true).build();
100+
client.deleteConversation(request);
101+
}
102+
System.setOut(null);
103+
}
104+
105+
@Test
106+
public void testCreateAnalysis() throws Exception, IOException {
107+
Analysis analysis = CreateAnalysis.createAnalysis(conversationName);
108+
assertThat(bout.toString()).contains(analysis.getName());
109+
}
110+
}

0 commit comments

Comments
 (0)