This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (107 loc) · 4.37 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
'use strict';
require('dotenv').config();
const Alexa = require('ask-sdk-core');
const axios = require('axios');
const moment = require('moment');
const BASE_URL = process.env.API_CALL_URL;
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speechText = 'Palmyra Racing Association can tell you about things going on at Palmyra Racing Association.';
return speakAndShowCard(handlerInput, speechText);
}
};
const AllEventsIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AllEventsIntent';
},
async handle(handlerInput) {
let allEventsResponse = await axios.get(BASE_URL + '/latest/events/thisyear');
let allEvents = allEventsResponse.data;
console.log((allEvents));
let eventsText = buildSpeakableDates(allEvents);
return speakAndShowCard(handlerInput, eventsText.join('. '));
}
};
const NextEventIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'NextEventIntent';
},
async handle(handlerInput) {
let allEventsResponse = await axios.get(BASE_URL + '/latest/events/next');
let allEvents = allEventsResponse.data;
console.log((allEvents));
let eventsText = buildSpeakableDates(allEvents);
return speakAndShowCard(handlerInput, eventsText.join('. '));
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = 'I can help you figure out what to ask Palmyra Racing Association. Ask things like "whens the next race"';
return speakAndShowCard(handlerInput, speechText);
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
return speakAndShowCard(handlerInput, 'Goodbye!');
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
//any cleanup logic goes here
return handlerInput.responseBuilder.getResponse();
}
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
return handlerInput.responseBuilder
.speak('Sorry, I can\'t understand the command. Please say again.')
.reprompt('Sorry, I can\'t understand the command. Please say again.')
.getResponse();
},
};
function buildSpeakableDates(allEvents) {
let eventsText = [];
eventsText.push('All races have a practice on the Saturday before.')
for (let event of allEvents) {
// this is a hack to put the dates on the right day since dates coming back from the
// API are in UTC+0 but they are actually meant for UTC+4.
let speakableDate = moment(event.date).utcOffset(0).format('dddd, MMMM Do YYYY');
eventsText.push('On ' + speakableDate + ' there is a ' + event.type + '. This is the ' + event.event_name + '.');
}
return eventsText;
}
function speakAndShowCard(handlerInput, speechText) {
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Palmyra Racing Association', speechText)
.getResponse();
}
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(LaunchRequestHandler,
AllEventsIntentHandler,
NextEventIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler)
.lambda();