forked from GoogleCloudPlatform/dfcx-scrapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaker_util.py
214 lines (177 loc) · 7.45 KB
/
maker_util.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""Methods for creating CX object types"""
# Copyright 2023 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
#
# https://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.
# DEPRECATION WARNING!
# THIS CLASS WILL BE DEPRECATED IN A FUTURE VERSION OF SCRAPI POST v1.5.1
# PLEASE NOTE THAT THE /builders CLASSES WILL TAKE PRECEDENCE OVER THIS CLASS
import logging
from google.cloud.dialogflowcx_v3beta1 import types
# logging config
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
SCOPES = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/dialogflow",
]
class MakerUtil:
"""Util class to create CX objects like transition routes"""
@classmethod
def make_generic(cls, obj, obj_type, default, conditionals=None):
if conditionals is None:
conditionals = {}
if isinstance(obj, obj_type):
return obj
elif isinstance(obj, dict):
obj_ins = obj_type()
for key, value in obj.items():
if key in conditionals.keys():
func = conditionals[key]
out = func(value)
setattr(obj_ins, key, out)
else:
print(value)
setattr(obj_ins, key, value)
return obj_ins
elif isinstance(obj, str):
dic = {
"unspecified": 0,
"map": 1,
"list": 2,
"regexp": 3,
"default": 1,
}
t = dic.get(obj.lower())
if t:
return obj_type(t)
else:
return default
else:
return default
@classmethod
def make_seq(cls, obj, obj_type, default, conditionals=None):
if conditionals is None:
conditionals = {}
assert isinstance(obj, list)
obj_list = []
for x in obj:
obj_list.append(cls.make_generic(
x, obj_type, default, conditionals))
return obj_list
@classmethod
def make_transition_route(cls, obj=None, **kwargs):
"""Creates a single Transition Route object for Dialogflow CX.
Transition routes are used to navigate a user from page to page, or
page to flow in Dialogflow CX. Routes can be part of a Page object or
they can also be associated with Route Groups. In either case, the
structure of the Route is the same. This method allows the user to
create a single Route object that can be used interchangeably with
Pages or Route Groups as needed.
Note: if no args are provided, a blank Route object will be created.
Args:
obj: (Optional) an existing Route object can be provided if the
user wants to modify or duplicate the object.
Keyword Args:
intent, (str): The UUID of the Intent to route to
condition, (str): The condition to evaluate on the route
target_page, (str): The UUID of the target page to transition to
target_flow, (str): The UUID of the target flow to transition to
trigger_fulfillment, (obj): Requires an object in the format of type
<google.cloud.dialogflowcx_v3beta1.types.fulfillment.Fulfillment>
Returns:
Route object of type
<google.cloud.dialogflowcx_v3beta1.types.page.TransitionRoute>
"""
if obj:
route = obj
# make sure the route name is cleared if this is a copy of
# another existing route object
route.name = ""
else:
route = types.page.TransitionRoute()
# Set route attributes to args
for key, value in kwargs.items():
if key == "trigger_fulfillment":
tf = cls.make_trigger_fulfillment(value)
setattr(route, key, tf)
else:
setattr(route, key, value)
return route
@classmethod
def make_trigger_fulfillment(
cls, messages=None, webhook_id=None, webhook_tag=None
):
"""Creates a single Fulfillment object for Dialogflow CX.
Fulfillments are used as part of Transition Routes to add Dialogue
messages back to the user, trigger webhooks, set parameter presets,
and enable IVR options where applicable.
Note: if no args are provided, a blank Fulfillment object will be
returned.
Args:
messages: Optional list of Dialogue messages to send
webhook_id, (str): (Optional)
The UUID of the Dialogflow CX webhook to trigger
when the Fulfillment is triggered by the conversation.
webhook_tag, (str): (Required if webhook_id is provided)
User defined tag associated with
Returns:
Fulfillment object of type
<google.cloud.dialogflowcx_v3beta1.types.fulfillment.Fulfillment>
"""
fulfillment = types.fulfillment.Fulfillment()
if messages:
response_message = types.response_message.ResponseMessage()
message_text = response_message.Text()
message_text.text = messages
response_message.text = message_text
fulfillment.messages = [response_message]
if webhook_id:
fulfillment.webhook = webhook_id
if not webhook_tag:
logging.info(
"webhook_tag is required when specifying webhook_id")
return None
else:
fulfillment.tag = webhook_tag
# print(fulfillment)
return fulfillment
@classmethod
def set_entity_type_attr(cls, entity_type, kwargs):
for key, value in kwargs.items():
if key == "kind":
kind = types.entity_type.EntityType.Kind
obj = cls.make_generic(value, kind, kind(0))
setattr(entity_type, key, obj)
# For the auto expansion mode case create helper object to set at
# entity_type attribute
elif key == "auto_expansion_mode":
aem = types.entity_type.EntityType.AutoExpansionMode
obj = cls.make_generic(value, aem, aem(1))
setattr(entity_type, key, obj)
# For the entities case iterate over dictionary and assign key value
# pairs to entity type elements of entities list
elif key == "entities":
entity = types.entity_type.EntityType.Entity
obj = cls.make_seq(value, entity, entity())
setattr(entity_type, key, obj)
# For the excluded phrases case assign value to the excluded phrase
# object then set as the entity_type attribute
elif key == "excluded_phrases":
ep = types.entity_type.EntityType.ExcludedPhrase
obj = cls.make_seq(value, ep, ep())
setattr(entity_type, key, obj)
else:
setattr(entity_type, key, value)