forked from raweee/node-red-contrib-whatsapp-link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchats-in.js
116 lines (104 loc) · 4.61 KB
/
chats-in.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
module.exports = function(RED) {
function WhatsappIn(config) {
RED.nodes.createNode(this,config);
var node = this;
var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink);
node.waClient = whatsappLinkNode.client;
var whatsappLiteEvents = config.whatsappLiteevent;
var whatsappWebEvents = config.whatsappWebevent;
function SetStatus(WAStatus, color){
node.status({fill:color,shape:"dot",text:WAStatus});
};
if(node.waClient.clientType === "waWebClient"){
whatsappWebEvents = whatsappWebEvents?.split(",") || whatsappWebEvents ;
whatsappWebEvents?.forEach((waEvent)=>{
if (waEvent ==='message'){
node.waClient.on(waEvent, async message => {
let msg = {};
msg.event = waEvent;
msg.payload = message.body|| null;
msg.from = message.author || message.from ;
msg.chatID = message.from.replace(/\D/g, '');
msg.from = msg.from.replace(/\D/g, '');
msg.message = message ;
node.send(msg);
});
} else {
node.waClient.on(waEvent, async message => {
msg = {};
msg.event = waEvent;
msg.message = message
node.send(msg);
});
};
})
//whatsapp Status Parameters----
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"){
var client = null
async function clientFromWhatsappLite(){
client = await node.waClient;
whatsappLiteEvents = whatsappLiteEvents?.split(",") || whatsappLiteEvents;
whatsappLiteEvents?.forEach((waEvent)=>{
if (waEvent ==='messages.upsert'){
client.ev.on('messages.upsert', msgs =>{
msgs.messages.forEach(async msg =>{
msg.event = waEvent;
msg.payload = msg.message?.conversation;
msg.from = msg.key.participant || msg.key.remoteJid;
msg.from = msg.from.replace(/\D/g, '') || msg.from;
msg.chatID = msg.key.remoteJid.replace(/\D/g, '') || msg.key.remoteJid ;
if(msg.message.extendedTextMessage){
return null
}
node.send(msg)
})
});
}
else {
client.ev.on(waEvent, msgs =>{
msgs.event = waEvent;
node.send(msgs)
});
};
})
//Setting conncetion status indication
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");
}
})
}
clientFromWhatsappLite();
};
}
RED.nodes.registerType("chats-in", WhatsappIn);
}