-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (91 loc) · 4.14 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
'use strict';
var helpers = require('./scripts/helpers.js');
// Load configuration
var cfg = require('config');
if(cfg) {
console.log('Successfully loaded environment variables!');
}
else {
console.log('Failed to load environment variables!');
}
// Setup logging
/* jshint ignore:start */
var logger = require('./scripts/logger.js');
/* jshint ignore:end */
/**
* Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback) {
var intent = intentRequest.intent,
intentName = intentRequest.intent.name;
logger.info('Routing to intent handler: %s', intentName);
// Keep track of what the user most recently was trying to do. This will be helpful for reprompting users for information
session.attributes = helpers.setSessionValue(session, 'previousIntent', helpers.getSessionValue(session, 'recentIntent'));
session.attributes = helpers.setSessionValue(session, 'recentIntent', intentName);
session.attributes = helpers.setSessionValue(session, 'recentIntentSuccessful', false);
// Define the route to your skill's intent handler
var route = require('./scripts/intent' + intentName + '.js');
// Dispatch to your skill's intent handlers
route.executeIntent(intent, session, callback);
}
/**
* Handler for AWS Lambda requests. Called for every request to your Lambda function.
* Route the incoming request based on type (LaunchRequest, IntentRequest,
* etc.) The JSON body of the request is provided in the event parameter.
*/
exports.handler = function (event, context) {
try {
logger.event = event;
logger.debug('applicationId: %s', event.session.application.applicationId);
logger.debug('sessionId: %s', event.session.sessionId);
logger.debug('Alexa Request Event', event);
logger.silly('Alexa Context', context);
/**
* Uncomment this if statement and populate with your skill's application ID to
* prevent someone else from configuring a skill that sends requests to this function.
*/
/*
if (event.session.application.applicationId !== cfg.get('global.applicationId')) {
context.fail('Invalid Application ID: ' + event.session.application.applicationId);
return;
}
*/
/**
* Define routes to handlers based on request type
*/
var route = null;
// Logic for initializing a new session is contained in sessionStart.js
if (event.session.new) {
logger.info('session=%s: started new sesion', event.session.sessionId);
route = require('./scripts/sessionStart.js');
event.session = route.onSessionStarted({requestId: event.request.requestId}, event.session);
}
// Handler for 'LaunchRequest' is called when the user launches the skill without specifying what they want.
if (event.request.type === 'LaunchRequest') {
logger.info('Received a launch request, routing to onLaunch handler');
route = require('./scripts/onLaunch.js');
route.onLaunch(event.request,
event.session,
function callback(session, speechletResponse) {
context.succeed(helpers.buildResponse(session, speechletResponse));
});
}
// Intent requests are handled via onIntent method
else if (event.request.type === 'IntentRequest') {
onIntent(event.request,
event.session,
function callback(session, speechletResponse) {
context.succeed(helpers.buildResponse(session, speechletResponse));
});
}
// Logic for ending a session is contained in sessionEnd.js
else if (event.request.type === 'SessionEndedRequest') {
logger.info('Received a session end request, routing to onSessionEnded handler');
route = require('./scripts/sessionEnd.js');
route.onSessionEnded(event.request, event.session);
context.succeed();
}
} catch (e) {
context.fail('Exception: ' + e);
}
};