forked from googleapis/python-dialogflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticipant_management.py
103 lines (84 loc) · 4.57 KB
/
participant_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# 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.
"""Dialogflow API Python sample showing how to manage Participants.
"""
from google.cloud import dialogflow_v2beta1 as dialogflow
ROLES = ['HUMAN_AGENT', 'AUTOMATED_AGENT', 'END_USER']
# [START dialogflow_create_participant]
def create_participant(project_id, conversation_id, role):
"""Creates a participant in a given conversation.
Args:
project_id: The GCP project linked with the conversation profile.
conversation_id: Id of the conversation.
participant: participant to be created."""
client = dialogflow.ParticipantsClient()
conversation_path = dialogflow.ConversationsClient.conversation_path(
project_id, conversation_id)
if role in ROLES:
response = client.create_participant(parent=conversation_path,
participant={'role': role})
print('Participant Created.')
print('Role: {}'.format(response.role))
print('Name: {}'.format(response.name))
return response
# [END dialogflow_create_participant]
# [START dialogflow_analyze_content_text]
def analyze_content_text(project_id, conversation_id, participant_id, text):
"""Analyze text message content from a participant.
Args:
project_id: The GCP project linked with the conversation profile.
conversation_id: Id of the conversation.
participant_id: Id of the participant.
text: the text message that participant typed."""
client = dialogflow.ParticipantsClient()
participant_path = client.participant_path(project_id, conversation_id,
participant_id)
text_input = {'text': text, 'language_code': 'en-US'}
response = client.analyze_content(participant=participant_path,
text_input=text_input)
print('AnalyzeContent Response:')
print('Reply Text: {}'.format(response.reply_text))
for suggestion_result in response.human_agent_suggestion_results:
if suggestion_result.error is not None:
print('Error: {}'.format(suggestion_result.error.message))
if suggestion_result.suggest_articles_response:
for answer in suggestion_result.suggest_articles_response.article_answers:
print('Article Suggestion Answer: {}'.format(answer.title))
print('Answer Record: {}'.format(answer.answer_record))
if suggestion_result.suggest_faq_answers_response:
for answer in suggestion_result.suggest_faq_answers_response.faq_answers:
print('Faq Answer: {}'.format(answer.answer))
print('Answer Record: {}'.format(answer.answer_record))
if suggestion_result.suggest_smart_replies_response:
for answer in suggestion_result.suggest_smart_replies_response.smart_reply_answers:
print('Smart Reply: {}'.format(answer.reply))
print('Answer Record: {}'.format(answer.answer_record))
for suggestion_result in response.end_user_suggestion_results:
if suggestion_result.error:
print('Error: {}'.format(suggestion_result.error.message))
if suggestion_result.suggest_articles_response:
for answer in suggestion_result.suggest_articles_response.article_answers:
print('Article Suggestion Answer: {}'.format(answer.title))
print('Answer Record: {}'.format(answer.answer_record))
if suggestion_result.suggest_faq_answers_response:
for answer in suggestion_result.suggest_faq_answers_response.faq_answers:
print('Faq Answer: {}'.format(answer.answer))
print('Answer Record: {}'.format(answer.answer_record))
if suggestion_result.suggest_smart_replies_response:
for answer in suggestion_result.suggest_smart_replies_response.smart_reply_answers:
print('Smart Reply: {}'.format(answer.reply))
print('Answer Record: {}'.format(answer.answer_record))
return response
# [END dialogflow_analyze_content_text]