forked from GoogleCloudPlatform/dfcx-scrapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fulfillments.py
294 lines (241 loc) · 9.48 KB
/
fulfillments.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
"""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 typing import List, Dict, Any
from google.cloud.dialogflowcx_v3beta1.types import Fulfillment
from google.cloud.dialogflowcx_v3beta1.types import ResponseMessage
from dfcx_scrapi.builders.builders_common import BuildersCommon
from dfcx_scrapi.builders.response_messages import ResponseMessageBuilder
# logging config
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
class FulfillmentBuilder(BuildersCommon):
"""Base Class for CX Fulfillment builder."""
# TODO: ConditionalCases: def add_conditional_case(self) -> Fulfillment:
_proto_type = Fulfillment
_proto_type_str = "Fulfillment"
def __str__(self) -> str:
"""String representation of the proto_obj."""
try:
self._check_proto_obj_attr_exist()
except ValueError:
return ""
basic_info_str = self._show_basic_info()
resp_msgs_str = self._show_response_messages()
params_str = self._show_parameters()
return (
f"Fulfillment Basic Information:\n{'-'*20}\n{basic_info_str}"
f"\n\n\nFulfillment ResponseMessages:\n{'-'*20}\n{resp_msgs_str}"
f"\n\n\nFulfillment Parameters:\n{'-'*20}\n{params_str}"
)
def _show_basic_info(self) -> str:
"""String representation for the basic information of proto_obj."""
self._check_proto_obj_attr_exist()
partial_resp = self.proto_obj.return_partial_responses
return (
f"webhook: {self.proto_obj.webhook}"
f"\ntag: {self.proto_obj.tag}"
f"\nreturn_partial_responses: {partial_resp}"
)
def _show_parameters(self) -> str:
"""String representation for the parameters presets of proto_obj."""
self._check_proto_obj_attr_exist()
return "\n".join([
f"{param.parameter}: {param.value if param.value else 'null'}"
for param in self.proto_obj.set_parameter_actions
])
def _show_response_messages(self) -> str:
"""String representation of response messages in proto_obj."""
self._check_proto_obj_attr_exist()
return "\n".join([
f"ResponseMessage {i+1}:\n{str(ResponseMessageBuilder(msg))}"
for i, msg in enumerate(self.proto_obj.messages)
])
def show_fulfillment(self, mode: str = "whole"):
"""Show the proto_obj information.
Args:
mode (str):
Specifies what part of the fulfillment to show.
Options:
['basic', 'parameters',
'messages' or 'response messages', 'whole']
"""
self._check_proto_obj_attr_exist()
if mode == "basic":
print(self._show_basic_info())
elif mode == "parameters":
print(self._show_parameters())
elif mode in ["messages", "response messages"]:
print(self._show_response_messages())
elif mode == "whole":
print(self)
else:
raise ValueError(
"mode should be in"
"['basic', 'parameters',"
" 'messages' or 'response messages', 'whole']"
)
def create_new_proto_obj(
self,
webhook: str = None,
tag: str = None,
return_partial_responses: bool = False,
overwrite: bool = False
) -> Fulfillment:
"""Create a new Fulfillment.
Args:
webhook (str):
The webhook to call. Format:
``projects/<Project ID>/locations/<Location ID>/agents
/<Agent ID>/webhooks/<Webhook ID>``.
tag (str):
The tag is typically used by
the webhook service to identify which fulfillment is being
called, but it could be used for other purposes. This field
is required if ``webhook`` is specified.
return_partial_responses (bool):
Whether Dialogflow should return currently
queued fulfillment response messages in
streaming APIs. If a webhook is specified, it
happens before Dialogflow invokes webhook.
overwrite (bool)
Overwrite the new proto_obj if proto_obj already
contains a Fulfillment.
Returns:
A Fulfillment object stored in proto_obj.
"""
# Types error checking
if (return_partial_responses and
not isinstance(return_partial_responses, bool)):
raise ValueError(
"return_partial_responses should be bool."
)
if ((webhook and not isinstance(webhook, str)) or
(tag and not isinstance(tag, str))):
raise ValueError(
"webhook and tag should be string."
)
# webhook with tag presence check
if webhook and not tag:
raise ValueError(
"tag is required when webhook is specified."
)
# `overwrite` parameter error checking
if self.proto_obj is not None and not overwrite:
raise UserWarning(
"proto_obj already contains a Fulfillment."
" If you wish to overwrite it, pass overwrite as True."
)
# Create the fulfillment
if overwrite or not self.proto_obj:
self.proto_obj = Fulfillment(
webhook=webhook,
return_partial_responses=return_partial_responses,
tag=tag
)
return self.proto_obj
def add_response_message(
self,
response_message: ResponseMessage
) -> Fulfillment:
"""Add a rich message response to present to the user.
Args:
response_message (ResponseMessage):
The ResponseMessage to add to the Fulfillment.
Refer to `builders.response_message.ResponseMessageBuilder`
to build one.
Returns:
A Fulfillment object stored in proto_obj
"""
self._check_proto_obj_attr_exist()
if not isinstance(response_message, ResponseMessage):
raise ValueError(
"`response_message` type should be ResponseMessage."
)
self.proto_obj.messages.append(response_message)
return self.proto_obj
def add_parameter_presets(
self,
parameter_map: Dict[str, Any]
) -> Fulfillment:
"""Set parameter values before executing the webhook.
Args:
parameter_map (Dict[str, Any]):
A dictionary that represents parameters as keys
and the parameter values as it's values.
A `None` value clears the parameter.
Returns:
A Fulfillment object stored in proto_obj
"""
self._check_proto_obj_attr_exist()
# Type error checking
if isinstance(parameter_map, dict):
if not all(isinstance(k, str) for k in parameter_map.keys()):
raise ValueError(
"Only strings are allowed as"
" dictionary keys in `parameter_map`."
)
for param, val in parameter_map.items():
self.proto_obj.set_parameter_actions.append(
Fulfillment.SetParameterAction(parameter=param, value=val)
)
return self.proto_obj
else:
raise ValueError(
"`parameter_map` should be a dictionary."
)
def remove_parameter_presets(
self,
parameters: List[str]
) -> Fulfillment:
"""Remove parameter values from the fulfillment.
Args:
parameters (List[str]):
A list of parameters that should be removed from the fulfillment.
Only strings are allowed.
Returns:
A Fulfillment object stored in proto_obj
"""
self._check_proto_obj_attr_exist()
# Type error checking
if isinstance(parameters, list):
if not all(isinstance(p, str) for p in parameters):
raise ValueError(
"Only strings are allowed as in parameters."
)
new_params = []
for param in self.proto_obj.set_parameter_actions:
if param.parameter in parameters:
continue
new_params.append(param)
self.proto_obj.set_parameter_actions = new_params
return self.proto_obj
else:
raise ValueError(
"parameter_map should be a list of strings."
)
def has_webhook(self) -> bool:
"""Check whether the Fulfillment in proto_obj uses a Webhook.
Returns:
True if proto_obj uses a Webhook and False otherwise
"""
try:
self._check_proto_obj_attr_exist()
except ValueError:
return False
return bool(self.proto_obj.webhook)