質問編集履歴

3

Alexa Developer Consoleでのテストでのレスポンスを加筆しました。

2019/09/13 00:27

投稿

yuta_kg
yuta_kg

スコア24

test CHANGED
File without changes
test CHANGED
@@ -54,7 +54,7 @@
54
54
 
55
55
 
56
56
 
57
- 以下、index.jsのコードです。
57
+ 以下、index.jsのコードとAlexa Developer Consoleテストした時のレスポンスです。
58
58
 
59
59
  ```javascript
60
60
 
@@ -375,3 +375,145 @@
375
375
  .lambda();
376
376
 
377
377
  ```
378
+
379
+
380
+
381
+ レスポンス
382
+
383
+ ```
384
+
385
+ {
386
+
387
+ "version": "1.0",
388
+
389
+ "session": {
390
+
391
+ "new": false,
392
+
393
+ "sessionId": "amzn1.echo-api.session.c6c70564-b3a6-483b-9fb1-3a5c81bacccc",
394
+
395
+ "application": {
396
+
397
+ "applicationId": "amzn1.ask.skill.0069cc67-8e5a-4305-9f9d-5c3d433d96d6"
398
+
399
+ },
400
+
401
+ "user": {
402
+
403
+ "userId": "個人情報なので省略",
404
+
405
+ "accessToken": "個人情報なので省略"
406
+
407
+ }
408
+
409
+ },
410
+
411
+ "context": {
412
+
413
+ "System": {
414
+
415
+ "application": {
416
+
417
+ "applicationId": "amzn1.ask.skill.0069cc67-8e5a-4305-9f9d-5c3d433d96d6"
418
+
419
+ },
420
+
421
+ "user": {
422
+
423
+ "userId": "個人情報なので省略",
424
+
425
+ "accessToken": "個人情報なので省略"
426
+
427
+ },
428
+
429
+ "device": {
430
+
431
+ "deviceId": "個人情報なので省略",
432
+
433
+ "supportedInterfaces": {}
434
+
435
+ },
436
+
437
+ "apiEndpoint": "https://api.fe.amazonalexa.com",
438
+
439
+ "apiAccessToken": "個人情報なので省略"
440
+
441
+ },
442
+
443
+ "Viewport": {
444
+
445
+ "experiences": [
446
+
447
+ {
448
+
449
+ "arcMinuteWidth": 246,
450
+
451
+ "arcMinuteHeight": 144,
452
+
453
+ "canRotate": false,
454
+
455
+ "canResize": false
456
+
457
+ }
458
+
459
+ ],
460
+
461
+ "shape": "RECTANGLE",
462
+
463
+ "pixelWidth": 1024,
464
+
465
+ "pixelHeight": 600,
466
+
467
+ "dpi": 160,
468
+
469
+ "currentPixelWidth": 1024,
470
+
471
+ "currentPixelHeight": 600,
472
+
473
+ "touch": [
474
+
475
+ "SINGLE"
476
+
477
+ ],
478
+
479
+ "video": {
480
+
481
+ "codecs": [
482
+
483
+ "H_264_42",
484
+
485
+ "H_264_41"
486
+
487
+ ]
488
+
489
+ }
490
+
491
+ }
492
+
493
+ },
494
+
495
+ "request": {
496
+
497
+ "type": "SessionEndedRequest",
498
+
499
+ "requestId": "amzn1.echo-api.request.e911134b-5bf9-416e-b594-33fe204658eb",
500
+
501
+ "timestamp": "2019-09-13T00:21:58Z",
502
+
503
+ "locale": "ja-JP",
504
+
505
+ "reason": "ERROR",
506
+
507
+ "error": {
508
+
509
+ "type": "INVALID_RESPONSE",
510
+
511
+ "message": "An exception occurred while dispatching the request to the skill."
512
+
513
+ }
514
+
515
+ }
516
+
517
+ }
518
+
519
+ ```

2

index.jsのコードを変更しました。GetNewFactHandlerをコメントアウトしました。

2019/09/13 00:27

投稿

yuta_kg
yuta_kg

スコア24

test CHANGED
File without changes
test CHANGED
@@ -158,37 +158,65 @@
158
158
 
159
159
 
160
160
 
161
+ // const GetNewFactHandler = {
162
+
163
+ // canHandle(handlerInput) {
164
+
165
+ // const request = handlerInput.requestEnvelope.request;
166
+
167
+ // return request.type === 'LaunchRequest'
168
+
169
+ // || (request.type === 'IntentRequest'
170
+
171
+ // && request.intent.name === 'GetNewFactIntent');
172
+
173
+ // },
174
+
175
+ // handle(handlerInput) {
176
+
177
+ // const factArr = data;
178
+
179
+ // const factIndex = Math.floor(Math.random() * factArr.length);
180
+
181
+ // const randomFact = factArr[factIndex];
182
+
183
+ // const speechOutput = GET_FACT_MESSAGE + randomFact;
184
+
185
+
186
+
187
+ // return handlerInput.responseBuilder
188
+
189
+ // .speak(speechOutput)
190
+
191
+ // .withSimpleCard(SKILL_NAME, randomFact)
192
+
193
+ // .getResponse();
194
+
195
+ // },
196
+
197
+ // };
198
+
199
+
200
+
161
- const GetNewFactHandler = {
201
+ const HelpHandler = {
162
202
 
163
203
  canHandle(handlerInput) {
164
204
 
165
205
  const request = handlerInput.requestEnvelope.request;
166
206
 
167
- return request.type === 'LaunchRequest'
207
+ return request.type === 'IntentRequest'
168
-
169
- || (request.type === 'IntentRequest'
208
+
170
-
171
- && request.intent.name === 'GetNewFactIntent');
209
+ && request.intent.name === 'AMAZON.HelpIntent';
172
210
 
173
211
  },
174
212
 
175
213
  handle(handlerInput) {
176
214
 
177
- const factArr = data;
178
-
179
- const factIndex = Math.floor(Math.random() * factArr.length);
180
-
181
- const randomFact = factArr[factIndex];
182
-
183
- const speechOutput = GET_FACT_MESSAGE + randomFact;
184
-
185
-
186
-
187
215
  return handlerInput.responseBuilder
188
216
 
189
- .speak(speechOutput)
217
+ .speak(HELP_MESSAGE)
190
-
218
+
191
- .withSimpleCard(SKILL_NAME, randomFact)
219
+ .reprompt(HELP_REPROMPT)
192
220
 
193
221
  .getResponse();
194
222
 
@@ -198,7 +226,7 @@
198
226
 
199
227
 
200
228
 
201
- const HelpHandler = {
229
+ const ExitHandler = {
202
230
 
203
231
  canHandle(handlerInput) {
204
232
 
@@ -206,7 +234,9 @@
206
234
 
207
235
  return request.type === 'IntentRequest'
208
236
 
209
- && request.intent.name === 'AMAZON.HelpIntent';
237
+ && (request.intent.name === 'AMAZON.CancelIntent'
238
+
239
+ || request.intent.name === 'AMAZON.StopIntent');
210
240
 
211
241
  },
212
242
 
@@ -214,9 +244,7 @@
214
244
 
215
245
  return handlerInput.responseBuilder
216
246
 
217
- .speak(HELP_MESSAGE)
247
+ .speak(STOP_MESSAGE)
218
-
219
- .reprompt(HELP_REPROMPT)
220
248
 
221
249
  .getResponse();
222
250
 
@@ -226,25 +254,49 @@
226
254
 
227
255
 
228
256
 
229
- const ExitHandler = {
257
+ const SessionEndedRequestHandler = {
230
258
 
231
259
  canHandle(handlerInput) {
232
260
 
233
261
  const request = handlerInput.requestEnvelope.request;
234
262
 
235
- return request.type === 'IntentRequest'
263
+ return request.type === 'SessionEndedRequest';
236
-
237
- && (request.intent.name === 'AMAZON.CancelIntent'
238
-
239
- || request.intent.name === 'AMAZON.StopIntent');
240
264
 
241
265
  },
242
266
 
243
267
  handle(handlerInput) {
244
268
 
269
+ console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
270
+
271
+
272
+
273
+ return handlerInput.responseBuilder.getResponse();
274
+
275
+ },
276
+
277
+ };
278
+
279
+
280
+
281
+ const ErrorHandler = {
282
+
283
+ canHandle() {
284
+
285
+ return true;
286
+
287
+ },
288
+
289
+ handle(handlerInput, error) {
290
+
291
+ console.log(`Error handled: ${error.message}`);
292
+
293
+
294
+
245
295
  return handlerInput.responseBuilder
246
296
 
247
- .speak(STOP_MESSAGE)
297
+ .speak('Sorry, an error occurred.')
298
+
299
+ .reprompt('Sorry, an error occurred.')
248
300
 
249
301
  .getResponse();
250
302
 
@@ -254,58 +306,6 @@
254
306
 
255
307
 
256
308
 
257
- const SessionEndedRequestHandler = {
258
-
259
- canHandle(handlerInput) {
260
-
261
- const request = handlerInput.requestEnvelope.request;
262
-
263
- return request.type === 'SessionEndedRequest';
264
-
265
- },
266
-
267
- handle(handlerInput) {
268
-
269
- console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
270
-
271
-
272
-
273
- return handlerInput.responseBuilder.getResponse();
274
-
275
- },
276
-
277
- };
278
-
279
-
280
-
281
- const ErrorHandler = {
282
-
283
- canHandle() {
284
-
285
- return true;
286
-
287
- },
288
-
289
- handle(handlerInput, error) {
290
-
291
- console.log(`Error handled: ${error.message}`);
292
-
293
-
294
-
295
- return handlerInput.responseBuilder
296
-
297
- .speak('Sorry, an error occurred.')
298
-
299
- .reprompt('Sorry, an error occurred.')
300
-
301
- .getResponse();
302
-
303
- },
304
-
305
- };
306
-
307
-
308
-
309
309
  const SKILL_NAME = 'Space Facts';
310
310
 
311
311
  const GET_FACT_MESSAGE = 'Here\'s your fact: ';
@@ -360,7 +360,7 @@
360
360
 
361
361
  LaunchRequestHandler,
362
362
 
363
- GetNewFactHandler,
363
+ // GetNewFactHandler,
364
364
 
365
365
  HelpHandler,
366
366
 
@@ -374,6 +374,4 @@
374
374
 
375
375
  .lambda();
376
376
 
377
-
378
-
379
377
  ```

1

index.jsのコードを追記しました

2019/09/13 00:23

投稿

yuta_kg
yuta_kg

スコア24

test CHANGED
File without changes
test CHANGED
@@ -51,3 +51,329 @@
51
51
  index.js
52
52
 
53
53
  node_modules
54
+
55
+
56
+
57
+ 以下、index.jsのコードです。
58
+
59
+ ```javascript
60
+
61
+ /* eslint-disable func-names */
62
+
63
+ /* eslint-disable no-console */
64
+
65
+
66
+
67
+ const Alexa = require('ask-sdk');
68
+
69
+ const rp = require('request-promise');
70
+
71
+ const line = require('@line/bot-sdk');
72
+
73
+
74
+
75
+ const client = new line.Client({
76
+
77
+ // Lambdaの環境変数よりMessagingAPIのチャネルアクセストークンを取得
78
+
79
+ channelAccessToken: process.env["channelAccessToken"]
80
+
81
+ });
82
+
83
+
84
+
85
+ const LaunchRequestHandler = {
86
+
87
+ canHandle(handlerInput) {
88
+
89
+ return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
90
+
91
+ },
92
+
93
+ async handle(handlerInput) {
94
+
95
+ // Userごとのアクセストークン
96
+
97
+ const accessToken = handlerInput.requestEnvelope.session.user.accessToken;
98
+
99
+ console.log(accessToken);
100
+
101
+
102
+
103
+ var options = {
104
+
105
+ method: 'GET',
106
+
107
+ uri: 'https://api.line.me/v2/profile',
108
+
109
+ headers: {
110
+
111
+ 'Authorization': `Bearer ${accessToken}`
112
+
113
+ }
114
+
115
+ };
116
+
117
+ // アクセストークンより、LINEのUser情報を取得
118
+
119
+ var lineData = JSON.parse(await rp(options));
120
+
121
+
122
+
123
+ const message = {
124
+
125
+ type: 'text',
126
+
127
+ text: 'スキルが起動されました。'
128
+
129
+ };
130
+
131
+
132
+
133
+ const userId = lineData.userId;
134
+
135
+
136
+
137
+ // botに通知を送信
138
+
139
+ await client.pushMessage(userId, message);
140
+
141
+
142
+
143
+ var msg = 'Botにメッセージが送信されました';
144
+
145
+
146
+
147
+ return handlerInput.responseBuilder
148
+
149
+ .speak(msg)
150
+
151
+ .reprompt(msg)
152
+
153
+ .getResponse();
154
+
155
+ }
156
+
157
+ };
158
+
159
+
160
+
161
+ const GetNewFactHandler = {
162
+
163
+ canHandle(handlerInput) {
164
+
165
+ const request = handlerInput.requestEnvelope.request;
166
+
167
+ return request.type === 'LaunchRequest'
168
+
169
+ || (request.type === 'IntentRequest'
170
+
171
+ && request.intent.name === 'GetNewFactIntent');
172
+
173
+ },
174
+
175
+ handle(handlerInput) {
176
+
177
+ const factArr = data;
178
+
179
+ const factIndex = Math.floor(Math.random() * factArr.length);
180
+
181
+ const randomFact = factArr[factIndex];
182
+
183
+ const speechOutput = GET_FACT_MESSAGE + randomFact;
184
+
185
+
186
+
187
+ return handlerInput.responseBuilder
188
+
189
+ .speak(speechOutput)
190
+
191
+ .withSimpleCard(SKILL_NAME, randomFact)
192
+
193
+ .getResponse();
194
+
195
+ },
196
+
197
+ };
198
+
199
+
200
+
201
+ const HelpHandler = {
202
+
203
+ canHandle(handlerInput) {
204
+
205
+ const request = handlerInput.requestEnvelope.request;
206
+
207
+ return request.type === 'IntentRequest'
208
+
209
+ && request.intent.name === 'AMAZON.HelpIntent';
210
+
211
+ },
212
+
213
+ handle(handlerInput) {
214
+
215
+ return handlerInput.responseBuilder
216
+
217
+ .speak(HELP_MESSAGE)
218
+
219
+ .reprompt(HELP_REPROMPT)
220
+
221
+ .getResponse();
222
+
223
+ },
224
+
225
+ };
226
+
227
+
228
+
229
+ const ExitHandler = {
230
+
231
+ canHandle(handlerInput) {
232
+
233
+ const request = handlerInput.requestEnvelope.request;
234
+
235
+ return request.type === 'IntentRequest'
236
+
237
+ && (request.intent.name === 'AMAZON.CancelIntent'
238
+
239
+ || request.intent.name === 'AMAZON.StopIntent');
240
+
241
+ },
242
+
243
+ handle(handlerInput) {
244
+
245
+ return handlerInput.responseBuilder
246
+
247
+ .speak(STOP_MESSAGE)
248
+
249
+ .getResponse();
250
+
251
+ },
252
+
253
+ };
254
+
255
+
256
+
257
+ const SessionEndedRequestHandler = {
258
+
259
+ canHandle(handlerInput) {
260
+
261
+ const request = handlerInput.requestEnvelope.request;
262
+
263
+ return request.type === 'SessionEndedRequest';
264
+
265
+ },
266
+
267
+ handle(handlerInput) {
268
+
269
+ console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
270
+
271
+
272
+
273
+ return handlerInput.responseBuilder.getResponse();
274
+
275
+ },
276
+
277
+ };
278
+
279
+
280
+
281
+ const ErrorHandler = {
282
+
283
+ canHandle() {
284
+
285
+ return true;
286
+
287
+ },
288
+
289
+ handle(handlerInput, error) {
290
+
291
+ console.log(`Error handled: ${error.message}`);
292
+
293
+
294
+
295
+ return handlerInput.responseBuilder
296
+
297
+ .speak('Sorry, an error occurred.')
298
+
299
+ .reprompt('Sorry, an error occurred.')
300
+
301
+ .getResponse();
302
+
303
+ },
304
+
305
+ };
306
+
307
+
308
+
309
+ const SKILL_NAME = 'Space Facts';
310
+
311
+ const GET_FACT_MESSAGE = 'Here\'s your fact: ';
312
+
313
+ const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
314
+
315
+ const HELP_REPROMPT = 'What can I help you with?';
316
+
317
+ const STOP_MESSAGE = 'Goodbye!';
318
+
319
+
320
+
321
+ const data = [
322
+
323
+ 'A year on Mercury is just 88 days long.',
324
+
325
+ 'Despite being farther from the Sun, Venus experiences higher temperatures than Mercury.',
326
+
327
+ 'Venus rotates counter-clockwise, possibly because of a collision in the past with an asteroid.',
328
+
329
+ 'On Mars, the Sun appears about half the size as it does on Earth.',
330
+
331
+ 'Earth is the only planet not named after a god.',
332
+
333
+ 'Jupiter has the shortest day of all the planets.',
334
+
335
+ 'The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 billion years.',
336
+
337
+ 'The Sun contains 99.86% of the mass in the Solar System.',
338
+
339
+ 'The Sun is an almost perfect sphere.',
340
+
341
+ 'A total solar eclipse can happen once every 1 to 2 years. This makes them a rare event.',
342
+
343
+ 'Saturn radiates two and a half times more energy into space than it receives from the sun.',
344
+
345
+ 'The temperature inside the Sun can reach 15 million degrees Celsius.',
346
+
347
+ 'The Moon is moving approximately 3.8 cm away from our planet every year.',
348
+
349
+ ];
350
+
351
+
352
+
353
+ const skillBuilder = Alexa.SkillBuilders.standard();
354
+
355
+
356
+
357
+ exports.handler = skillBuilder
358
+
359
+ .addRequestHandlers(
360
+
361
+ LaunchRequestHandler,
362
+
363
+ GetNewFactHandler,
364
+
365
+ HelpHandler,
366
+
367
+ ExitHandler,
368
+
369
+ SessionEndedRequestHandler
370
+
371
+ )
372
+
373
+ .addErrorHandlers(ErrorHandler)
374
+
375
+ .lambda();
376
+
377
+
378
+
379
+ ```