forked from GoogleCloudPlatform/dfcx-scrapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
378 lines (322 loc) · 13.2 KB
/
routes.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""A set of builder methods to create CX proto resource objects"""
# 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.
import logging
from google.cloud.dialogflowcx_v3beta1.types import Fulfillment
from google.cloud.dialogflowcx_v3beta1.types import TransitionRoute
from google.cloud.dialogflowcx_v3beta1.types import EventHandler
from dfcx_scrapi.builders.builders_common import BuildersCommon
from dfcx_scrapi.builders.fulfillments import FulfillmentBuilder
# logging config
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
class TransitionRouteBuilder(BuildersCommon):
"""Base Class for CX TransitionRoute builder."""
_proto_type = TransitionRoute
_proto_type_str = "TransitionRoute"
def __str__(self) -> str:
"""String representation of the proto_obj."""
self._check_proto_obj_attr_exist()
target_str = self._show_target()
transition_criteria = self._show_transition_criteria()
fulfillment_str = self._show_fulfillment()
return (
f"{target_str}"
f"\n{transition_criteria}"
f"\nFulfillment:\n\n{fulfillment_str}"
)
def _show_transition_criteria(self) -> str:
"""String representation for the transition criteria of proto_obj."""
self._check_proto_obj_attr_exist()
intent_str, cond_str = "Not Specified", "Not Specified"
if self.proto_obj.intent:
intent_str = self.proto_obj.intent
if self.proto_obj.condition:
cond_str = self.proto_obj.condition
return (
"Transition criteria:"
f"\n\tIntent: {intent_str}"
f"\n\tCondition: {cond_str}"
)
def _show_target(self) -> str:
"""String representation for the target of proto_obj."""
self._check_proto_obj_attr_exist()
if self.proto_obj.target_page:
target_type = "Page"
target_id = self.proto_obj.target_page
elif self.proto_obj.target_flow:
target_type = "Flow"
target_id = self.proto_obj.target_flow
else:
target_type = "Not Specified"
target_id = "None"
return f"Target: {target_type}\nTarget ID: {target_id}"
def _show_fulfillment(self) -> str:
"""String representation for the fulfillment of proto_obj."""
self._check_proto_obj_attr_exist()
fulfillment_str = ""
if self.proto_obj.trigger_fulfillment:
fulfillment_str = str(
FulfillmentBuilder(self.proto_obj.trigger_fulfillment)
)
return fulfillment_str
def show_transition_route(self, mode: str = "whole"):
"""Show the proto_obj information.
Args:
mode (str):
Specifies what part of the TransitionRoute to show.
Options:
['target', 'fulfillment',
'transition criteria' or 'conditions', 'whole']
"""
self._check_proto_obj_attr_exist()
if mode == "target":
print(self._show_target())
elif mode in ["transition criteria", "conditions"]:
print(self._show_transition_criteria())
elif mode == "fulfillment":
print(self._show_fulfillment())
elif mode == "whole":
print(self)
else:
raise ValueError(
"mode should be in"
" ['target', 'fulfillment',"
" 'transition criteria' or 'conditions', 'whole']"
)
def create_new_proto_obj(
self,
intent: str = None,
condition: str = None,
trigger_fulfillment: Fulfillment = None,
target_page: str = None,
target_flow: str = None,
overwrite: bool = False
) -> TransitionRoute:
"""Create a new TransitionRoute.
Args:
intent (str):
Indicates that the transition can only happen when the given
intent is matched.
Format:
``projects/<Project ID>/locations/<Location ID>/
agents/<Agent ID>/intents/<Intent ID>``.
At least one of ``intent`` or ``condition`` must be specified.
When both ``intent`` and ``condition`` are specified,
the transition can only happen when both are fulfilled.
condition (str):
The condition to evaluate.
See the conditions reference:
https://cloud.google.com/dialogflow/cx/docs/reference/condition
At least one of ``intent`` or ``condition`` must be specified.
When both ``intent`` and ``condition`` are specified,
the transition can only happen when both are fulfilled.
trigger_fulfillment (Fulfillment):
The fulfillment to call when the condition is satisfied.
When ``trigger_fulfillment`` and ``target`` are defined,
``trigger_fulfillment`` is executed first.
target_page (str):
The target page to transition to. Format:
``projects/<Project ID>/locations/<Location ID>/
agents/<Agent ID>/flows/<Flow ID>/pages/<Page ID>``.
At most one of ``target_page`` and ``target_flow``
can be specified at the same time.
target_flow (str):
The target flow to transition to. Format:
``projects/<Project ID>/locations/<Location ID>/
agents/<Agent ID>/flows/<Flow ID>``.
At most one of ``target_page`` and ``target_flow``
can be specified at the same time.
overwrite (bool)
Overwrite the new proto_obj if proto_obj already
contains a TransitionRoute.
Returns:
A TransitionRoute object stored in proto_obj.
"""
# Types error checking
if ((intent and not isinstance(intent, str)) or
(condition and not isinstance(condition, str)) or
(target_page and not isinstance(target_page, str)) or
(target_flow and not isinstance(target_flow, str))):
raise ValueError(
"intent, condition, target_page, and target_flow"
" if existed should be a string."
)
if (trigger_fulfillment and
not isinstance(trigger_fulfillment, Fulfillment)):
raise ValueError(
"The type of trigger_fulfillment should be a Fulfillment."
)
# Minimum requirement error checking
if not(intent or condition):
raise RuntimeError(
"At least one of `intent` or `condition` must be specified."
)
# Oneof error checking
if target_page and target_flow:
raise RuntimeError(
"At most one of `target_page` and `target_flow`"
" can be specified at the same time."
)
# `overwrite` parameter error checking
if self.proto_obj and not overwrite:
raise UserWarning(
"proto_obj already contains a TransitionRoute."
" If you wish to overwrite it, pass overwrite as True."
)
# Create the TransitionRoute
if overwrite or not self.proto_obj:
# Create empty Fulfillment in case user didn't pass any
if not trigger_fulfillment:
trigger_fulfillment = Fulfillment()
self.proto_obj = TransitionRoute(
intent=intent,
condition=condition,
trigger_fulfillment=trigger_fulfillment,
target_page=target_page,
target_flow=target_flow
)
return self.proto_obj
class EventHandlerBuilder(BuildersCommon):
"""Base Class for CX EventHandler builder."""
_proto_type = EventHandler
_proto_type_str = "EventHandler"
def __str__(self) -> str:
"""String representation of the proto_obj."""
self._check_proto_obj_attr_exist()
event_and_target_str = self._show_event_and_target()
fulfillment_str = self._show_fulfillment()
return (
f"{event_and_target_str}"
f"\nFulfillment:\n\n{fulfillment_str}"
)
def _show_event_and_target(self) -> str:
"""String representation for the target of proto_obj."""
self._check_proto_obj_attr_exist()
# Event str
event_str = f"Event: {self.proto_obj.event}"
# Target str
if self.proto_obj.target_page:
target_type = "Page"
target_id = self.proto_obj.target_page
elif self.proto_obj.target_flow:
target_type = "Flow"
target_id = self.proto_obj.target_flow
else:
target_type = "Not Specified"
target_id = "None"
return (
f"{event_str}"
f"\nTarget: {target_type}"
f"\nTarget ID: {target_id}"
)
def _show_fulfillment(self) -> str:
"""String representation for the fulfillment of proto_obj."""
self._check_proto_obj_attr_exist()
fulfillment_str = ""
if self.proto_obj.trigger_fulfillment:
fulfillment_str = str(
FulfillmentBuilder(self.proto_obj.trigger_fulfillment)
)
return fulfillment_str
def show_event_handler(self, mode: str = "whole"):
"""Show the proto_obj information.
Args:
mode (str):
Specifies what part of the EventHandler to show.
Options:
['basic' or 'target' or 'event', 'fulfillment', 'whole']
"""
self._check_proto_obj_attr_exist()
if mode in ["basic", "target", "event"]:
print(self._show_event_and_target())
elif mode == "fulfillment":
print(self._show_fulfillment())
elif mode == "whole":
print(self)
else:
raise ValueError(
"mode should be in"
" ['basic' or 'target' or 'event', 'fulfillment', 'whole']"
)
def create_new_proto_obj(
self,
event: str,
trigger_fulfillment: Fulfillment = None,
target_page: str = None,
target_flow: str = None,
overwrite: bool = False
) -> EventHandler:
"""Create a new EventHandler.
Args:
event (str):
Required. The name of the event to handle.
trigger_fulfillment (Fulfillment):
The fulfillment to call when the event occurs.
Handling webhook errors with a fulfillment enabled with webhook
could cause infinite loop. It is invalid to specify
such fulfillment for a handler handling webhooks.
target_page (str):
The target page to transition to. Format:
``projects/<Project ID>/locations/<Location ID>/
agents/<Agent ID>/flows/<Flow ID>/pages/<Page ID>``.
At most one of ``target_page`` and ``target_flow``
can be specified at the same time.
target_flow (str):
The target flow to transition to. Format:
``projects/<Project ID>/locations/<Location ID>/
agents/<Agent ID>/flows/<Flow ID>``.
At most one of ``target_page`` and ``target_flow``
can be specified at the same time.
overwrite (bool)
Overwrite the new proto_obj if proto_obj already
contains a EventHandler.
Returns:
An EventHandler object stored in proto_obj.
"""
# Types error checking
if event and not isinstance(event, str):
raise ValueError("event should be a string.")
if (trigger_fulfillment and
not isinstance(trigger_fulfillment, Fulfillment)):
raise ValueError(
"The type of trigger_fulfillment should be a Fulfillment."
)
# Oneof error checking
if target_page and target_flow:
raise RuntimeError(
"At most one of `target_page` and `target_flow`"
" can be specified at the same time."
)
# `overwrite` parameter error checking
if self.proto_obj and not overwrite:
raise UserWarning(
"proto_obj already contains an EventHandler."
" If you wish to overwrite it, pass overwrite as True."
)
# Create the EventHandler
if overwrite or not self.proto_obj:
# Create empty Fulfillment in case user didn't pass any
if not trigger_fulfillment:
trigger_fulfillment = Fulfillment()
self.proto_obj = EventHandler(
event=event,
trigger_fulfillment=trigger_fulfillment,
target_page=target_page,
target_flow=target_flow
)
return self.proto_obj