-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.js
More file actions
452 lines (415 loc) · 16.4 KB
/
index.js
File metadata and controls
452 lines (415 loc) · 16.4 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
//
// Alexa Fact Skill - Sample for Beginners
//
// sets up dependencies
const Alexa = require('ask-sdk-core');
const i18n = require('i18next');
// core functionality for fact skill
const GetNewFactHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
// checks request type
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'GetNewFactIntent');
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
// gets a random fact by assigning an array to the variable
// the random item from the array will be selected by the i18next library
// the i18next library is set up in the Request Interceptor
const randomFact = requestAttributes.t('FACTS');
// concatenates a standard message with the random fact
const speakOutput = requestAttributes.t('GET_FACT_MESSAGE') + randomFact;
return handlerInput.responseBuilder
.speak(speakOutput)
// Uncomment the next line if you want to keep the session open so you can
// ask for another fact without first re-opening the skill
// .reprompt(requestAttributes.t('HELP_REPROMPT'))
.withSimpleCard(requestAttributes.t('SKILL_NAME'), randomFact)
.getResponse();
},
};
const HelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('HELP_MESSAGE'))
.reprompt(requestAttributes.t('HELP_REPROMPT'))
.getResponse();
},
};
const FallbackHandler = {
// The FallbackIntent can only be sent in those locales which support it,
// so this handler will always be skipped in locales where it is not supported.
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.FallbackIntent';
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('FALLBACK_MESSAGE'))
.reprompt(requestAttributes.t('FALLBACK_REPROMPT'))
.getResponse();
},
};
const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& (request.intent.name === 'AMAZON.CancelIntent'
|| request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('STOP_MESSAGE'))
.getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
console.log(`Error stack: ${error.stack}`);
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t('ERROR_MESSAGE'))
.reprompt(requestAttributes.t('ERROR_MESSAGE'))
.getResponse();
},
};
const LocalizationInterceptor = {
process(handlerInput) {
// Gets the locale from the request and initializes i18next.
const localizationClient = i18n.init({
lng: handlerInput.requestEnvelope.request.locale,
resources: languageStrings,
returnObjects: true
});
// Creates a localize function to support arguments.
localizationClient.localize = function localize() {
// gets arguments through and passes them to
// i18next using sprintf to replace string placeholders
// with arguments.
const args = arguments;
const value = i18n.t(...args);
// If an array is used then a random value is selected
if (Array.isArray(value)) {
return value[Math.floor(Math.random() * value.length)];
}
return value;
};
// this gets the request attributes and save the localize function inside
// it to be used in a handler by calling requestAttributes.t(STRING_ID, [args...])
const attributes = handlerInput.attributesManager.getRequestAttributes();
attributes.t = function translate(...args) {
return localizationClient.localize(...args);
}
}
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
GetNewFactHandler,
HelpHandler,
ExitHandler,
FallbackHandler,
SessionEndedRequestHandler,
)
.addRequestInterceptors(LocalizationInterceptor)
.addErrorHandlers(ErrorHandler)
.withCustomUserAgent('sample/basic-fact/v2')
.lambda();
// TODO: Replace this data with your own.
// It is organized by language/locale. You can safely ignore the locales you aren't using.
// Update the name and messages to align with the theme of your skill
const deData = {
translation: {
SKILL_NAME: 'Weltraumwissen',
GET_FACT_MESSAGE: 'Hier sind deine Fakten: ',
HELP_MESSAGE: 'Du kannst sagen, „Nenne mir einen Fakt über den Weltraum“, oder du kannst „Beenden“ sagen... Wie kann ich dir helfen?',
HELP_REPROMPT: 'Wie kann ich dir helfen?',
FALLBACK_MESSAGE: 'Die Weltraumfakten Skill kann dir dabei nicht helfen. Sie kann dir Fakten über den Raum erzählen, wenn du dannach fragst.',
FALLBACK_REPROMPT: 'Wie kann ich dir helfen?',
ERROR_MESSAGE: 'Es ist ein Fehler aufgetreten.',
STOP_MESSAGE: 'Auf Wiedersehen!',
FACTS:
[
'Ein Jahr dauert auf dem Merkur nur 88 Tage.',
'Die Venus ist zwar weiter von der Sonne entfernt, hat aber höhere Temperaturen als Merkur.',
'Venus dreht sich entgegen dem Uhrzeigersinn, möglicherweise aufgrund eines früheren Zusammenstoßes mit einem Asteroiden.',
'Auf dem Mars erscheint die Sonne nur halb so groß wie auf der Erde.',
'Jupiter hat den kürzesten Tag aller Planeten.',
],
},
};
const dedeData = {
translation: {
SKILL_NAME: 'Weltraumwissen auf Deutsch',
},
};
const enData = {
translation: {
SKILL_NAME: 'Space Facts',
GET_FACT_MESSAGE: 'Here\'s your fact: ',
HELP_MESSAGE: 'You can say tell me a space fact, or, you can say exit... What can I help you with?',
HELP_REPROMPT: 'What can I help you with?',
FALLBACK_MESSAGE: 'The Space Facts skill can\'t help you with that. It can help you discover facts about space if you say tell me a space fact. What can I help you with?',
FALLBACK_REPROMPT: 'What can I help you with?',
ERROR_MESSAGE: 'Sorry, an error occurred.',
STOP_MESSAGE: 'Goodbye!',
FACTS:
[
'A year on Mercury is just 88 days long.',
'Despite being farther from the Sun, Venus experiences higher temperatures than Mercury.',
'On Mars, the Sun appears about half the size as it does on Earth.',
'Jupiter has the shortest day of all the planets.',
'The Sun is an almost perfect sphere.',
],
},
};
const enauData = {
translation: {
SKILL_NAME: 'Australian Space Facts',
},
};
const encaData = {
translation: {
SKILL_NAME: 'Canadian Space Facts',
},
};
const engbData = {
translation: {
SKILL_NAME: 'British Space Facts',
},
};
const eninData = {
translation: {
SKILL_NAME: 'Indian Space Facts',
},
};
const enusData = {
translation: {
SKILL_NAME: 'American Space Facts',
},
};
const esData = {
translation: {
SKILL_NAME: 'Curiosidades del Espacio',
GET_FACT_MESSAGE: 'Aquí está tu curiosidad: ',
HELP_MESSAGE: 'Puedes decir dime una curiosidad del espacio o puedes decir salir... Cómo te puedo ayudar?',
HELP_REPROMPT: 'Como te puedo ayudar?',
FALLBACK_MESSAGE: 'La skill Curiosidades del Espacio no te puede ayudar con eso. Te puede ayudar a descubrir curiosidades sobre el espacio si dices dime una curiosidad del espacio. Como te puedo ayudar?',
FALLBACK_REPROMPT: 'Como te puedo ayudar?',
ERROR_MESSAGE: 'Lo sentimos, se ha producido un error.',
STOP_MESSAGE: 'Adiós!',
FACTS:
[
'Un año en Mercurio es de solo 88 días',
'A pesar de estar más lejos del Sol, Venus tiene temperaturas más altas que Mercurio',
'En Marte el sol se ve la mitad de grande que en la Tierra',
'Jupiter tiene el día más corto de todos los planetas',
'El sol es una esféra casi perfecta',
],
},
};
const esesData = {
translation: {
SKILL_NAME: 'Curiosidades del Espacio para España',
},
};
const esmxData = {
translation: {
SKILL_NAME: 'Curiosidades del Espacio para México',
},
};
const esusData = {
translation: {
SKILL_NAME: 'Curiosidades del Espacio para Estados Unidos',
},
};
const frData = {
translation: {
SKILL_NAME: 'Anecdotes de l\'Espace',
GET_FACT_MESSAGE: 'Voici votre anecdote : ',
HELP_MESSAGE: 'Vous pouvez dire donne-moi une anecdote, ou, vous pouvez dire stop... Comment puis-je vous aider?',
HELP_REPROMPT: 'Comment puis-je vous aider?',
FALLBACK_MESSAGE: 'La skill des anecdotes de l\'espace ne peux vous aider avec cela. Je peux vous aider à découvrir des anecdotes sur l\'espace si vous dites par exemple, donne-moi une anecdote. Comment puis-je vous aider?',
FALLBACK_REPROMPT: 'Comment puis-je vous aider?',
ERROR_MESSAGE: 'Désolé, une erreur est survenue.',
STOP_MESSAGE: 'Au revoir!',
FACTS:
[
'Une année sur Mercure ne dure que 88 jours.',
'En dépit de son éloignement du Soleil, Vénus connaît des températures plus élevées que sur Mercure.',
'Sur Mars, le Soleil apparaît environ deux fois plus petit que sur Terre.',
'De toutes les planètes, Jupiter a le jour le plus court.',
'Le Soleil est une sphère presque parfaite.',
],
},
};
const frfrData = {
translation: {
SKILL_NAME: 'Anecdotes françaises de l\'espace',
},
};
const frcaData = {
translation: {
SKILL_NAME: 'Anecdotes canadiennes de l\'espace',
},
};
const hiData = {
translation: {
SKILL_NAME: 'अंतरिक्ष तथ्य',
GET_FACT_MESSAGE: 'ये लीजिए आपका तथ्य: ',
HELP_MESSAGE: 'आप मुझे नया तथ्य सुनाओ बोल सकते हैं या फिर exit भी बोल सकते हैं... आप क्या करना चाहेंगे?',
HELP_REPROMPT: 'मैं आपकी किस प्रकार से सहायता कर सकती हूँ?',
ERROR_MESSAGE: 'सॉरी, मैं वो समज नहीं पायी. क्या आप repeat कर सकते हैं?',
STOP_MESSAGE: 'अच्छा bye, फिर मिलते हैं',
FACTS:
[
'बुध गृह में एक साल में केवल अठासी दिन होते हैं',
'सूरज से दूर होने के बावजूद, शुक्र ग्रह का तापमान बुध ग्रह से ज़्यादा होता हैं',
'पृथ्वी के तुलना से मंगल ग्रह में सूरज का आकार तक़रीबन आधा हैं',
'सारे ग्रहों में बृहस्पति ग्रह का दिन सबसे कम हैं',
'सूरज का आकार एकदम गेंद आकार में हैं'
],
},
};
const hiinData = {
translation: {
SKILL_NAME: 'अंतरिक्ष तथ्य',
},
}
const itData = {
translation: {
SKILL_NAME: 'Aneddoti dallo spazio',
GET_FACT_MESSAGE: 'Ecco il tuo aneddoto: ',
HELP_MESSAGE: 'Puoi chiedermi un aneddoto dallo spazio o puoi chiudermi dicendo "esci"... Come posso aiutarti?',
HELP_REPROMPT: 'Come posso aiutarti?',
FALLBACK_MESSAGE: 'Non posso aiutarti con questo. Posso aiutarti a scoprire fatti e aneddoti sullo spazio, basta che mi chiedi di dirti un aneddoto. Come posso aiutarti?',
FALLBACK_REPROMPT: 'Come posso aiutarti?',
ERROR_MESSAGE: 'Spiacenti, si è verificato un errore.',
STOP_MESSAGE: 'A presto!',
FACTS:
[
'Sul pianeta Mercurio, un anno dura solamente 88 giorni.',
'Pur essendo più lontana dal Sole, Venere ha temperature più alte di Mercurio.',
'Su Marte il sole appare grande la metà che su la terra. ',
'Tra tutti i pianeti del sistema solare, la giornata più corta è su Giove.',
'Il Sole è quasi una sfera perfetta.',
],
},
};
const ititData = {
translation: {
SKILL_NAME: 'Aneddoti dallo spazio',
},
};
const jpData = {
translation: {
SKILL_NAME: '日本語版豆知識',
GET_FACT_MESSAGE: '知ってましたか?',
HELP_MESSAGE: '豆知識を聞きたい時は「豆知識」と、終わりたい時は「おしまい」と言ってください。どうしますか?',
HELP_REPROMPT: 'どうしますか?',
ERROR_MESSAGE: '申し訳ありませんが、エラーが発生しました',
STOP_MESSAGE: 'さようなら',
FACTS:
[
'水星の一年はたった88日です。',
'金星は水星と比べて太陽より遠くにありますが、気温は水星よりも高いです。',
'金星は反時計回りに自転しています。過去に起こった隕石の衝突が原因と言われています。',
'火星上から見ると、太陽の大きさは地球から見た場合の約半分に見えます。',
'木星の<sub alias="いちにち">1日</sub>は全惑星の中で一番短いです。',
'天の川銀河は約50億年後にアンドロメダ星雲と衝突します。',
],
},
};
const jpjpData = {
translation: {
SKILL_NAME: '日本語版豆知識',
},
};
const ptbrData = {
translation: {
SKILL_NAME: 'Fatos Espaciais',
},
};
const ptData = {
translation: {
SKILL_NAME: 'Fatos Espaciais',
GET_FACT_MESSAGE: 'Aqui vai: ',
HELP_MESSAGE: 'Você pode me perguntar por um fato interessante sobre o espaço, ou, fexar a skill. Como posso ajudar?',
HELP_REPROMPT: 'O que vai ser?',
FALLBACK_MESSAGE: 'A skill fatos espaciais não tem uma resposta para isso. Ela pode contar informações interessantes sobre o espaço, é só perguntar. Como posso ajudar?',
FALLBACK_REPROMPT: 'Eu posso contar fatos sobre o espaço. Como posso ajudar?',
ERROR_MESSAGE: 'Desculpa, algo deu errado.',
STOP_MESSAGE: 'Tchau!',
FACTS:
[
'Um ano em Mercúrio só dura 88 dias.',
'Apesar de ser mais distante do sol, Venus é mais quente que Mercúrio.',
'Visto de marte, o sol parece ser metade to tamanho que nós vemos da terra.',
'Júpiter tem os dias mais curtos entre os planetas no nosso sistema solar.',
'O sol é quase uma esfera perfeita.',
],
},
};
// constructs i18n and l10n data structure
const languageStrings = {
'de': deData,
'de-DE': dedeData,
'en': enData,
'en-AU': enauData,
'en-CA': encaData,
'en-GB': engbData,
'en-IN': eninData,
'en-US': enusData,
'es': esData,
'es-ES': esesData,
'es-MX': esmxData,
'es-US': esusData,
'fr': frData,
'fr-FR': frfrData,
'fr-CA': frcaData,
'hi': hiData,
'hi-IN': hiinData,
'it': itData,
'it-IT': ititData,
'ja': jpData,
'ja-JP': jpjpData,
'pt': ptData,
'pt-BR': ptbrData,
};