質問編集履歴
1
コードを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,3 +25,343 @@
|
|
25
25
|
|
26
26
|
|
27
27
|
Alexaに話したことをslackに表示させることはできたのですが、大盛りや弁当の個数、注文を終わらせるまでループ処理をさせる、といったところでつまずいております。なにかヒントをいただければ幸いです。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```python
|
32
|
+
|
33
|
+
#coding : utf-8
|
34
|
+
|
35
|
+
"""
|
36
|
+
|
37
|
+
This sample demonstrates a simple skill built with the Amazon Alexa Skills Kit.
|
38
|
+
|
39
|
+
The Intent Schema, Custom Slots, and Sample Utterances for this skill, as well
|
40
|
+
|
41
|
+
as testing instructions are located at http://amzn.to/1LzFrj6
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
For additional samples, visit the Alexa Skills Kit Getting Started guide at
|
46
|
+
|
47
|
+
http://amzn.to/1LGWsLG
|
48
|
+
|
49
|
+
"""
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
from __future__ import print_function
|
54
|
+
|
55
|
+
import urllib.parse
|
56
|
+
|
57
|
+
import urllib.request
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
# --------------- Helpers that build all of the responses ----------------------
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
def build_speechlet_response(title, output, reprompt_text, should_end_session):
|
68
|
+
|
69
|
+
return {
|
70
|
+
|
71
|
+
'outputSpeech': {
|
72
|
+
|
73
|
+
'type': 'PlainText',
|
74
|
+
|
75
|
+
'text': output
|
76
|
+
|
77
|
+
},
|
78
|
+
|
79
|
+
'card': {
|
80
|
+
|
81
|
+
'type': 'Simple',
|
82
|
+
|
83
|
+
'title': "SessionSpeechlet - " + title,
|
84
|
+
|
85
|
+
'content': "SessionSpeechlet - " + output
|
86
|
+
|
87
|
+
},
|
88
|
+
|
89
|
+
'reprompt': {
|
90
|
+
|
91
|
+
'outputSpeech': {
|
92
|
+
|
93
|
+
'type': 'PlainText',
|
94
|
+
|
95
|
+
'text': reprompt_text
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
},
|
100
|
+
|
101
|
+
'shouldEndSession': should_end_session
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def build_response(session_attributes, speechlet_response):
|
110
|
+
|
111
|
+
return {
|
112
|
+
|
113
|
+
'version': '1.0',
|
114
|
+
|
115
|
+
'sessionAttributes': session_attributes,
|
116
|
+
|
117
|
+
'response': speechlet_response
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
# --------------- Functions that control the skill's behavior ------------------
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def get_welcome_response():
|
130
|
+
|
131
|
+
""" If we wanted to initialize the session to have some attributes we could
|
132
|
+
|
133
|
+
add those here
|
134
|
+
|
135
|
+
"""
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
session_attributes = {}
|
140
|
+
|
141
|
+
card_title = "Welcome"
|
142
|
+
|
143
|
+
speech_output = "注文をどうぞ"
|
144
|
+
|
145
|
+
reprompt_text = "もう一度おねがいします"
|
146
|
+
|
147
|
+
should_end_session = False
|
148
|
+
|
149
|
+
return build_response(session_attributes, build_speechlet_response(
|
150
|
+
|
151
|
+
card_title, speech_output, reprompt_text, should_end_session))
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
def handle_session_end_request():
|
158
|
+
|
159
|
+
card_title = "Session Ended"
|
160
|
+
|
161
|
+
speech_output = "Thank you for trying the Alexa Skills Kit sample. " \
|
162
|
+
|
163
|
+
"Have a nice day! "
|
164
|
+
|
165
|
+
# Setting this to true ends the session and exits the skill.
|
166
|
+
|
167
|
+
should_end_session = True
|
168
|
+
|
169
|
+
return build_response({}, build_speechlet_response(
|
170
|
+
|
171
|
+
card_title, speech_output, None, should_end_session))
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
def take_memo(intent, session):
|
178
|
+
|
179
|
+
try:
|
180
|
+
|
181
|
+
memo = intent["slots"]["contents"].get("value")
|
182
|
+
|
183
|
+
url = ''//ここにwebhookを使って作成したURLを入れる
|
184
|
+
|
185
|
+
params = urllib.parse.urlencode({"payload": {"text":":memo:"+memo}}).encode("utf-8")
|
186
|
+
|
187
|
+
f = urllib.request.urlopen(url, params)
|
188
|
+
|
189
|
+
speech_output = "注文を受け付けました"
|
190
|
+
|
191
|
+
except:
|
192
|
+
|
193
|
+
speech_output = "失敗しました"
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
session_attributes = {}
|
198
|
+
|
199
|
+
reprompt_text = None
|
200
|
+
|
201
|
+
should_end_session = True
|
202
|
+
|
203
|
+
return build_response(session_attributes, build_speechlet_response(
|
204
|
+
|
205
|
+
intent['name'], speech_output, reprompt_text, should_end_session))
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
# --------------- Events ------------------
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
def on_session_started(session_started_request, session):
|
216
|
+
|
217
|
+
""" Called when the session starts """
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
print("on_session_started requestId=" + session_started_request['requestId']
|
222
|
+
|
223
|
+
+ ", sessionId=" + session['sessionId'])
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
def on_launch(launch_request, session):
|
230
|
+
|
231
|
+
""" Called when the user launches the skill without specifying what they
|
232
|
+
|
233
|
+
want
|
234
|
+
|
235
|
+
"""
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
print("on_launch requestId=" + launch_request['requestId'] +
|
240
|
+
|
241
|
+
", sessionId=" + session['sessionId'])
|
242
|
+
|
243
|
+
# Dispatch to your skill's launch
|
244
|
+
|
245
|
+
return get_welcome_response()
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
def on_intent(intent_request, session):
|
252
|
+
|
253
|
+
""" Called when the user specifies an intent for this skill """
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
print("on_intent requestId=" + intent_request['requestId'] +
|
258
|
+
|
259
|
+
", sessionId=" + session['sessionId'])
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
intent = intent_request['intent']
|
264
|
+
|
265
|
+
intent_name = intent_request['intent']['name']
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
# Dispatch to your skill's intent handlers
|
270
|
+
|
271
|
+
if intent_name == "Memointent":
|
272
|
+
|
273
|
+
return take_memo(intent, session)
|
274
|
+
|
275
|
+
elif intent_name == "AMAZON.HelpIntent":
|
276
|
+
|
277
|
+
return get_welcome_response()
|
278
|
+
|
279
|
+
elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
|
280
|
+
|
281
|
+
return handle_session_end_request()
|
282
|
+
|
283
|
+
else:
|
284
|
+
|
285
|
+
raise ValueError("Invalid intent")
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
def on_session_ended(session_ended_request, session):
|
292
|
+
|
293
|
+
""" Called when the user ends the session.
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
Is not called when the skill returns should_end_session=true
|
298
|
+
|
299
|
+
"""
|
300
|
+
|
301
|
+
print("on_session_ended requestId=" + session_ended_request['requestId'] +
|
302
|
+
|
303
|
+
", sessionId=" + session['sessionId'])
|
304
|
+
|
305
|
+
# add cleanup logic here
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
# --------------- Main handler ------------------
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
def lambda_handler(event, context):
|
316
|
+
|
317
|
+
""" Route the incoming request based on type (LaunchRequest, IntentRequest,
|
318
|
+
|
319
|
+
etc.) The JSON body of the request is provided in the event parameter.
|
320
|
+
|
321
|
+
"""
|
322
|
+
|
323
|
+
print("event.session.application.applicationId=" +
|
324
|
+
|
325
|
+
event['session']['application']['applicationId'])
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
"""
|
330
|
+
|
331
|
+
Uncomment this if statement and populate with your skill's application ID to
|
332
|
+
|
333
|
+
prevent someone else from configuring a skill that sends requests to this
|
334
|
+
|
335
|
+
function.
|
336
|
+
|
337
|
+
"""
|
338
|
+
|
339
|
+
# if (event['session']['application']['applicationId'] !=
|
340
|
+
|
341
|
+
# "amzn1.echo-sdk-ams.app.[unique-value-here]"):
|
342
|
+
|
343
|
+
# raise ValueError("Invalid Application ID")
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
if event['session']['new']:
|
348
|
+
|
349
|
+
on_session_started({'requestId': event['request']['requestId']},
|
350
|
+
|
351
|
+
event['session'])
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
if event['request']['type'] == "LaunchRequest":
|
356
|
+
|
357
|
+
return on_launch(event['request'], event['session'])
|
358
|
+
|
359
|
+
elif event['request']['type'] == "IntentRequest":
|
360
|
+
|
361
|
+
return on_intent(event['request'], event['session'])
|
362
|
+
|
363
|
+
elif event['request']['type'] == "SessionEndedRequest":
|
364
|
+
|
365
|
+
return on_session_ended(event['request'], event['session'])
|
366
|
+
|
367
|
+
```
|