-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
311 lines (287 loc) · 9.25 KB
/
lambda_function.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
from __future__ import print_function
import random
import game
# constants
reprompts = ["Try something else", "Another command please", "Try again"]
# --------------- Helpers ----------------------
def build_speechlet_response(output, should_end_session):
return {
'outputSpeech': {
'type': 'SSML',
'ssml': "<speak>"+output+"</speak>"
},
'shouldEndSession': should_end_session
}
def build_response(speechlet_response):
"""
Build the full response JSON from the speechlet response
"""
return {
'version': '1.0',
'sessionAttributes': {},
'response': speechlet_response
}
# --------------- Skill behavior ------------------
def play(intent):
res = game.play(intent)
toSay = res['toSay']
finish = res['finish']
return build_response(build_speechlet_response(toSay, finish))
def reprompt(session):
return build_response(build_speechlet_response(reprompts[random.randint(0,len(reprompts))-1], False))
def start():
return build_response(build_speechlet_response(
game.play("",True), False))
def end():
return build_response(build_speechlet_response(
"Game quit", True))
def getInventory():
inv = game.getInventory()
toSay = ""
if (inv is None):
toSay = "You have diddly squat in your inventory"
else:
toSay = "You have "+ ' '.join(inv)+ " in your inventory"
return build_response(build_speechlet_response(toSay, False))
# --------------- Events ------------------
def on_session_started(session_started_request, session):
""" Called when the session starts """
print("on_session_started requestId=" + session_started_request['requestId']
+ ", sessionId=" + session['sessionId'])
def on_launch(launch_request, session):
""" Called when the user launches the skill without specifying what they want """
print("on_launch requestId=" + launch_request['requestId'] +
", sessionId=" + session['sessionId'])
# Dispatch to your skill's launch
return getStartResponse()
def on_intent(intent_request, session):
""" Called when the user specifies an intent for this skill """
print("on_intent requestId=" + intent_request['requestId'] +
", sessionId=" + session['sessionId'])
intent = intent_request['intent']
intent_name = intent_request['intent']['name']
# Dispatch to your skill's intent handlers
if intent_name == "CommandStart":
return start()
if intent_name == "DoGame":
return play(intent_request['intent']['slots'])
if intent_name =="CommandInventory":
return getInventory()
elif intent_name == "AMAZON.HelpIntent":
return start()
elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
return end("Game saved")
else:
return reprompt(session)
def on_session_ended(session_ended_request, session):
""" Called when the user ends the session. Is not called when the skill returns should_end_session=true """
print("on_session_ended requestId=" + session_ended_request['requestId'] +
", sessionId=" + session['sessionId'])
# --------------- Main handler ------------------
def lambda_handler(event, context):
""" Route the incoming request based on type (LaunchRequest, IntentRequest,
etc.) The JSON body of the request is provided in the event parameter.
"""
print("event.session.application.applicationId=" +
event['session']['application']['applicationId'])
if event['session']['new']:
on_session_started({'requestId': event['request']['requestId']},
event['session'])
if event['request']['type'] == "LaunchRequest":
return on_launch(event['request'], event['session'])
elif event['request']['type'] == "IntentRequest":
return on_intent(event['request'], event['session'])
elif event['request']['type'] == "SessionEndedRequest":
return on_session_ended(event['request'], event['session'])
# -- RESPONSE --
start_r = {
"session": {
"new": False,
"sessionId": "SessionId.850f9d57-0269-497f-a8a2-494af350863b",
"application": {
"applicationId": "amzn1.ask.skill.0a173718-903e-48e5-b0cc-592f61de28a6"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.AHRB4ZWZZ5GBFEUARZGNCN47CZDDVSBX4KNOMUGEPUFSPAOEM2S72DYICBN5YJZZ3MU4G2RWO5XTDCBVJJPI2DTGX4NCOJVSWR6VJ2UWRMLSGIQJBYVNNTL762YIXXXXEVUQSBYONRMMFGK73AAJTTGR7XFVUVF4XEKSV7GENDMIUSKT3WYG3VSXGGKVUPCI6JL2N6TDAGIW5DQ"
}
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.63aff37d-df73-49b8-b3e1-693f8063a3f7",
"intent": {
"name": "CommandStart",
"slots": {}
},
"locale": "en-US",
"timestamp": "2018-01-28T05:56:29Z"
},
"context": {
"AudioPlayer": {
"playerActivity": "IDLE"
},
"System": {
"application": {
"applicationId": "amzn1.ask.skill.0a173718-903e-48e5-b0cc-592f61de28a6"
},
"user": {
"userId": "amzn1.ask.account.AHRB4ZWZZ5GBFEUARZGNCN47CZDDVSBX4KNOMUGEPUFSPAOEM2S72DYICBN5YJZZ3MU4G2RWO5XTDCBVJJPI2DTGX4NCOJVSWR6VJ2UWRMLSGIQJBYVNNTL762YIXXXXEVUQSBYONRMMFGK73AAJTTGR7XFVUVF4XEKSV7GENDMIUSKT3WYG3VSXGGKVUPCI6JL2N6TDAGIW5DQ"
},
"device": {
"supportedInterfaces": {}
}
}
},
"version": "1.0"
}
search_r = {
"session": {
"new": False,
"sessionId": "SessionId.850f9d57-0269-497f-a8a2-494af350863b",
"application": {
"applicationId": "amzn1.ask.skill.0a173718-903e-48e5-b0cc-592f61de28a6"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.AHRB4ZWZZ5GBFEUARZGNCN47CZDDVSBX4KNOMUGEPUFSPAOEM2S72DYICBN5YJZZ3MU4G2RWO5XTDCBVJJPI2DTGX4NCOJVSWR6VJ2UWRMLSGIQJBYVNNTL762YIXXXXEVUQSBYONRMMFGK73AAJTTGR7XFVUVF4XEKSV7GENDMIUSKT3WYG3VSXGGKVUPCI6JL2N6TDAGIW5DQ"
}
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.14a35585-fb1b-4b47-8eb8-a9eca383357a",
"intent": {
"name": "DoGame",
"slots": {
"BLoc": {
"name": "BLoc"
},
"ANoun": {
"name": "ANoun"
},
"CLoc": {
"name": "CLoc"
},
"CNoun": {
"name": "CNoun"
},
"CUsing": {
"name": "CUsing"
},
"BVerb": {
"name": "BVerb"
},
"BNoun": {
"name": "BNoun",
"value": "north"
},
"AVerb": {
"name": "AVerb",
"value": "go"
},
"DNoun": {
"name": "DNoun"
},
"BSwitch": {
"name": "BSwitch"
}
}
},
"locale": "en-US",
"timestamp": "2018-01-28T05:57:06Z"
},
"context": {
"AudioPlayer": {
"playerActivity": "IDLE"
},
"System": {
"application": {
"applicationId": "amzn1.ask.skill.0a173718-903e-48e5-b0cc-592f61de28a6"
},
"user": {
"userId": "amzn1.ask.account.AHRB4ZWZZ5GBFEUARZGNCN47CZDDVSBX4KNOMUGEPUFSPAOEM2S72DYICBN5YJZZ3MU4G2RWO5XTDCBVJJPI2DTGX4NCOJVSWR6VJ2UWRMLSGIQJBYVNNTL762YIXXXXEVUQSBYONRMMFGK73AAJTTGR7XFVUVF4XEKSV7GENDMIUSKT3WYG3VSXGGKVUPCI6JL2N6TDAGIW5DQ"
},
"device": {
"supportedInterfaces": {}
}
}
},
"version": "1.0"
}
attack_r = {
"session": {
"new": False,
"sessionId": "SessionId.da1f9049-56f0-498d-9fd0-8055f51fabcc",
"application": {
"applicationId": "amzn1.ask.skill.0a173718-903e-48e5-b0cc-592f61de28a6"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.AH5JKB3UIZLYS3GIFJA25Z6YKBSJV2KUO4NZKCAXQ3IBHEJLYJP7IVURZOX5BCPGICTJ3RR3AQOAHRIPXGV7EMAFHGOSXDEJOROU7I77MXWDST5UX3VQXNQHYR6F2M65DFO7ONXUVNFQABCZWIMUOUF2Y6NM3B6TQ7VWH6OGL4DSA4UHDTAZ2L7QUCL7OLIP2IEOBJPKCANKSEA"
}
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.e0c92d5c-3647-4a20-8493-cd9d8aa8a426",
"intent": {
"name": "DoGame",
"slots": {
"BLoc": {
"name": "BLoc"
},
"ANoun": {
"name": "ANoun"
},
"CLoc": {
"name": "CLoc"
},
"CNoun": {
"name": "CNoun"
},
"CUsing": {
"name": "CUsing",
"value": "with"
},
"BVerb": {
"name": "BVerb"
},
"BNoun": {
"name": "BNoun",
"value": "mutant"
},
"AVerb": {
"name": "AVerb",
"value": "attack"
},
"BSwitch": {
"name": "BSwitch"
},
"DNoun": {
"name": "DNoun",
"value": "machete"
}
}
},
"locale": "en-US",
"timestamp": "2018-01-28T12:20:11Z"
},
"context": {
"AudioPlayer": {
"playerActivity": "IDLE"
},
"System": {
"application": {
"applicationId": "amzn1.ask.skill.0a173718-903e-48e5-b0cc-592f61de28a6"
},
"user": {
"userId": "amzn1.ask.account.AH5JKB3UIZLYS3GIFJA25Z6YKBSJV2KUO4NZKCAXQ3IBHEJLYJP7IVURZOX5BCPGICTJ3RR3AQOAHRIPXGV7EMAFHGOSXDEJOROU7I77MXWDST5UX3VQXNQHYR6F2M65DFO7ONXUVNFQABCZWIMUOUF2Y6NM3B6TQ7VWH6OGL4DSA4UHDTAZ2L7QUCL7OLIP2IEOBJPKCANKSEA"
},
"device": {
"supportedInterfaces": {}
}
}
},
"version": "1.0"
}
# print(lambda_handler(start_r,""))
print(lambda_handler(search_r,""))
# print(lambda_handler(attack_r,""))