-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
72 lines (70 loc) · 3.02 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
var Alexa = require('alexa-sdk');
const util = require('util');
const esp8266 = require('./esp8266');
esp8266.setup();
var handlers = {
'TurnOnIntent': function () {
var intent = this.event.request.intent;
let displayText = intent.slots.Device.value.toString();
if (intent.slots.Device.resolutions.resolutionsPerAuthority != undefined) {
let resolutions = intent.slots.Device.resolutions.resolutionsPerAuthority;
let success_code = resolutions[0].status.code;
if (success_code.toString().replace(/[']/g, "") == "ER_SUCCESS_MATCH") {
let resolvedDeviceName = resolutions[0].values[0].value.name.toString();
let alexa = this
let alexaEmit = function() {
alexa.emit(':tell', 'As you wish.');
}
esp8266.displayText(resolvedDeviceName, 'ON', alexaEmit);
} else {
this.emit(':tell', "Sorry, I couldn't figure out which device you meant");
}
} else {
console.log(util.inspect(intent.slots.Device, false, null));
}
},
'TurnOffIntent': function () {
var intent = this.event.request.intent;
let displayText = intent.slots.Device.value.toString();
if (intent.slots.Device.resolutions.resolutionsPerAuthority != undefined) {
let resolutions = intent.slots.Device.resolutions.resolutionsPerAuthority;
let success_code = resolutions[0].status.code;
if (success_code.toString().replace(/[']/g, "") == "ER_SUCCESS_MATCH") {
let resolvedDeviceName = resolutions[0].values[0].value.name.toString();
let alexa = this
let alexaEmit = function() {
alexa.emit(':tell', 'As you wish.');
}
esp8266.displayText(resolvedDeviceName, 'OFF', alexaEmit);
} else {
this.emit(':tell', "Sorry, I couldn't figure out which device you meant");
}
} else {
console.log(util.inspect(intent.slots.Device, false, null));
}
},
'SleepIntent': function () {
let alexa = this
let alexaEmit = function() {
alexa.emit(':tell', 'Goodnight Sam. Sleep well. Most likely kill you in the morning.');
}
let emptyResponse = function(){};
esp8266.displayText('desk', 'OFF', emptyResponse);
esp8266.displayText('nightstand', 'OFF', emptyResponse);
esp8266.displayText('other lamp', 'OFF', alexaEmit);
},
'LightsOnIntent': function () {
let alexa = this
let alexaEmit = function() {
alexa.emit(':tell', 'Howdie doo Sam.');
}
let emptyResponse = function(){};
esp8266.displayText('desk', 'ON', emptyResponse);
esp8266.displayText('nightstand', 'ON', alexaEmit);
}
};
exports.handler = function(event, context, callback) {
let alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};