-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblackflag.js
More file actions
97 lines (92 loc) · 2.64 KB
/
Copy pathblackflag.js
File metadata and controls
97 lines (92 loc) · 2.64 KB
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
module.exports = function(RED) {
var request=require("request");
var scope="";
function connect(node){
var level=0;
var chunk;
scope="";
if(node.speech){
scope+="SPEECH";
level++;
}
if(node.tts){
if(level>0)
scope+=',';
scope+="TTS";
level++;
}
if(node.advert){
if(level>0)
scope+=',';
scope+="ADS";
level++;
}
if(node.inapp){
if(level>0)
scope+=",";
scope+="IMMN";
}
if(node.sms){
if(level>0)
scope+=",";
scope+="SMS";
}
if(node.mms){
if(level>0)
scope+=",";
scope+="MMS";
}
if(node.billing){
if(level>0)
scope+=',';
scope+="PAYMENT";
}
var headers={
'User-Agent':'AT&T Breeze Agent',
'Content-Type':'application/x-www-form-urlencoded'
}
var options = {
url: 'https://api.att.com/oauth/v4/token',
method: 'POST',
headers: headers,
form: {'client_id': node.key, 'client_secret': node.secret,'scope':scope,'grant_type':'client_credentials'}
};
request(options,function(error,response,body){
var msg=new Object();
if(!error&&response.statusCode==200){
var token=JSON.parse(body).access_token;
var token_name=node.name;
node.context.global[token_name]=token;
msg.payload=token_name+":"+token;
node.status({fill:"green",shape:"dot",text:"connected"});
node.send(msg);
}else{
msg.payload="ERROR:"+response.error +" Scope: "+scope;
node.send(msg);
return;
}
});
}
function BlackFlagNode(config) {
this.context = {global:RED.settings.functionGlobalContext || {}};
RED.nodes.createNode(this,config);
this.status({fill:"red",shape:"ring",text:"disconnected"});
var node = this;
this.name=config.name;
this.key=config.key;
this.secret=config.secret;
this.advert = config.advert;
this.speech = config.speech;
this.tts = config.tts;
this.inapp = config.inapp;
this.sms = config.sms;
this.mms = config.mms;
this.billing = config.billing;
setImmediate(connect,node);
this.on('input', function(msg) {
msg.payload = "";
node.send(msg);
});
}
RED.nodes.registerType("BlackFlag",BlackFlagNode);
}