-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (67 loc) · 2.24 KB
/
index.js
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
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
'use strict';
const Alexa = require('alexa-sdk');
const APP_ID = process.env.APP_ID;
const contentful = require('contentful').createClient({
space: process.env.SPACE,
accessToken: process.env.TOKEN
})
const languageStrings = {
'en': {
translation: {
JOKE_START: 'Here\'s a joke about',
SPORT_NOT_FOUND: 'Sorry, I couldn\'t find a joke about',
HELP_MESSAGE: 'Tell me a sport',
STOP_MESSAGE: 'I\'ll stop now'
}
}
}
const handlers = {
'Joke': function() {
// Get a random space fact from the space facts list
// Use this.t() to get corresponding language data
const sport = this.event.request.intent.slots.sport.value
console.log(sport)
contentful.getEntries({
"content_type": 'joke',
"fields.sport.sys.contentType.sys.id": 'sport',
"fields.sport.fields.name[match]": sport
}).then((response) => {
if (response.items.length) {
let joke = response.items[Math.floor(Math.random() * response.items.length)]
this.emit(':tell', `${this.t('JOKE_START')} ${sport}: ${joke.fields.body}`);
} else {
contentful.getEntries({"content_type": 'joke',}).then((response) => {
let joke = response.items[Math.floor(Math.random() * response.items.length)]
let _sport = joke.fields.sport.fields.name
this.emit(':tell', `${this.t('SPORT_NOT_FOUND')} ${sport}, but ${this.t('JOKE_START')} ${_sport}: ${joke.fields.body}`);
})
}
})
},
'AMAZON.HelpIntent': function() {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('HELP_MESSAGE');
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function() {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'AMAZON.StopIntent': function() {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'SessionEndedRequest': function () {
console.log('SessionEndedRequest')
},
'Unhandled': function() {
console.log('Unhandled')
}
};
exports.handler = function(event, context) {
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};