-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
137 lines (124 loc) · 4.89 KB
/
handler.js
File metadata and controls
137 lines (124 loc) · 4.89 KB
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
import { checkNEOs } from './libs/nasa';
function buildResponse(sessionAttributes, speechletResponse) {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
};
}
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
return {
outputSpeech: {
type: 'PlainText',
text: output
},
card: {
type: 'Simple',
title: title,
content: output
},
reprompt: {
outputSpeech: {
type: 'PlainText',
text: repromptText
}
},
shouldEndSession: shouldEndSession
};
}
// Welcome
function getWelcomeResponse(callback) {
// If we wanted to initialize the session to have some attributes we could add those here.
const sessionAttributes = {};
const cardTitle = 'Welcome to Space Ball';
const speechOutput = 'Welcome to Space Ball. Find out if you are in danger from near earth objects by asking, Space Ball are there astroids close by or, Space Ball, are we all going to die today?';
// If the user either does not reply to the welcome message or says something that is not
// understood, they will be prompted again with this text.
const repromptText = 'You can also ask me, Space Ball are we all going to die.';
const shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
// Help
function getHelpResponse(callback) {
const sessionAttributes = {};
const cardTitle = 'Help';
const speechOutput = 'SpaceBall contacts NASA to find objects near the earth for a given day. You can ask, Space Ball list near earth objects for July 4th, 2017 or Space Ball what\'s out there and it will reply with information about objects that NASA currently monitors including their approximate size, speed and distance from the earth';
const repromptText = 'You can also ask Space Ball, Space Ball are we all going to die? and Space Ball what\s up?';
const shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
// Intent matches
function onIntent(intentRequest, session, callback) {
const intent = intentRequest.intent;
const intentName = intentRequest.intent.name;
console.log(JSON.stringify(intent));
console.log(`intent name: ${intentName}`);
// Dispatch to your skill's intent handlers
if('AMAZON.HelpIntent' === intentName) {
getHelpResponse(callback);
} else if('AMAZON.StopIntent' === intentName ||
'AMAZON.CancelIntent' === intentName) {
const cardTitle = intent.name;
const sessionAttributes = {};
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, '', '', true));
} else if('SpaceBall' === intentName) {
checkNEOs(intent.slots.Date.value, (speechOutput) => {
const cardTitle = intent.name;
const sessionAttributes = {};
const shouldEndSession = true;
const repromptText = '';
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
} else {
throw 'Invalid intent';
}
}
// Called when the user ends the session.
// Is not called when the skill returns shouldEndSession=true.
function onSessionEnded(sessionEndedRequest, session) {
console.log(`End session: requestId=${request.requestId} sessionId=${session.sessionId}`);
}
// Called when the session starts.
function onSessionStarted(request, session) {
console.log(`New session: requestId=${request.requestId} sessionId=${session.sessionId}`);
}
// Called when the user launches the skill without specifying what they want.
function onLaunch(launchRequest, session, callback) {
getWelcomeResponse(callback);
}
// Handle the incoming request based on type (LaunchRequest, IntentRequest)
module.exports.handler = function(event, context, callback) {
try {
// Verify application id
console.log(`session : ${event.session.application.applicationId}`);
console.log(`process : ${process.env.APPLICATION_ID}`);
if(event.session.application.applicationId !== process.env.APPLICATION_ID) {
return callback('Invalid Application ID');
}
if(event.session.new) {
onSessionStarted(event.request, event.session);
}
if(event.request.type === 'LaunchRequest') {
onLaunch(event.request,
event.session,
(sessionAttributes, speechletResponse) => {
callback(null, buildResponse(sessionAttributes, speechletResponse));
});
} else if(event.request.type === 'IntentRequest') {
onIntent(event.request,
event.session,
(sessionAttributes, speechletResponse) => {
callback(null, buildResponse(sessionAttributes, speechletResponse));
});
} else if(event.request.type === 'SessionEndedRequest') {
onSessionEnded(event.request, event.session);
callback(null);
}
} catch(e) {
callback(`Error: ${e}`);
}
};