forked from jonferreira/node-red-contrib-alexa-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alexa-command.js
126 lines (89 loc) · 4.48 KB
/
alexa-command.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
var debug = false;
module.exports = function(RED) {
var AlexaCommandNode;
var Alexa = require('alexa-remote2');
AlexaCommandNode = function(config) {
var key, node, value;
RED.nodes.createNode(this, config);
node = this;
for (key in config) {
value = config[key];
node[key] = value;
}
this.account = RED.nodes.getNode(config.account);
if(debug) {node.warn("1");}
if(debug) {node.warn(this);}
this.on('input', function(msg) {
//return function(msg) {
var body, req, request;
node.status({
fill: "grey",
shape: "dot",
text: "connecting"
});
var alexa = new Alexa();
if(debug) {node.warn("2");}
if(debug) {node.warn(node);}
alexa.init({
cookie: node.account.cookie, // cookie if already known, else can be generated using email/password
email: node.account.email, // optional, amazon email for login to get new cookie
password: node.account.password, // optional, amazon password for login to get new cookie
logger: console.log, // optional
alexaServiceHost: node.account.alexaServiceHost || 'pitangui.amazon.com', // optional, e.g. "pitangui.amazon.com" for amazon.com, default is "layla.amazon.de"
//userAgent: '...', // optional, override used user-Agent for all Requests and Cookie determination
acceptLanguage: node.account.acceptLanguage || 'en-UK', // optional, override Accept-Language-Header for cookie determination
amazonPage: node.account.amazonPage || 'amazon.com' // optional, override Amazon-Login-Page for cookie determination and referer for requests
//useWsMqtt: false // optional, true to use the Websocket/MQTT direct push connection
},
function (err) {
if (err) {
node.status({
shape: "dot",
fill: "red",
text : "Error: " + err
});
msg.payload = err;
msg.error = true;
node.send(msg);
}
if(debug) {node.warn("3");}
if(debug) {node.warn(msg.payload.serialOrName || node.serialOrName);}
if(debug) {node.warn(msg.payload.text || node.text);}
var value;
if (msg.payload.command == "volume" || node.command == "volume") {
value = (msg.payload.volume || node.volume);
} else if (msg.payload.command == "shuffle" || node.command == "shuffle") {
value = (msg.payload.action || node.action);
} else if (msg.payload.command == "repeat" || node.command == "repeat") {
value = (msg.payload.action || node.action);
}
alexa.sendMessage((msg.payload.serialOrName || node.serialOrName), (msg.payload.command || node.command), value, function(error, result){
if (err) {
node.status({
shape: "dot",
fill: "red",
text : "Error: " + err
});
msg.payload = err;
msg.error = true;
node.send(msg);
} else {
if(debug) {node.warn("4");}
if(debug) {node.warn(result);}
node.status({
shape: "dot",
fill: "green",
text : "Success"
});
msg.payload = result;
msg.error = false;
node.send(msg);
}
});
}
)
//};
})
};
RED.nodes.registerType("Alexa-command", AlexaCommandNode);
};