-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
386 lines (349 loc) · 10 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
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
"use strict";
module.change_code = 1;
const _ = require("lodash");
const express = require("express");
const alexa = require("alexa-app");
const app = express();
// Setup the alexa app and attach it to express before anything else.
var alexaApp = new alexa.app("");
var AmazonDateParser = require("amazon-date-parser");
//My Helper Objects
var GPDataHelper = require("./gp_data_helper");
var GPPodcastHelper = require("./gp_podcast_helper");
const PORT = process.env.PORT || 3000;
function convertAmDate(amDate) {
// This function returns TODAY's date if the amDate is undefined.
if (_.isEmpty(amDate)) {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = "0" + dd;
}
if (mm < 10) {
mm = "0" + mm;
}
return yyyy + "-" + mm + "-" + dd;
} else {
var dateObj = new AmazonDateParser(amDate);
//We're going to assume if the user says 'This Week' they mean
//'the sermon at the first service which happens this week!
return dateObj.startDate.toString();
}
}
// POST calls to / in express will be handled by the app.request() function
alexaApp.express({
expressApp: app,
checkCert: true,
// sets up a GET route when set to true. This is handy for testing in
// development, but not recommended for production.
debug: true
});
app.set("view engine", "ejs");
alexaApp.launch(function(req, res) {
var prompt =
'Welcome to the Gympie Presbyterian Church Skill. You can say something like <emphasis level="moderate">play sermon.</emphasis>';
res
.say(prompt)
.reprompt(prompt)
.shouldEndSession(false);
});
alexaApp.intent(
"sermontitle",
{
slots: {
DATE: "AMAZON.DATE"
},
utterances: [
"{what the|tell me the} {sermon|message} {|title} {|is} {|on|for} {-|DATE}",
"{what is the} {sermon|message} {|title} {|on|for} {-|DATE}"
]
},
function(req, res) {
//get the slot
var date = convertAmDate(req.slot("DATE"));
var prompt;
var reprompt =
"I don't have a sermon title for " + date + " Please try again";
var gpHelper = new GPDataHelper();
return gpHelper
.getSermonTitle(date)
.then(function(sermonTitle) {
if (_.isUndefined(sermonTitle)) {
prompt = "Sorry. We haven't got a sermon title yet.";
} else {
prompt = "The sermon title is " + sermonTitle;
}
console.log(prompt);
res.say(prompt).send();
})
.catch(function(err) {
console.log(err.statusCode);
var prompt = "I don't have a sermon title for " + date.toString();
res
.say(prompt)
.reprompt(reprompt)
.shouldEndSession(false)
.send();
});
}
);
alexaApp.intent(
"sermonpassage",
{
slots: {
DATE: "AMAZON.DATE"
},
utterances: [
"{what the|tell me the} {sermon|message|preaching} {|bible} {passage|reading} {|is} {|on|for} {-|DATE}",
"{what is the} {sermon|message|preaching} {|bible} {passage|reading} {|on|for} {-|DATE}"
]
},
function(req, res) {
var reprompt = "I don't have a sermon passage. Please try again";
var prompt;
//get the slot
var date = convertAmDate(req.slot("DATE"));
var gpHelper = new GPDataHelper();
return gpHelper
.getSermonPassage(date)
.then(function(sermonPassage) {
if (_.isUndefined(sermonPassage)) {
prompt = "Sorry. We haven't got a sermon passage yet.";
} else {
prompt = "The sermon passage is " + sermonPassage;
}
console.log(prompt);
res.say(prompt).send();
})
.catch(function(err) {
console.log(err.statusCode);
var prompt = "I don't have a sermon passage for " + date.toDateString();
res
.say(prompt)
.reprompt(reprompt)
.shouldEndSession(false)
.send();
});
}
);
alexaApp.intent(
"otherpassage",
{
slots: {
DATE: "AMAZON.DATE"
},
utterances: [
"{what is the|tell me the} {other|second} {passage|reading|bible reading} {|is on|for} {-|DATE}"
]
},
function(req, res) {
//get the slot
var date = convertAmDate(req.slot("DATE"));
var prompt;
var reprompt =
'Tell me a date to get the other bible passage. You can say <emphasis level="moderate">this Sunday</emphasis> or <emphasis level="moderate">next Sunday</emphasis>.';
var gpHelper = new GPDataHelper();
return gpHelper
.getOtherPassage(date)
.then(function(otherPassage) {
if (_.isUndefined(otherPassage)) {
prompt = "Sorry. We haven't got an other bible passage yet.";
} else {
prompt = "The other passage is " + otherPassage;
}
console.log(prompt);
res.say(prompt).send();
})
.catch(function(err) {
console.log(err.statusCode);
var prompt =
"I don't have an other bible passage for " + date.toString();
res
.say(prompt)
.reprompt(reprompt)
.shouldEndSession(false)
.send();
});
}
);
alexaApp.intent(
"sermonseries",
{
slots: {
DATE: "AMAZON.DATE"
},
utterances: [
"{what is the|tell me the} {|sermon} {series} {|on|for} {-|DATE}"
]
},
function(req, res) {
//get the slot
var date = convertAmDate(req.slot("DATE"));
var prompt;
var reprompt = "I couldn't get the sermon series. Please try again.";
var gpHelper = new GPDataHelper();
return gpHelper
.getSermonSeries(date)
.then(function(sermonSeries) {
if (_.isUndefined(sermonSeries)) {
prompt = "Sorry. We haven't got a sermon series yet.";
} else {
prompt = "The sermon series is " + sermonSeries;
}
console.log(prompt);
res.say(prompt).send();
})
.catch(function(err) {
console.log(err.statusCode);
var prompt = "I didn't have a sermon series for " + date.toString();
res
.say(prompt)
.reprompt(reprompt)
.shouldEndSession(false)
.send();
});
}
);
alexaApp.intent(
"podcast",
{
slots: {},
utterances: ["{play|listen to} {podcast|sermon|message}"]
},
function(req, res) {
// retrieve the podcast Mpeg enclosure from the RSS feed
var podcast = new GPPodcastHelper();
//Since this is the first request for a sermon, get the latest episode '0'
return podcast.getEpisode(0).then(function(podEp) {
var stream = {
url: podEp.mp3URL,
token: 0,
offsetInMilliseconds: 0
};
res.say(podEp.preRoll);
res.audioPlayerPlayStream("REPLACE_ALL", stream).send();
});
}
);
alexaApp.intent("AMAZON.NextIntent", {}, function(req, res) {
// retrieve the podcast Mpeg enclosure from the RSS feed
var podcast = new GPPodcastHelper();
if (_.isUndefined(req.context.AudioPlayer.token)) {
res.say("Something has gone wrong skipping to the next track!").send();
return;
} else {
var episode = Number(req.context.AudioPlayer.token);
}
if (episode < 9) {
var nextEp = episode + 1;
} else {
res
.say("No more sermons here. For more sermons, visit our website.")
.send();
return;
}
return podcast.getEpisode(nextEp).then(function(podEp) {
var stream = {
url: podEp.mp3URL,
token: nextEp,
offsetInMilliseconds: 0
};
res.say(podEp.preRoll);
res.audioPlayerPlayStream("REPLACE_ALL", stream).send();
});
});
alexaApp.intent("AMAZON.PreviousIntent", {}, function(req, res) {
// retrieve the podcast Mpeg enclosure from the RSS feed
var podcast = new GPPodcastHelper();
if (_.isUndefined(req.context.AudioPlayer.token)) {
res.say("Something has gone wrong skipping to the next track!").send();
return;
} else {
var episode = Number(req.context.AudioPlayer.token);
}
if (episode > 0) {
var prevEp = episode - 1;
} else {
res
.say("No more sermons here. For more sermons, visit our website.")
.send();
return;
}
return podcast.getEpisode(prevEp).then(function(podEp) {
var stream = {
url: podEp.mp3URL,
token: prevEp,
offsetInMilliseconds: 0
};
res.say(podEp.preRoll);
res.audioPlayerPlayStream("REPLACE_ALL", stream).send();
});
});
alexaApp.intent("AMAZON.PauseIntent", {}, function(req, res) {
console.log("app.AMAZON.PauseIntent");
res
.audioPlayerStop()
.shouldEndSession(false)
.send();
});
alexaApp.intent("AMAZON.ResumeIntent", {}, function(req, res) {
console.log("app.AMAZON.ResumeIntent");
if (
req.context.AudioPlayer.offsetInMilliseconds > 0 &&
req.context.AudioPlayer.playerActivity === "STOPPED"
) {
var episode = req.context.AudioPlayer.token;
var podcast = new GPPodcastHelper();
return podcast.getEpisodeURL(episode).then(function(URL) {
res.audioPlayerPlayStream("REPLACE_ALL", {
token: episode,
url: URL,
offsetInMilliseconds: req.context.AudioPlayer.offsetInMilliseconds
});
});
res.send();
}
});
alexaApp.intent(
"AMAZON.HelpIntent",
{
slots: {},
utterances: []
},
function(req, res) {
var helpOutput =
"Welcome to the Gympie Presbyterian Church skill. You can play the latest sermon by saying 'play sermon', or you can find out about sermon series by asking 'what is the sermon series'.";
var reprompt = "What would you like to do?";
// AMAZON.HelpIntent must leave session open -> .shouldEndSession(false)
res
.say(helpOutput)
.reprompt(reprompt)
.shouldEndSession(false);
}
);
alexaApp.intent(
"AMAZON.StopIntent",
{
slots: {},
utterances: []
},
function(req, res) {
var stopOutput = "Good bye. Thanks for using Gympie Presbyterian on Alexa.";
res.say(stopOutput);
}
);
alexaApp.intent(
"AMAZON.CancelIntent",
{
slots: {},
utterances: []
},
function(req, res) {
var cancelOutput = "No problem. Request cancelled.";
res.say(cancelOutput);
}
);
module.exports = alexaApp;
app.listen(PORT, () => console.log("Listening on port " + PORT + "."));