Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

samples: create conversation, create analysis, export data to BigQuery #11

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a846539
samples: initial create and delete conversation samples
TrucHLe Aug 3, 2021
dc776c1
Fix typo
TrucHLe Aug 3, 2021
5e6b160
Use product prefix contactcenterinsights
TrucHLe Aug 3, 2021
7185074
Change year to 2021
TrucHLe Aug 3, 2021
cf55ba5
Update GCS URI as suggested by telpirion@
TrucHLe Aug 3, 2021
d902541
Make the sample closely resemble the public Curl snippet
TrucHLe Aug 4, 2021
41b10f3
Update a comment
TrucHLe Aug 6, 2021
a86bb20
Remove an unused import in test
TrucHLe Aug 6, 2021
a836685
Move assert statement closer to var declaration
TrucHLe Aug 6, 2021
06a87f3
samples: initial create and delete analysis samples
TrucHLe Aug 6, 2021
17717e0
Remove unused import
TrucHLe Aug 9, 2021
26e17e5
Initial sample for exporting Insights data to BigQuery
TrucHLe Aug 9, 2021
97586dd
sample for creating a conversation with TTL
TrucHLe Aug 9, 2021
f8b5174
Clean up tests
TrucHLe Aug 9, 2021
37bb953
Update snippet tags
TrucHLe Aug 10, 2021
69e5d21
Merge branch 'master' into create_del_convo
TrucHLe Aug 12, 2021
0e9b50f
Clean up unused vars
TrucHLe Aug 12, 2021
a20e21c
Remove optional params from export sample
TrucHLe Aug 12, 2021
f634d81
Separate create_conversation and create_conversation_with_ttl
TrucHLe Aug 12, 2021
6ad93a0
use fstring
TrucHLe Aug 13, 2021
7e7485e
use built-in location path builder
TrucHLe Aug 13, 2021
a995b5e
Merge branch 'master' into create_del_convo
TrucHLe Aug 13, 2021
657d8ac
remove region tag from helper function
TrucHLe Aug 13, 2021
1c28181
Default TTL in create_conversation_with_ttl
TrucHLe Aug 13, 2021
0706b03
Catch GoogleAPICallError in export because this is expected
TrucHLe Aug 16, 2021
b7d87ca
ignore specific error message in export
TrucHLe Aug 16, 2021
9becfb2
Merge branch 'master' into create_del_convo
TrucHLe Aug 18, 2021
c664088
Update PR from advice in comments
TrucHLe Aug 18, 2021
9587c63
increase analysis timeout
TrucHLe Aug 19, 2021
238aeee
reorder import groups
TrucHLe Aug 19, 2021
80705d6
lint
TrucHLe Aug 19, 2021
53ed236
🦉 Updates from OwlBot
gcf-owl-bot[bot] Aug 20, 2021
5d66848
Remove old files
TrucHLe Aug 23, 2021
f7593a5
Merge branch 'master' into create_del_convo
TrucHLe Aug 27, 2021
7a13570
Autoformat files with nox
TrucHLe Aug 27, 2021
061ad40
remove utf-8
TrucHLe Aug 27, 2021
29a7bac
use pytest fixtures
TrucHLe Aug 27, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions samples/snippets/create_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# [START contactcenterinsights_create_analysis]
from google.cloud import contact_center_insights_v1


def create_analysis(conversation_name: str) -> contact_center_insights_v1.Analysis:
# Construct an analysis.
analysis = contact_center_insights_v1.Analysis()

# Call the Insights client to create an analysis.
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
analysis_operation = insights_client.create_analysis(
parent=conversation_name, analysis=analysis
)
analysis = analysis_operation.result(timeout=600000)
print(f"Created {analysis.name}")
return analysis


# [END contactcenterinsights_create_analysis]
45 changes: 45 additions & 0 deletions samples/snippets/create_conversation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# [START contactcenterinsights_create_conversation]
from google.cloud import contact_center_insights_v1


def create_conversation(
project_id: str,
transcript_uri: str = "gs://cloud-samples-data/ccai/chat_sample.json",
audio_uri: str = "gs://cloud-samples-data/ccai/voice_6912.txt",
) -> contact_center_insights_v1.Conversation:
# Construct a parent resource.
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
project_id, "us-central1"
)

# Construct a conversation.
conversation = contact_center_insights_v1.Conversation()
conversation.data_source.gcs_source.transcript_uri = transcript_uri
conversation.data_source.gcs_source.audio_uri = audio_uri
conversation.medium = contact_center_insights_v1.Conversation.Medium.CHAT

# Call the Insights client to create a conversation.
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
conversation = insights_client.create_conversation(
parent=parent, conversation=conversation
)

print(f"Created {conversation.name}")
return conversation


# [END contactcenterinsights_create_conversation]
51 changes: 51 additions & 0 deletions samples/snippets/create_conversation_with_ttl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# [START contactcenterinsights_create_conversation_with_ttl]
from google.cloud import contact_center_insights_v1
from google.protobuf import duration_pb2


def create_conversation_with_ttl(
project_id: str,
transcript_uri: str = "gs://cloud-samples-data/ccai/chat_sample.json",
audio_uri: str = "gs://cloud-samples-data/ccai/voice_6912.txt",
) -> contact_center_insights_v1.Conversation:
# Construct a parent resource.
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
project_id, "us-central1"
)

# Construct a conversation.
conversation = contact_center_insights_v1.Conversation()
conversation.data_source.gcs_source.transcript_uri = transcript_uri
conversation.data_source.gcs_source.audio_uri = audio_uri
conversation.medium = contact_center_insights_v1.Conversation.Medium.CHAT

# Construct a TTL.
ttl = duration_pb2.Duration()
ttl.seconds = 600
conversation.ttl = ttl

# Call the Insights client to create a conversation.
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
conversation = insights_client.create_conversation(
parent=parent, conversation=conversation
)

print(f"Created {conversation.name}")
return conversation


# [END contactcenterinsights_create_conversation_with_ttl]
52 changes: 52 additions & 0 deletions samples/snippets/export_data_to_bigquery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# [START contactcenterinsights_export_data_to_bigquery]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud import contact_center_insights_v1


def export_data_to_bigquery(
project_id: str,
bigquery_project_id: str,
bigquery_dataset_id: str,
bigquery_table_id: str,
) -> None:
# Construct an export request.
request = contact_center_insights_v1.ExportInsightsDataRequest()
request.parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
project_id, "us-central1"
)
request.big_query_destination.project_id = bigquery_project_id
request.big_query_destination.dataset = bigquery_dataset_id
request.big_query_destination.table = bigquery_table_id
request.filter = 'agent_id="007"'

# Call the Insights client to export data to BigQuery.
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
export_operation = insights_client.export_insights_data(request=request)

try:
export_operation.result(timeout=600000)
except GoogleAPICallError as e:
if "Long-running operation had neither response nor error set" in str(e):
# Ignore because the export operation doesn't return a response when it finishes successfully.
pass
else:
raise

print("Exported data to BigQuery")


# [END contactcenterinsights_export_data_to_bigquery]
Loading