-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (77 loc) · 2.48 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
const amazon = require('./src/amazon')
const alexa = require('./src/alexa')
const APP = 'My App'
const DESCRIPTION = 'My App Description'
const SPEECH = {
launch: 'Launching your custom Alexa skill',
sayHello: 'Hello World',
sayGoodbye: 'Goodbye World',
}
const handleInit = (event, context) => {
console.log('Initialized')
}
const handleLaunch = (event, context) => {
const cardTitle = APP
const cardDescription = DESCRIPTION
const wordsToSay = SPEECH.launch
const action = alexa.speakAndUpdateCard(cardTitle, cardDescription, wordsToSay, true)
amazon.dispatch(event, context, action)
}
const handleIntent = (event, context) => {
const intent = event.request.intent
let wordsToSay
let action
switch (intent.name) {
case 'Hello':
wordsToSay = SPEECH.sayHello
// create your speech object / lambda response
action = alexa.speak(wordsToSay, true)
// end lambda callback with response
amazon.dispatch(event, context, action)
break
case 'Goodbye':
wordsToSay = SPEECH.sayGoodbye
action = alexa.speak(wordsToSay, true)
amazon.dispatch(event, context, action)
break
default:
}
}
const handleEndSession = (event, context) => {
// cleanup
}
/*
Amazon Lifecycle --------------------------------------------------------------------
- onStartSession - occcurs before launch for new sessions only
- onLaunch - when user launches app but does not ask it to danythin
- onIntent - when user asks app to perform action
- onSessionEnd - when user says exit / no matching intent is found / an error occurs
*/
const LAUNCH = 'LaunchRequest'
const INTENT = 'IntentRequest'
const END = 'SessionEndedRequest'
const handler = (event, context, cb) => {
// While a Lambda function is executing, it can interact with AWS Lambda to get useful runtime information
// https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
try {
// runs once on startup
amazon.onStartSession(event, context, handleInit)
const requestType = event.request.type
switch (requestType) {
case LAUNCH:
amazon.onLaunch(event, context, handleLaunch)
break
case INTENT:
amazon.onIntent(event, context, handleIntent)
break
case END:
amazon.onSessionEnd(event, context, handleEndSession)
break
default:
}
} catch (e) {
const action = { error: `Exception: ${e}` }
amazon.dispatch(event, context, action)
}
}
module.exports = { handler }