Skip to content

Commit 5da924a

Browse files
TrucHLeShabirmean
authored andcommitted
samples: create conversation (#80)
1 parent 77bcebc commit 5da924a

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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_conversation]
20+
21+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
22+
import com.google.cloud.contactcenterinsights.v1.Conversation;
23+
import com.google.cloud.contactcenterinsights.v1.ConversationDataSource;
24+
import com.google.cloud.contactcenterinsights.v1.CreateConversationRequest;
25+
import com.google.cloud.contactcenterinsights.v1.GcsSource;
26+
import com.google.cloud.contactcenterinsights.v1.LocationName;
27+
import java.io.IOException;
28+
29+
public class CreateConversation {
30+
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "my_project_id";
34+
String transcriptUri = "gs://cloud-samples-data/ccai/chat_sample.json";
35+
String audioUri = "gs://cloud-samples-data/ccai/voice_6912.txt";
36+
37+
createConversation(projectId, transcriptUri, audioUri);
38+
}
39+
40+
public static Conversation createConversation(
41+
String projectId, String transcriptUri, String audioUri) throws IOException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests. After completing all of your requests, call
44+
// the "close" method on the client to safely clean up any remaining background resources.
45+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
46+
// Construct a parent resource.
47+
LocationName parent = LocationName.of(projectId, "us-central1");
48+
49+
// Construct a conversation.
50+
Conversation conversation =
51+
Conversation.newBuilder()
52+
.setDataSource(
53+
ConversationDataSource.newBuilder()
54+
.setGcsSource(
55+
GcsSource.newBuilder()
56+
.setTranscriptUri(transcriptUri)
57+
.setAudioUri(audioUri)
58+
.build())
59+
.build())
60+
.setMedium(Conversation.Medium.CHAT)
61+
.build();
62+
63+
// Construct a request.
64+
CreateConversationRequest request =
65+
CreateConversationRequest.newBuilder()
66+
.setParent(parent.toString())
67+
.setConversation(conversation)
68+
.build();
69+
70+
// Call the Insights client to create a conversation.
71+
Conversation response = client.createConversation(request);
72+
System.out.printf("Created %s%n", response.getName());
73+
return response;
74+
}
75+
}
76+
}
77+
78+
// [END contactcenterinsights_create_conversation]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.ContactCenterInsightsClient;
23+
import com.google.cloud.contactcenterinsights.v1.Conversation;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
33+
34+
@RunWith(JUnit4.class)
35+
public class CreateConversationIT {
36+
37+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
38+
private static final String TRANSCRIPT_URI = "gs://cloud-samples-data/ccai/chat_sample.json";
39+
private static final String AUDIO_URI = "gs://cloud-samples-data/ccai/voice_6912.txt";
40+
private ByteArrayOutputStream bout;
41+
private PrintStream out;
42+
private String conversationName;
43+
44+
private static void requireEnvVar(String varName) {
45+
assertNotNull(String.format(varName), String.format(varName));
46+
}
47+
48+
@BeforeClass
49+
public static void checkRequirements() {
50+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
51+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
bout = new ByteArrayOutputStream();
57+
out = new PrintStream(bout);
58+
System.setOut(out);
59+
}
60+
61+
@After
62+
public void tearDown() throws IOException {
63+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
64+
client.deleteConversation(conversationName);
65+
}
66+
System.setOut(null);
67+
}
68+
69+
@Test
70+
public void testCreateConversation() throws IOException {
71+
Conversation conversation =
72+
CreateConversation.createConversation(PROJECT_ID, TRANSCRIPT_URI, AUDIO_URI);
73+
conversationName = conversation.getName();
74+
assertThat(bout.toString()).contains(conversationName);
75+
}
76+
}

0 commit comments

Comments
 (0)