forked from raweee/node-red-contrib-whatsapp-link
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
185 lines (160 loc) · 6.95 KB
/
admin.js
File metadata and controls
185 lines (160 loc) · 6.95 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module.exports = function(RED) {
const QRCode = require('qrcode');
function WhatsappAdmin(config) {
RED.nodes.createNode(this,config);
var node = this;
var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink);
node.client = whatsappLinkNode.client;
function SetStatus(WAStatus, color){
node.status({fill:color,shape:"dot",text:WAStatus});
msg = {payload : WAStatus};
node.send(msg);
};
// Commands recived for Whatsapp Admin.
this.on('input', async function(msg, send){
if (msg.payload === "destroy") {
if(node.client.clientType === "waWebClient"){
node.clinet.WAClose();
SetStatus("Disconnected","red");
} else {
var client = await node.client;
client.end();
SetStatus("Disconnected","red");
}
}
else if (msg.payload==="logout") {
if(node.client.clientType === "waWebClient"){
node.client.logout();
SetStatus("Logged Out","red");
} else{
var client = await node.client;
client.logout();
SetStatus("Disconnected","red");
}
}
else if (msg.payload === "test"){
if(node.client.clientType === "waWebClient"){
msg.payload = await node.client.getState();
node.send(msg);
} else {
var client = await node.client;
await client.sendPresenceUpdate('available')
SetStatus("Presence Avilable","green");
}
}
else if (msg.payload === "restart"){
if(node.client.clientType === "waWebClient"){
node.client.WARestart();
SetStatus("Connecting...", "yellow");
} else {
var client = await node.client;
client.end();
SetStatus("Restarting...", "yellow");
setTimeout(() => {
SetStatus("Please Deploy all nodes to subscribe for messgaes", "yellow");
node.client.clientStartFunction();
}, 3000)
}
};
});
if(node.client.clientType === "waWebClient"){
//Group Add/leave status-----
node.client.on('group_join', async (notification)=>{
msg.chat = await notification.getChat();
msg.payload = msg.chat.name;
msg.chatID = msg.chat.id.user || `No ID Avilable`;
msg.type = notification.type;
msg.notification = notification;
node.send(msg);
// notification.reply('!Node-Red joined');
});
node.client.on('group_leave', async (notification)=>{
msg.chat = await notification.getChat();
msg.payload = msg.chat.name;
msg.type = notification.type;
msg.notification = notification;
node.send(msg);
});
node.client.on('group_update', (msg)=>{
node.send(msg);
});
//whatsapp Status Parameters----
SetStatus("Connecting whatsapp...", "yellow");
node.client.on('qr', (qr) => {
SetStatus("Scan QR code to connect.", "yellow");
QRCode.toDataURL(qr, function(err, url){
msg = {payload : url};
node.send(msg);
let qrImageWithID = {};
qrImageWithID.id = node.id;
qrImageWithID.image = url;
RED.comms.publish("whatsappLinkQrCode", qrImageWithID);
});
});
node.client.on('auth_failure', () => {
SetStatus('Connection Fail.','red');
});
node.client.on('loading_screen', () => {
SetStatus('Connecting...','yellow');
let qrImageWithID = {};
qrImageWithID.id = node.id;
qrImageWithID.image = null;
RED.comms.publish("whatsappLinkQrCode", qrImageWithID);
});
node.client.on('ready', () => {
SetStatus('Connected','green');
});
node.client.on('disconnected', () => {
SetStatus("Disconnected","red");
});
};
if(node.client.clientType === "waSocketClient"){
var client = null
async function clientFromWhatsappLite(){
client = await node.client;
client.ev.on('connection.update', (updates)=>{
function printQrCode(urlQr) {
var qrImageWithID = {};
qrImageWithID.id = node.id;
qrImageWithID.image = urlQr;
RED.comms.publish("whatsappLinkQrCode", qrImageWithID);
}
if(updates.qr){
QRCode.toDataURL(updates.qr, function(err, url){
printQrCode(url);
});
QRCode.toString(updates.qr, {type : 'terminal', small:true }, function(err, QRTerminal){
node.log(`To Connect, Scan the QR Code through your Whatsapp Mobile App.`)
console.log("");
console.log(QRTerminal);
});
}
//Setting conncetion status indication
var {connection} = updates
if(connection === 'open'){
printQrCode(null);
SetStatus("Connected", "green");
}
else if(updates.isOnline){
printQrCode(null);
SetStatus("Connected", "green");
}
else if(connection === 'close'){
SetStatus("Disconnected", "red");
}
else if(connection === 'connecting'){
SetStatus("Connecting...", "yellow");
}
else if(updates.qr){
SetStatus("Scan QR Code to Connect.", "yellow");
}
})
}
clientFromWhatsappLite();
};
this.on(`close`, ()=>{
SetStatus("Disconnected", "red");
});
}
RED.nodes.registerType("admin", WhatsappAdmin);
}