-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (135 loc) · 4.9 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const baseDomain = 'http://blynk-cloud.com/[YOUR KEY HERE]'
// const Alexa = require('alexa-sdk');
const Alexa = require('ask-sdk-core');
var http = require('http');
function makeRequest(path){
return new Promise(((resolve, reject) => {
http.get(`${baseDomain}${path}`, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve();
});
}).on("error", (err) => {
reject(err);
});
}));
}
const valuesMap = {
"confetti" : 1,
"arcade" :4,
"juggle" : 3,
"jungle" : 3,
"dots" : 2,
"dot" : 2,
"off" : 0,
"loud" : 5,
"crazy" : 5,
"random" : 6
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
const NeoIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'NeoIntent';
},
async handle(handlerInput) {
let lightName = handlerInput.requestEnvelope.request.intent.slots.animation.value;
// let lightVal = (valuesMap[lightName] === 6) ? (getRandomInt(3)+1) : valuesMap[lightName];
let lightVal = valuesMap[lightName];
await makeRequest(`/update/v0?value=${lightVal}`);
const speakOutput = `Let's get the ${lightName} light started.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speakOutput = 'Welcome, ask me how to work your box lights.';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
let names = "";
for(let key in valuesMap){
names += key +",";
}
const speakOutput = `Welcome, ask me to run a light show with names like, juggle, dots, confetti, and crazy.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
async handle(handlerInput) {
await makeRequest(`/update/v0?value=0`);
const speakOutput = `Bummer. Stopping box lights.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
// Any cleanup logic goes here.
return handlerInput.responseBuilder.getResponse();
}
};
// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`~~~~ Error handled: ${error.message}`);
const speakOutput = `Sorry, I couldn't understand what you said. Please try again.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
NeoIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
// IntentReflectorHandler
) // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
.addErrorHandlers(
ErrorHandler)
.lambda();