-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (104 loc) · 3.53 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
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
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
/**
* This skill supplies users with information from the Mustang Band calendar.
* This skill supports English. (en-US).
**/
'use strict';
const Alexa = require('alexa-sdk');
const ical = require('ical');
const http = require('http');
const utils = require('util');
var alexa;
const APP_ID = undefined;
var cal = 'https://calendar.google.com/calendar/ical/9aqoirufc0h1mrihk78us5mlgs%40group.calendar.google.com/public/basic.ics';
var languageStrings = {
'en': {
'translation': {
'WELCOME_MESSAGE': 'Welcome to Mustang Band Reminders. What would you like to know?',
'HELP_MESSAGE': 'Welcome to Mustang Band Reminders. You can ask a question like, what are my upcoming band events?... This will let you know what the next Mustang Band event is. What would you like to know?',
'REPROMPT_MESSAGE': 'Sorry, I didn\'t get that. What would you like to know?',
'STOP_MESSAGE': 'Goodbye! 8-oh-7.',
'807_MESSAGE': 'The band always wins!'
}
}
};
function removeTags(str) {
if (str) {
return str.replace(/<(?:.|\n)*?>/gm, '');
}
}
function getUpcomingEvents(callback) {
var today = new Date();
var nextEvent = undefined;
ical.fromURL(cal, {}, function(err, data) {
for (var k in data) {
if (data.hasOwnProperty(k)) {
var ev = data[k];
var eventData = {
summary: removeTags(ev.summary),
location: removeTags(ev.location),
description: removeTags(ev.description),
start: ev.start
};
if (eventData.summary != undefined && eventData.start >= today)
if (nextEvent == undefined || eventData.start < nextEvent.start)
nextEvent = eventData;
}
}
callback(nextEvent);
});
}
function getNextEvent(eventList) {
var nextEvent = eventList[0];
for (var i = 0; i < eventList.length; i++) {
if (eventList[i].start < nextEvent.start)
nextEvent = eventList[i];
}
return nextEvent;
}
const handlers = {
'LaunchRequest': function () {
this.emit(':ask', this.t('WELCOME_MESSAGE'));
},
'BandEventsIntent': function () {
this.emit('BandEvents');
},
'BandEvents': function () {
getUpcomingEvents(function(output) {
var speechOutput = '';
speechOutput += 'Your next event is ';
speechOutput += output.summary;
speechOutput += ' on ';
var options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'};
options.timeZone = 'UTC';
var date = output.start.toLocaleString('en-US', options);
speechOutput += date;
alexa.emit(':tellWithCard', speechOutput, 'Mustang Band', speechOutput);
});
},
'BAWIntent': function() {
this.emit('BAW');
},
'BAW': function() {
this.emit(':tell', this.t('807_MESSAGE'));
},
'AMAZON.HelpIntent': function () {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('REPROMPT_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'));
},
};
exports.handler = function (event, context) {
alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};