Skip to content

Commit 40d1263

Browse files
authored
samples: create conversation (#42)
1 parent 26edca8 commit 40d1263

File tree

2 files changed

+89
-0
lines changed

2 files changed

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