質問編集履歴
3
Alexa Developer Consoleでのテストでのレスポンスを加筆しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -26,7 +26,7 @@
|
|
26
26
|
index.js
|
27
27
|
node_modules
|
28
28
|
|
29
|
-
以下、index.jsのコードです。
|
29
|
+
以下、index.jsのコードとAlexa Developer Consoleでテストした時のレスポンスです。
|
30
30
|
```javascript
|
31
31
|
/* eslint-disable func-names */
|
32
32
|
/* eslint-disable no-console */
|
@@ -186,4 +186,75 @@
|
|
186
186
|
)
|
187
187
|
.addErrorHandlers(ErrorHandler)
|
188
188
|
.lambda();
|
189
|
+
```
|
190
|
+
|
191
|
+
レスポンス
|
192
|
+
```
|
193
|
+
{
|
194
|
+
"version": "1.0",
|
195
|
+
"session": {
|
196
|
+
"new": false,
|
197
|
+
"sessionId": "amzn1.echo-api.session.c6c70564-b3a6-483b-9fb1-3a5c81bacccc",
|
198
|
+
"application": {
|
199
|
+
"applicationId": "amzn1.ask.skill.0069cc67-8e5a-4305-9f9d-5c3d433d96d6"
|
200
|
+
},
|
201
|
+
"user": {
|
202
|
+
"userId": "個人情報なので省略",
|
203
|
+
"accessToken": "個人情報なので省略"
|
204
|
+
}
|
205
|
+
},
|
206
|
+
"context": {
|
207
|
+
"System": {
|
208
|
+
"application": {
|
209
|
+
"applicationId": "amzn1.ask.skill.0069cc67-8e5a-4305-9f9d-5c3d433d96d6"
|
210
|
+
},
|
211
|
+
"user": {
|
212
|
+
"userId": "個人情報なので省略",
|
213
|
+
"accessToken": "個人情報なので省略"
|
214
|
+
},
|
215
|
+
"device": {
|
216
|
+
"deviceId": "個人情報なので省略",
|
217
|
+
"supportedInterfaces": {}
|
218
|
+
},
|
219
|
+
"apiEndpoint": "https://api.fe.amazonalexa.com",
|
220
|
+
"apiAccessToken": "個人情報なので省略"
|
221
|
+
},
|
222
|
+
"Viewport": {
|
223
|
+
"experiences": [
|
224
|
+
{
|
225
|
+
"arcMinuteWidth": 246,
|
226
|
+
"arcMinuteHeight": 144,
|
227
|
+
"canRotate": false,
|
228
|
+
"canResize": false
|
229
|
+
}
|
230
|
+
],
|
231
|
+
"shape": "RECTANGLE",
|
232
|
+
"pixelWidth": 1024,
|
233
|
+
"pixelHeight": 600,
|
234
|
+
"dpi": 160,
|
235
|
+
"currentPixelWidth": 1024,
|
236
|
+
"currentPixelHeight": 600,
|
237
|
+
"touch": [
|
238
|
+
"SINGLE"
|
239
|
+
],
|
240
|
+
"video": {
|
241
|
+
"codecs": [
|
242
|
+
"H_264_42",
|
243
|
+
"H_264_41"
|
244
|
+
]
|
245
|
+
}
|
246
|
+
}
|
247
|
+
},
|
248
|
+
"request": {
|
249
|
+
"type": "SessionEndedRequest",
|
250
|
+
"requestId": "amzn1.echo-api.request.e911134b-5bf9-416e-b594-33fe204658eb",
|
251
|
+
"timestamp": "2019-09-13T00:21:58Z",
|
252
|
+
"locale": "ja-JP",
|
253
|
+
"reason": "ERROR",
|
254
|
+
"error": {
|
255
|
+
"type": "INVALID_RESPONSE",
|
256
|
+
"message": "An exception occurred while dispatching the request to the skill."
|
257
|
+
}
|
258
|
+
}
|
259
|
+
}
|
189
260
|
```
|
2
index.jsのコードを変更しました。GetNewFactHandlerをコメントアウトしました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -78,25 +78,25 @@
|
|
78
78
|
}
|
79
79
|
};
|
80
80
|
|
81
|
-
const GetNewFactHandler = {
|
81
|
+
// const GetNewFactHandler = {
|
82
|
-
|
82
|
+
// canHandle(handlerInput) {
|
83
|
-
|
83
|
+
// const request = handlerInput.requestEnvelope.request;
|
84
|
-
|
84
|
+
// return request.type === 'LaunchRequest'
|
85
|
-
|
85
|
+
// || (request.type === 'IntentRequest'
|
86
|
-
|
86
|
+
// && request.intent.name === 'GetNewFactIntent');
|
87
|
-
|
87
|
+
// },
|
88
|
-
|
88
|
+
// handle(handlerInput) {
|
89
|
-
|
89
|
+
// const factArr = data;
|
90
|
-
|
90
|
+
// const factIndex = Math.floor(Math.random() * factArr.length);
|
91
|
-
|
91
|
+
// const randomFact = factArr[factIndex];
|
92
|
-
|
92
|
+
// const speechOutput = GET_FACT_MESSAGE + randomFact;
|
93
93
|
|
94
|
-
|
94
|
+
// return handlerInput.responseBuilder
|
95
|
-
|
95
|
+
// .speak(speechOutput)
|
96
|
-
|
96
|
+
// .withSimpleCard(SKILL_NAME, randomFact)
|
97
|
-
|
97
|
+
// .getResponse();
|
98
|
-
|
98
|
+
// },
|
99
|
-
};
|
99
|
+
// };
|
100
100
|
|
101
101
|
const HelpHandler = {
|
102
102
|
canHandle(handlerInput) {
|
@@ -179,12 +179,11 @@
|
|
179
179
|
exports.handler = skillBuilder
|
180
180
|
.addRequestHandlers(
|
181
181
|
LaunchRequestHandler,
|
182
|
-
GetNewFactHandler,
|
182
|
+
// GetNewFactHandler,
|
183
183
|
HelpHandler,
|
184
184
|
ExitHandler,
|
185
185
|
SessionEndedRequestHandler
|
186
186
|
)
|
187
187
|
.addErrorHandlers(ErrorHandler)
|
188
188
|
.lambda();
|
189
|
-
|
190
189
|
```
|
1
index.jsのコードを追記しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -24,4 +24,167 @@
|
|
24
24
|
|
25
25
|
また、zipファイルの内部は以下になります。
|
26
26
|
index.js
|
27
|
-
node_modules
|
27
|
+
node_modules
|
28
|
+
|
29
|
+
以下、index.jsのコードです。
|
30
|
+
```javascript
|
31
|
+
/* eslint-disable func-names */
|
32
|
+
/* eslint-disable no-console */
|
33
|
+
|
34
|
+
const Alexa = require('ask-sdk');
|
35
|
+
const rp = require('request-promise');
|
36
|
+
const line = require('@line/bot-sdk');
|
37
|
+
|
38
|
+
const client = new line.Client({
|
39
|
+
// Lambdaの環境変数よりMessagingAPIのチャネルアクセストークンを取得
|
40
|
+
channelAccessToken: process.env["channelAccessToken"]
|
41
|
+
});
|
42
|
+
|
43
|
+
const LaunchRequestHandler = {
|
44
|
+
canHandle(handlerInput) {
|
45
|
+
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
|
46
|
+
},
|
47
|
+
async handle(handlerInput) {
|
48
|
+
// Userごとのアクセストークン
|
49
|
+
const accessToken = handlerInput.requestEnvelope.session.user.accessToken;
|
50
|
+
console.log(accessToken);
|
51
|
+
|
52
|
+
var options = {
|
53
|
+
method: 'GET',
|
54
|
+
uri: 'https://api.line.me/v2/profile',
|
55
|
+
headers: {
|
56
|
+
'Authorization': `Bearer ${accessToken}`
|
57
|
+
}
|
58
|
+
};
|
59
|
+
// アクセストークンより、LINEのUser情報を取得
|
60
|
+
var lineData = JSON.parse(await rp(options));
|
61
|
+
|
62
|
+
const message = {
|
63
|
+
type: 'text',
|
64
|
+
text: 'スキルが起動されました。'
|
65
|
+
};
|
66
|
+
|
67
|
+
const userId = lineData.userId;
|
68
|
+
|
69
|
+
// botに通知を送信
|
70
|
+
await client.pushMessage(userId, message);
|
71
|
+
|
72
|
+
var msg = 'Botにメッセージが送信されました';
|
73
|
+
|
74
|
+
return handlerInput.responseBuilder
|
75
|
+
.speak(msg)
|
76
|
+
.reprompt(msg)
|
77
|
+
.getResponse();
|
78
|
+
}
|
79
|
+
};
|
80
|
+
|
81
|
+
const GetNewFactHandler = {
|
82
|
+
canHandle(handlerInput) {
|
83
|
+
const request = handlerInput.requestEnvelope.request;
|
84
|
+
return request.type === 'LaunchRequest'
|
85
|
+
|| (request.type === 'IntentRequest'
|
86
|
+
&& request.intent.name === 'GetNewFactIntent');
|
87
|
+
},
|
88
|
+
handle(handlerInput) {
|
89
|
+
const factArr = data;
|
90
|
+
const factIndex = Math.floor(Math.random() * factArr.length);
|
91
|
+
const randomFact = factArr[factIndex];
|
92
|
+
const speechOutput = GET_FACT_MESSAGE + randomFact;
|
93
|
+
|
94
|
+
return handlerInput.responseBuilder
|
95
|
+
.speak(speechOutput)
|
96
|
+
.withSimpleCard(SKILL_NAME, randomFact)
|
97
|
+
.getResponse();
|
98
|
+
},
|
99
|
+
};
|
100
|
+
|
101
|
+
const HelpHandler = {
|
102
|
+
canHandle(handlerInput) {
|
103
|
+
const request = handlerInput.requestEnvelope.request;
|
104
|
+
return request.type === 'IntentRequest'
|
105
|
+
&& request.intent.name === 'AMAZON.HelpIntent';
|
106
|
+
},
|
107
|
+
handle(handlerInput) {
|
108
|
+
return handlerInput.responseBuilder
|
109
|
+
.speak(HELP_MESSAGE)
|
110
|
+
.reprompt(HELP_REPROMPT)
|
111
|
+
.getResponse();
|
112
|
+
},
|
113
|
+
};
|
114
|
+
|
115
|
+
const ExitHandler = {
|
116
|
+
canHandle(handlerInput) {
|
117
|
+
const request = handlerInput.requestEnvelope.request;
|
118
|
+
return request.type === 'IntentRequest'
|
119
|
+
&& (request.intent.name === 'AMAZON.CancelIntent'
|
120
|
+
|| request.intent.name === 'AMAZON.StopIntent');
|
121
|
+
},
|
122
|
+
handle(handlerInput) {
|
123
|
+
return handlerInput.responseBuilder
|
124
|
+
.speak(STOP_MESSAGE)
|
125
|
+
.getResponse();
|
126
|
+
},
|
127
|
+
};
|
128
|
+
|
129
|
+
const SessionEndedRequestHandler = {
|
130
|
+
canHandle(handlerInput) {
|
131
|
+
const request = handlerInput.requestEnvelope.request;
|
132
|
+
return request.type === 'SessionEndedRequest';
|
133
|
+
},
|
134
|
+
handle(handlerInput) {
|
135
|
+
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
|
136
|
+
|
137
|
+
return handlerInput.responseBuilder.getResponse();
|
138
|
+
},
|
139
|
+
};
|
140
|
+
|
141
|
+
const ErrorHandler = {
|
142
|
+
canHandle() {
|
143
|
+
return true;
|
144
|
+
},
|
145
|
+
handle(handlerInput, error) {
|
146
|
+
console.log(`Error handled: ${error.message}`);
|
147
|
+
|
148
|
+
return handlerInput.responseBuilder
|
149
|
+
.speak('Sorry, an error occurred.')
|
150
|
+
.reprompt('Sorry, an error occurred.')
|
151
|
+
.getResponse();
|
152
|
+
},
|
153
|
+
};
|
154
|
+
|
155
|
+
const SKILL_NAME = 'Space Facts';
|
156
|
+
const GET_FACT_MESSAGE = 'Here\'s your fact: ';
|
157
|
+
const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
|
158
|
+
const HELP_REPROMPT = 'What can I help you with?';
|
159
|
+
const STOP_MESSAGE = 'Goodbye!';
|
160
|
+
|
161
|
+
const data = [
|
162
|
+
'A year on Mercury is just 88 days long.',
|
163
|
+
'Despite being farther from the Sun, Venus experiences higher temperatures than Mercury.',
|
164
|
+
'Venus rotates counter-clockwise, possibly because of a collision in the past with an asteroid.',
|
165
|
+
'On Mars, the Sun appears about half the size as it does on Earth.',
|
166
|
+
'Earth is the only planet not named after a god.',
|
167
|
+
'Jupiter has the shortest day of all the planets.',
|
168
|
+
'The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 billion years.',
|
169
|
+
'The Sun contains 99.86% of the mass in the Solar System.',
|
170
|
+
'The Sun is an almost perfect sphere.',
|
171
|
+
'A total solar eclipse can happen once every 1 to 2 years. This makes them a rare event.',
|
172
|
+
'Saturn radiates two and a half times more energy into space than it receives from the sun.',
|
173
|
+
'The temperature inside the Sun can reach 15 million degrees Celsius.',
|
174
|
+
'The Moon is moving approximately 3.8 cm away from our planet every year.',
|
175
|
+
];
|
176
|
+
|
177
|
+
const skillBuilder = Alexa.SkillBuilders.standard();
|
178
|
+
|
179
|
+
exports.handler = skillBuilder
|
180
|
+
.addRequestHandlers(
|
181
|
+
LaunchRequestHandler,
|
182
|
+
GetNewFactHandler,
|
183
|
+
HelpHandler,
|
184
|
+
ExitHandler,
|
185
|
+
SessionEndedRequestHandler
|
186
|
+
)
|
187
|
+
.addErrorHandlers(ErrorHandler)
|
188
|
+
.lambda();
|
189
|
+
|
190
|
+
```
|