-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5-netpie-push.js
81 lines (73 loc) · 1.94 KB
/
5-netpie-push.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
module.exports = function(RED) {
function jsonparse(jstr) {
try {
var out = JSON.parse(jstr);
return out;
}
catch(e) {
return "";
}
}
function NetpiePushNode(config) {
var rest = require('restler');
RED.nodes.createNode(this, config);
var node = this;
node.on('input', function(msg) {
var auth = config.auth.split(':');
var body = config.bodyType=='str'?config.body:(msg[config.body]||'');
var restbody = {
body: body.toString(),
title: config.titleType=='str'?config.title:(msg[config.title]||''),
sound: config.sound || 'default'
}
switch (config.targetType) {
case '1' :
if (body) {
rest.put("https://api.netpie.io/push/owner", {
headers: {"Content-Type": "text/plain"},
username: auth[0],
password: auth[1],
data: JSON.stringify(restbody)
}).on('complete', function(data) {
var msg = {
payload : data,
};
node.send(msg);
});
}
break;
case '2' :
if (body) {
rest.put("https://api.netpie.io/push/group/"+config.appGroup, {
headers: {"Content-Type": "text/plain"},
username: auth[0],
password: auth[1],
data: JSON.stringify(restbody)
}).on('complete', function(data) {
var msg = {
payload : data,
};
node.send(msg);
});
}
break;
case '3' :
if (body) {
rest.put("https://api.netpie.io/push/mobiletopic/"+config.mobileAppKey+'/'+config.mobileAppTopic, {
headers: {"Content-Type": "text/plain"},
username: auth[0],
password: auth[1],
data: JSON.stringify(restbody)
}).on('complete', function(data) {
var msg = {
payload : data,
};
node.send(msg);
});
}
break;
}
});
}
RED.nodes.registerType("push",NetpiePushNode);
}