-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·99 lines (91 loc) · 3.33 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
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
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
'use strict';
const Alexa = require('./node_modules/alexa-sdk');
var http = require('http');
const APP_ID = undefined; // TODO replace with your app ID (OPTIONAL).
const languageStrings = {
'en': {
translation: {
"STOP_MESSAGE": "Guten Appetit"
},
},
'de': {
translation: {
"STOP_MESSAGE": "Guten Appetit"
},
},
};
const handlers = {
'LaunchRequest': function() {
var responseString = '',
mythis = this,
speechOutput = '',
menuObj,
output,
nomenu,
dataUri = 'http://kantine.telegrafenberg.de/speiseplan/api.php?lang=de&date=today',
date = new Date;
if (date.getHours() > 14) {
dataUri = 'http://kantine.telegrafenberg.de/speiseplan/api.php?lang=de&date=tomorrow';
}
http.get(dataUri, (res) => {
res.on('data', (d) => {
responseString += d;
});
res.on('end', function(res) {
menuObj = JSON.parse(responseString);
if(menuObj.hasOwnProperty('weekday')){
output(menuObj);
} else {
nomenu();
}
});
}).on('error', (e) => {
console.error(e);
});
output = function(menuObj) {
if (menuObj) {
speechOutput += 'Hier das Essen für ' + menuObj.weekday + ' auf dem Telegrafenberg <break time="0.5s"/>';
for (var prop in menuObj.menu) {
if (menuObj.menu.hasOwnProperty(prop)) {
speechOutput += prop + '<break time="0.5s"/>';
speechOutput += menuObj.menu[prop].title.replace(/&/g, " und ") + '<break time="1s"/>';
};
}
speechOutput += '<break time="1s"/> Das war der Speiseplan für '+ menuObj.weekday + '<break time="1s"/> Guten Appetit';
mythis.emit(':tell', speechOutput);
}
},
nomenu = function(){
speechOutput = 'Entschuldige, leider liegt kein Speiseplan vor.';
mythis.emit(':tell', speechOutput);
}
},
'AMAZON.HelpIntent': function() {
this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
},
'AMAZON.RepeatIntent': function() {
this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
},
'AMAZON.StopIntent': function() {
this.emit('SessionEndedRequest');
},
'AMAZON.CancelIntent': function() {
this.emit('SessionEndedRequest');
},
'SessionEndedRequest': function() {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'Unhandled': function() {
this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
},
};
exports.handler = function(event, context) {
const alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};