Skip to content

Commit f90d2d9

Browse files
authored
samples: create conversation with TTL (#43)
1 parent b0c37c4 commit f90d2d9

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
# http://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+
# Create a conversation with a TTL.
16+
# [START contactcenterinsights_create_conversation_with_ttl]
17+
from google.cloud import contact_center_insights_v1
18+
from google.protobuf import duration_pb2
19+
20+
21+
def create_conversation_with_ttl(
22+
project_id: str,
23+
transcript_uri: str = "gs://cloud-samples-data/ccai/chat_sample.json",
24+
audio_uri: str = "gs://cloud-samples-data/ccai/voice_6912.txt",
25+
) -> contact_center_insights_v1.Conversation:
26+
# Construct a parent resource.
27+
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
28+
project_id, "us-central1"
29+
)
30+
31+
# Construct a conversation.
32+
conversation = contact_center_insights_v1.Conversation()
33+
conversation.data_source.gcs_source.transcript_uri = transcript_uri
34+
conversation.data_source.gcs_source.audio_uri = audio_uri
35+
conversation.medium = contact_center_insights_v1.Conversation.Medium.CHAT
36+
37+
# Construct a TTL.
38+
ttl = duration_pb2.Duration()
39+
ttl.seconds = 86400
40+
conversation.ttl = ttl
41+
42+
# Call the Insights client to create a conversation.
43+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
44+
conversation = insights_client.create_conversation(
45+
parent=parent, conversation=conversation
46+
)
47+
48+
print(f"Created {conversation.name}")
49+
return conversation
50+
51+
52+
# [END contactcenterinsights_create_conversation_with_ttl]

contact-center-insights/snippets/create_issue_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create_issue_model(project_id: str) -> contact_center_insights_v1.IssueModel
2525
# Construct an issue model.
2626
issue_model = contact_center_insights_v1.IssueModel()
2727
issue_model.display_name = "my-model"
28-
issue_model.input_data_config.filter = "medium=\"CHAT\""
28+
issue_model.input_data_config.filter = 'medium="CHAT"'
2929

3030
# Call the Insights client to create an issue model.
3131
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()

contact-center-insights/snippets/noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019 Google LLC
1+
# Copyright 2021 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

contact-center-insights/snippets/set_project_ttl.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ def set_project_ttl(project_id: str) -> None:
5050
)
5151
)
5252

53+
5354
# [END contactcenterinsights_set_project_ttl]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
# http://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+
import google.auth
16+
17+
from google.cloud import contact_center_insights_v1
18+
19+
import pytest
20+
21+
import create_conversation_with_ttl
22+
23+
24+
@pytest.fixture
25+
def project_id():
26+
_, project_id = google.auth.default()
27+
return project_id
28+
29+
30+
@pytest.fixture
31+
def conversation_resource(project_id):
32+
# Create a conversation
33+
conversation = create_conversation_with_ttl.create_conversation_with_ttl(project_id)
34+
yield conversation
35+
36+
# Delete the conversation.
37+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
38+
insights_client.delete_conversation(name=conversation.name)
39+
40+
41+
def test_create_conversation_with_ttl(capsys, conversation_resource):
42+
conversation = conversation_resource
43+
out, err = capsys.readouterr()
44+
assert "Created {}".format(conversation.name) in out

0 commit comments

Comments
 (0)