forked from raweee/node-red-contrib-whatsapp-link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group-out.js
189 lines (170 loc) · 7.14 KB
/
group-out.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
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
186
187
188
189
module.exports = function(RED) {
function WhatsappGroupOut(config) {
RED.nodes.createNode(this,config);
var node = this;
node.number = config.gID;
var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink);
node.waClient = whatsappLinkNode.client;
const { MessageMedia, Buttons } = require('whatsapp-web.js')
let SetStatus = function(WAStatus, color){
node.status({fill:color,shape:"dot",text:WAStatus});
};
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
async function webNubmerSeteing(numb){
numb = typeof numb ==='number' ? numb : numb.replace(/\D/g, '');
// numb = `${numb}@c.us`;
var numbID = await node.waClient.getNumberId(numb);
if(numbID) {
return `${numbID.user}@${numbID.server}`;
} else {
return `${numb}@g.us`
}
// return numb
}
async function socNubmerSeteing(numb){
if (numb.remoteJid){
return numb.remoteJid;
}
numb = typeof numb ==='number' ? numb : numb.replace(/\D/g, '');
const [result] = await (await node.waClient).onWhatsApp(numb)
if (result?.exists){
console.log(result.exists)
return result.jid
}
return numb = `${numb}@g.us`;
}
async function whatsappMessage(numb , inputMessage){
if (node.waClient.clientType === "waWebClient"){
try {
numb = await webNubmerSeteing(numb);
if(typeof inputMessage === "object"){
inputMessage = new Buttons(inputMessage.text, inputMessage.buttons, "text" ,inputMessage.footer);
node.waClient.sendMessage(numb, inputMessage);
}
node.waClient.sendMessage(numb, inputMessage);
}
catch(e){
node.error(`Error Sending Msg: ${e}`);
}
}
else if (node.waClient.clientType === "waSocketClient"){
try {
let client = await node.waClient;
numb = await socNubmerSeteing(numb)
if (typeof inputMessage ==="string"){
inputMessage = {text : inputMessage};
}
const msgStatus = await client.sendMessage(numb, inputMessage);
}
catch(e) {
node.error(`Error Sending Msg:: ${e}`);
}
}
else {
node.error(`Error Sending Msg: ${e}`)
}
SetStatus("Message Send.", "green");
setTimeout(()=>{
SetStatus('Connected','green');
}, 2000)
};
async function whatsappMultiMediaMessage(numb, whatsappImage, whatsappCaption){
var whatsappImageBase64 = whatsappImage.split(',')[1] || whatsappImage;
try {
if (node.waClient.clientType === "waWebClient"){
numb = await webNubmerSeteing(node.number)
var myMessage = new MessageMedia('image/png', whatsappImageBase64, null, null);
node.waClient.sendMessage(numb, myMessage, {caption : whatsappCaption || "Image from Node-Red"});
}
else {
numb = await socNubmerSeteing(node.number)
const imageMessage = {
text: whatsappCaption,
footer: null,
templateButtons: null,
image: {url : whatsappImage }
};
let client = await node.waClient;
const msgStatus = await client.sendMessage(numb, imageMessage);
}
SetStatus("Message Send.", "green");
setTimeout(()=>{
SetStatus('Connected','green');
}, 2000)
} catch(e) {
node.error(`Error sending MultiMedia Message : ${e}`)
}
};
node.on('input', (message)=> {
if (node.number){
if (message.image){
whatsappMultiMediaMessage(node.number, message.image, message.payload);
}
else {
whatsappMessage(node.number, message.payload);
}
} else if (message.toNumber){
var numbers = message.toNumber instanceof Array ? message.toNumber : Array.of(message.toNumber);
for (number of numbers) {
if(message.image){
whatsappMultiMediaMessage(number, message.image, message.payload)
delay(2000);
} else {
whatsappMessage(number, message.payload)
delay(2000)
}
}
} else if(message.key.remoteJid){
whatsappMessage(message.key, message.payload)
}
else {
SetStatus("No number","red");
setTimeout(()=>{
SetStatus('Connected','green');
}, 5000)
}
});
//whatsapp Status Parameters----
if (node.waClient.clientType === "waWebClient"){
node.waClient.on('qr', (qr) => {
SetStatus("QR Code Generated", "yellow");
});
node.waClient.on('auth_failure', () => {
SetStatus('Not Connected','red');
});
node.waClient.on('loading_screen', () => {
SetStatus('Connecting...','yellow');
});
node.waClient.on('ready', () => {
SetStatus('Connected','green');
});
node.waClient.on('disconnected', () => {
SetStatus("Disconnected","red");
});
} else if (node.waClient.clientType === "waSocketClient"){
async function checkStatusOfSockets(){
let client = await node.waClient;
client.ev.on('connection.update', (updates)=>{
var {connection} = updates
if(connection === 'open'){
SetStatus("Connected", "green");
}
else if(updates.isOnline){
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");
}
})
}
checkStatusOfSockets();
}
}
RED.nodes.registerType("group-out", WhatsappGroupOut);
}