-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheSoaringArtist.py
194 lines (156 loc) · 5.1 KB
/
TheSoaringArtist.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
import json
import dateutil.parser
import logging
import boto3
import uuid
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
dyn_client = boto3.client('dynamodb')
TABLE_NAME = "SoaringArtistArtCommissions"
def validate(slots):
if not slots['TypeOfOrder']:
return {
'isValid': False,
'violatedSlot': 'TypeOfOrder'
}
if not slots['Budget']:
return {
'isValid': False,
'violatedSlot': 'Budget'
}
if not slots['Timeline']:
return {
'isValid': False,
'violatedSlot': 'Timeline'
}
if not slots['ColourSchemes']:
return {
'isValid': False,
'violatedSlot': 'ColourSchemes'
}
if not slots['Purpose']:
return {
'isValid': False,
'violatedSlot': 'Purpose'
}
if not slots['Email']:
return {
'isValid': False,
'violatedSlot': 'Email'
}
if not slots['PhoneNumber']:
return {
'isValid': False,
'violatedSlot': 'PhoneNumber'
}
if not slots['PointOfContact']:
return {
'isValid': False,
'violatedSlot': 'PointOfContact'
}
if not slots['Name']:
return {
'isValid': False,
'violatedSlot': 'Name'
}
return {'isValid': True}
def lambda_handler(event,context):
slots = event['sessionState']['intent']['slots']
intent = event['sessionState']['intent']['name']
validation_result = validate(slots)
if event['invocationSource'] == 'DialogCodeHook':
if not validation_result['isValid']:
response = {
"sessionState": {
"dialogAction": {
'slotToElicit': validation_result['violatedSlot'],
"type":"ElicitSlot"
},
"intent": {
'name': intent,
'slots' : slots
}
}
}
else:
response = {
"sessionState": {
"dialogAction": {
"type": "Delegate"
},
"intent": {
'name':intent,
'slots': slots
}
}
}
elif event['invocationSource'] == 'FulfillmentCodeHook':
# Add order in Database
id = str(uuid.uuid4())
typeOfOrder = str(slots['TypeOfOrder']['value']['interpretedValue'])
budget = str(slots['Budget']['value']['interpretedValue'])
timeline = str(slots['Timeline']['value']['interpretedValue'])
colourSchemes = str(slots['ColourSchemes']['value']['interpretedValue'])
purpose = str(slots['Purpose']['value']['interpretedValue'])
email = str(slots['Email']['value']['interpretedValue'])
phoneNumber = str(slots['PhoneNumber']['value']['interpretedValue'])
pointOfContact = str(slots['PointOfContact']['value']['interpretedValue'])
name = str(slots['Name']['value']['interpretedValue'])
try:
response = dyn_client.put_item(
TableName=TABLE_NAME,
Item={
'id': {'S': id},
'TypeOfOrder': {'S': typeOfOrder},
'Budget': {'S': budget},
'Timeline': {'S': timeline},
'ColourScheme': {'S': colourSchemes},
'Purpose': {'S': purpose},
'Email': {'S': email},
'PhoneNumber': {'S': phoneNumber},
'PointOfContact': {'S': pointOfContact},
'Name': {'S': name},
}
)
except Exception as e:
logger.error("Error occurred while adding order to DynamoDB: {}".format(str(e)))
raise e
response = {
"sessionState": {
"dialogAction": {
"type": "Close"
},
"intent": {
'name': intent,
'slots': slots,
'state':'Fulfilled'
}
},
"messages": [
{
"contentType": "PlainText",
"content": "Thanks, I have placed your order, I will get in-contact with you for more information"
}
]
}
else:
print("In")
response = {
"sessionState": {
"dialogAction": {
"type": "Close"
},
"intent": {
'name': intent,
'slots': slots,
'state':'Fulfilled'
}
},
"messages": [
{
"contentType": "PlainText",
"content": "Sorry, I didn't understand what you said. Can you please rephrase your question?"
}
]
}
return response