Skip to content

Commit f1feb12

Browse files
TrucHLeShabirmean
authored andcommitted
samples: create converation with TTL (#81)
1 parent 5da924a commit f1feb12

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_with_ttl]
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 com.google.protobuf.Duration;
28+
import java.io.IOException;
29+
30+
public class CreateConversationWithTtl {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "my_project_id";
35+
String transcriptUri = "gs://cloud-samples-data/ccai/chat_sample.json";
36+
String audioUri = "gs://cloud-samples-data/ccai/voice_6912.txt";
37+
38+
createConversationWithTtl(projectId, transcriptUri, audioUri);
39+
}
40+
41+
public static Conversation createConversationWithTtl(
42+
String projectId, String transcriptUri, String audioUri) throws IOException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests. After completing all of your requests, call
45+
// the "close" method on the client to safely clean up any remaining background resources.
46+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
47+
// Construct a parent resource.
48+
LocationName parent = LocationName.of(projectId, "us-central1");
49+
50+
// Construct a conversation.
51+
Conversation conversation =
52+
Conversation.newBuilder()
53+
.setDataSource(
54+
ConversationDataSource.newBuilder()
55+
.setGcsSource(
56+
GcsSource.newBuilder()
57+
.setTranscriptUri(transcriptUri)
58+
.setAudioUri(audioUri)
59+
.build())
60+
.build())
61+
.setMedium(Conversation.Medium.CHAT)
62+
.setTtl(Duration.newBuilder().setSeconds(86400).build())
63+
.build();
64+
65+
// Construct a request.
66+
CreateConversationRequest request =
67+
CreateConversationRequest.newBuilder()
68+
.setParent(parent.toString())
69+
.setConversation(conversation)
70+
.build();
71+
72+
// Call the Insights client to create a conversation.
73+
Conversation response = client.createConversation(request);
74+
System.out.printf("Created %s%n", response.getName());
75+
return response;
76+
}
77+
}
78+
}
79+
80+
// [END contactcenterinsights_create_conversation_with_ttl]
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 CreateConversationWithTtlIT {
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 testCreateConversationWithTtl() throws IOException {
71+
Conversation conversation =
72+
CreateConversationWithTtl.createConversationWithTtl(PROJECT_ID, TRANSCRIPT_URI, AUDIO_URI);
73+
conversationName = conversation.getName();
74+
assertThat(bout.toString()).contains(conversationName);
75+
}
76+
}

0 commit comments

Comments
 (0)