-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
211 lines (164 loc) · 6.65 KB
/
utils.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
import uuid #unique id
import typing #typing for python typing
### Lex Utils Functions
def get_random_id(type: str) -> typing.Union[int, str]:
"""Generate a random id based on given type
Args:
type (str): data type for the random id
'B' -> binary
'N' -> number
'S' -> string
Returns:
id (type): random id generated by uuid
"""
#Todo need to consider binary 'B'
id = uuid.uuid4()
if(type == 'N'):
return id.int >> 64
id = str(id)
if(type == 'B'):
return id.encode('ascii')
return id
def get_intent_name(intent_request: dict) -> str:
"""Get intent's name based on the event input
Args:
intent_request (dict): Amazon Lex Event Input
Returns:
str: Intent Name
"""
return intent_request["sessionState"]["intent"]["name"]
def get_session_attributes(intent_request: dict) -> dict:
"""Get session's attributes based on the event input
Args:
intent_request (dict): Amazon Lex Event Input
Returns:
dict: Session Attributes or Empty dict
"""
sessionState = intent_request["sessionState"]
if "sessionAttributes" in sessionState:
return sessionState["sessionAttributes"]
return {}
def get_active_contexts(intent_request: dict) -> dict:
"""Get session's active contexts based on the event input
Args:
intent_request (dict): Amazon Lex Event Input
Returns:
dict: Active Contexts or Empty dict
"""
sessionState = intent_request["sessionState"]
if "activeContexts" in sessionState:
return sessionState["activeContexts"]
return {}
def get_slots(intent_request: dict) -> dict:
"""Get all the slots based on the event input
Args:
intent_request (dict): Amazon Lex Event Input
Returns:
dict: All the slots
"""
return intent_request["sessionState"]["intent"]["slots"]
def get_slot(intent_request: dict, slotName: str, original: bool = False) -> str:
"""Get the corrsponding slot based on name
Args:
intent_request (dict): Amazon Lex Event Input
slotName (str): Slot Name
original (bool, optional): Original value instead of Resolved value. Defaults to False.
Returns:
str: Corrsponding value or None
"""
slots = get_slots(intent_request)
if slots is not None and slotName in slots and slots[slotName] is not None:
val = slots[slotName]["value"]
return val["originalValue"] if original else val["interpretedValue"]
else:
return None
### Response Functions
#Todo ConfirmIntent
'''
There are 5 type of dialog actions (https://docs.aws.amazon.com/lex/latest/dg/API_runtime_DialogAction.html)
- Close - Indicates that there will not be a response from the user.
For example, the statement "Your order has been placed" does not require a response.
- ConfirmIntent - The next action is asking the user if the intent is complete and ready to be fulfilled.
This is a yes/no question such as "Place the order?"
- Delegate - The next action is determined by Amazon Lex V2.
- ElicitIntent - The next action is to elicit an intent from the user.
- ElicitSlot - The next action is to elicit a slot value from the user.
'''
def convertMessage(message: str):
if message is None:
return None
elif type(message) == str:
return [{"contentType": "PlainText", "content": message}]
#Message Array
else:
return [{"contentType": "PlainText", "content": msg} for msg in message]
def close(intent_request: dict, fulfillment_state: str, message: list) -> dict:
"""Generate response back to Amazon Lex with Dialog Action = Close
- Indicates that there will not be a response from the user.
Args:
intent_request (dict): Amazon Lex Event Input
fulfillment_state (str): The fulfillment state of the intent
message (str): The message that should be shown to the user
Returns:
dict: Response for closing dialog action
"""
intent_request["sessionState"]["intent"]["state"] = fulfillment_state
return {
"sessionState": {
"activeContexts": get_active_contexts(intent_request),
"sessionAttributes": get_session_attributes(intent_request),
"dialogAction": {
"type": "Close"
},
"intent": intent_request["sessionState"]["intent"]
},
"messages": convertMessage(message),
"sessionId": intent_request["sessionId"],
"requestAttributes": intent_request["requestAttributes"] if "requestAttributes" in intent_request else None
}
def elicit_intent(intent_request, message):
"""Generate response back to Amazon Lex with Dialog Action = Elicit Intent
- The next action is to elicit an intent from the user.
Args:
intent_request (dict): Amazon Lex Event Input
message (str): The message that should be shown to the user
Returns:
dict: Response for elicit intent dialog action
"""
intent_request["sessionState"]["intent"]["state"] = "Fulfilled"
return {
"sessionState": {
"dialogAction": {
"type": "ElicitIntent"
},
"activeContexts": get_active_contexts(intent_request),
"sessionAttributes": get_session_attributes(intent_request),
"intent": intent_request["sessionState"]["intent"]
},
"messages": convertMessage(message),
"requestAttributes": intent_request["requestAttributes"] if "requestAttributes" in intent_request else None
}
def elicit_slot(intent_request, slot_name, message):
"""Generate response back to Amazon Lex with Dialog Action = Elicit Slot
- The next action is to elicit a slot value from the user.
Args:
intent_request (_type_): Amazon Lex Event Input
slot_name (_type_): The name of the slot that should be elicited from the user.
message (_type_): The message that should be shown to the user.
Returns:
_type_: Response for elicit slot dialog action
"""
intent_request["sessionState"]["intent"]["state"] = "InProgress"
return {
"sessionState": {
"dialogAction": {
"type": "ElicitSlot",
"slotToElicit": slot_name
},
"activeContexts": get_active_contexts(intent_request),
"sessionAttributes": get_session_attributes(intent_request),
"intent": intent_request["sessionState"]["intent"]
},
"messages": convertMessage(message),
"requestAttributes": intent_request["requestAttributes"] if "requestAttributes" in intent_request else None
}