-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRouter.js
More file actions
112 lines (107 loc) · 3.57 KB
/
Copy pathRouter.js
File metadata and controls
112 lines (107 loc) · 3.57 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
module.exports = function() {
return new Router();
}
function Router() {
var self = this;
self.intents = {};
self.applicationId = null;
self.intent = function(intent_name, callback) {
self.intents[intent_name] = callback;
};
self.launch = function(callback) {
self.launch_handler = callback;
};
self.handler = handler;
function handler(event, context) {
try {
if(self.applicationId && !validateApplicationId(event.session.application.applicationId)) {
console.log("ApplicationId validation failure - from event.session.application.applicationId=" + JSON.stringify(event.session.application.applicationId));
return context.fail("Invalid Application ID");
}
console.log("Request event: " + JSON.stringify(event));
if (event.session.new) {
console.log("session_started requestId=" + event.request.requestId + ", sessionId=" + event.session.sessionId);
if(self.session_started) {
return self.session_started(event, context, function() {
handle_request(event, context);
});
}
}
if (event.request.type === "SessionEndedRequest") {
console.log("session_ended requestId=" + event.request.requestId + ", sessionId=" + event.session.sessionId);
if(self.session_ended) {
return session_ended(event, context, function() {
context.done();
});
}
return context.succeed();
}
handle_request(event, context);
}
catch (e) {
if(e.stack) {
console.log("Caught unhandled stack-trace error: " + e.stack);
}
else {
console.log("Caught unhandled error: " + JSON.stringify(e));
}
context.fail("An error occurred.");
}
}
function handle_request(event, context) {
var handler;
var params = {};
if (event.request.type === "LaunchRequest") {
console.log("LaunchRequest.");
if(!self.launch_handler) {
console.log("ERROR: No Launch handler defined!");
return context.fail("Launch Request not registered.");
}
if(self.launch_handler.length === 2) {
return handler(event, function(response) {
// TODO - Handle shortcut cases where the response is a string and return as text (ending session)
context.succeed(response.render());
});
}
else {
// TODO - Handle shortcut cases where the response is a string and return as text (ending session)
return context.succeed(self.launch_handler(event).render());
}
}
else if (event.request.type === "IntentRequest") {
console.log("IntentRequest - " + event.request.intent.name);
for(var key in event.request.intent.slots) {
params[key] = event.request.intent.slots[key].value;
}
handler = self.intents[event.request.intent.name];
if(!handler) {
console.log("Intent - " + event.request.intent.name + " not registered.");
return context.fail("Invalid Intent");
}
if(handler.length === 3) {
// TODO - Handle shortcut cases where the response is a string and return as text (ending session)
return handler(params, event, function(response) {
context.succeed(response.render());
});
}
else {
// TODO - Handle shortcut cases where the response is a string and return as text (ending session)
return context.succeed(handler(params, event).render());
}
}
};
function validateApplicationId(applicationId) {
var requestingApplicationId = applicationId.replace("^amzn1.echo-sdk-ams.app.","");
if(Array.isArray(self.applicationId)) {
if(self.applicationId.indexOf(requestingApplicationId)==-1) {
return false;
}
}
else if(typeof self.applicationId === "string") {
if(requestingApplicationId !== self.applicationId) {
return false;
}
}
return true;
}
}