From a14e82514c54668506496800594710218cdf7385 Mon Sep 17 00:00:00 2001 From: rawee Date: Tue, 6 Dec 2022 02:04:24 +0530 Subject: [PATCH] Readme updated and bug removed --- README.md | 24 ++++++++++++- package.json | 2 +- whatsapp-in.js | 50 +++++++++++++++------------ whatsapp-out.html | 3 +- whatsappAdmin.html | 4 ++- whatsappAdmin.js | 45 ++++++++++++------------- whatsappLink.html | 9 ++--- whatsappLink.js | 84 ++++++++++++++++++++++++++++++++++------------ 8 files changed, 146 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index c3a5253..f813e27 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,25 @@ # Whatsapp Bot -Simple Node for connecting Node-Red to Whatsapp. \ No newline at end of file +Simple Node for connecting Node-Red to Whatsapp. + +Currently in developing mode, Continous updated may encounter. + +To Connect with whatsapp- +1. Deploy the any whatsapp node with whatsappLink node. +2. look in Console/Bash/terminal. +3. whatsappLink node will initilize, connect with whatsapp and generate a QR code it terminal. +4. Scan the QR code with your Whatsapp Mobile App (Go to settings > Linked device). +5. Done - Whatsapp Connected. + + It will create a Whatsapp Web instance in your machine and store your session locally in Node-RED. + + +Currently working on more Whatsapp Node and will be avilable soon - + +1. Group Message Node. +2. Chat Reply node. +3. Instruction Reply Node. + +Complete detail for Nodes will also be updated as soon as possible. + +Thanks to bear with me :p diff --git a/package.json b/package.json index 00162f4..d3be269 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-whatsapp-link", - "version": "0.1.1", + "version": "0.1.12", "description": "Whatsapp connection with Node-Red", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" diff --git a/whatsapp-in.js b/whatsapp-in.js index b7de356..137039f 100644 --- a/whatsapp-in.js +++ b/whatsapp-in.js @@ -6,44 +6,50 @@ module.exports = function(RED) { var node = this; var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink); node.waClient = whatsappLinkNode.client; - var whatsappConnectionStauts = null; - async function whatsappConnection(){ - whatsappConnectionStauts = await node.waClient.getState(); - }; function SetStatus(WAStatus, color){ - node.status({fill:color,shape:"dot",text:WAStatus}); - msg = {payload : WAStatus}; - node.send(msg); + node.status({fill:color,shape:"dot",text:WAStatus}); }; - setInterval(function(){ - whatsappConnection(); - if (whatsappConnectionStauts==="CONNECTED"){ - node.status({fill:"green",shape:"dot",text:"Connected"}); - } else { - node.status({fill:"red",shape:"dot",text:"Not Connected"}); - } - },5000) - - node.waClient.on('message', async msg => { msg.payload = msg.body; node.send(msg); // Whatsapp Chats testing text. - if(msg.body === '!ping') { + if(msg.body === '!nodered') { var fromContact = msg.from.match(/\d+/); - msg.reply('pong from Node-Red'); - msg.payload = `!ping recieved from ${fromContact}.` + msg.reply('Hi from Node-Red'); + msg.payload = `!nodered recieved from ${fromContact}.` node.send(msg); } }); - node.on(`close`, ()=>{ - SetStatus("Disconnected", "red"); + //whatsapp Status Parameters---- + node.waClient.on('qr', (qr) => { + SetStatus("QR Code Generated", "yellow"); + QRCode.toDataURL(qr, function(err, url){ + msg = {payload : url}; + node.send(msg); + }); }); + + 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"); + }); + } RED.nodes.registerType("whatsapp-in", WhatsappIn); } diff --git a/whatsapp-out.html b/whatsapp-out.html index 4cd4d15..1d3b0d9 100644 --- a/whatsapp-out.html +++ b/whatsapp-out.html @@ -34,5 +34,6 @@ diff --git a/whatsappAdmin.html b/whatsappAdmin.html index 852021a..d346539 100644 --- a/whatsappAdmin.html +++ b/whatsappAdmin.html @@ -28,5 +28,7 @@ diff --git a/whatsappAdmin.js b/whatsappAdmin.js index 5779986..75c81e0 100644 --- a/whatsappAdmin.js +++ b/whatsappAdmin.js @@ -17,21 +17,18 @@ module.exports = function(RED) { // Commands recived for Whatsapp Client. - this.on('input', function(msg, send){ + this.on('input', async function(msg, send){ if (msg.payload === "destroy") { - node.waClient.destroy(); + await node.waClient.destroy(); SetStatus("Disconnected","red"); } else if (msg.payload==="logout") { - node.waClient.logout(); - SetStatus("Logged Out..","red"); + await node.waClient.logout(); + SetStatus("Logged Out","red"); } - else if (msg.payload === "test"){ - async function test() { - msg.payload = await node.waClient.getState(); - node.send(msg); - } - test(); + else if (msg.payload === "state"){ + msg.payload = await node.waClient.getState(); + node.send(msg); } else if (msg.payload === "restart"){ node.WARestart(); @@ -39,33 +36,33 @@ module.exports = function(RED) { }; }); - this.on(`close`, ()=>{ - SetStatus("Disconnected", "red"); - }); - - //QR Code generation. + //whatsapp Status Parameters---- node.waClient.on('qr', (qr) => { - QRCode.toString(qr, {type : 'terminal', small:true }, function(err, QRTerminal){ - console.log("To Connect, Scan the QR Code through your Whatsapp Mobile App.") - console.log(QRTerminal); - }); SetStatus("QR Code Generated", "yellow"); QRCode.toDataURL(qr, function(err, url){ - SetStatus("Scan QR in Terminal", "green"); msg = {payload : url}; node.send(msg); }); }); - SetStatus("Connecting...", "yellow"); + node.waClient.on('auth_failure', () => { + SetStatus('Not Connected','red'); + }); + + node.waClient.on('loading_screen', () => { + SetStatus('Connecting...','yellow'); + }); + node.waClient.on('ready', () => { - console.log('Whatsapp Client is ready!'); SetStatus('Connected','green'); }); node.waClient.on('disconnected', () => { - console.log('WA Client is Disconnected!'); - SetStatus("Disconeccted","red"); + SetStatus("Disconnected","red"); + }); + + this.on(`close`, ()=>{ + SetStatus("Disconnected", "red"); }); } RED.nodes.registerType("whatsapp-admin", WhatsappAdmin); diff --git a/whatsappLink.html b/whatsappLink.html index 4cbd646..671b5db 100644 --- a/whatsappLink.html +++ b/whatsappLink.html @@ -2,7 +2,8 @@ RED.nodes.registerType('whatsappLink',{ category: 'config', defaults: { - cName : {value:"whatsapp-web",required:true} + cName : {value:"whatsapp-web",required:true}, + name : {value : "Web"} }, label: function() { return this.cName ; @@ -12,10 +13,10 @@ \ No newline at end of file diff --git a/whatsappLink.js b/whatsappLink.js index 5508c75..f1bda91 100644 --- a/whatsappLink.js +++ b/whatsappLink.js @@ -1,44 +1,86 @@ module.exports = function(RED) { const { Client, LocalAuth } = require('whatsapp-web.js'); + const QRCode = require('qrcode'); function RemoteClientNode(n) { RED.nodes.createNode(this,n); let WAnode = this; - WAnode.client = n.cName; - let WAClientID = `${n.cName}ID`; - - WAnode.client = new Client({ - authStrategy : new LocalAuth({ - clientId : WAClientID - }), + let whatsappConnectionStatus; + + const client = new Client({ + authStrategy : new LocalAuth(), puppeteer : {headless : true } }); - + // WAnode.log(`Client is generated`); + let WAConnect = function(){ - WAnode.client.initialize(); - WAnode.log("Connecting to Whatsapp.."); + try { + client.initialize(); + WAnode.log("Status : Initializing Whatsapp.."); + } + catch(e) { + WAnode.log(`Error : ${e}`); + }; + + client.on("qr", (qr)=>{ + clearInterval(connectionSetupID); + QRCode.toString(qr, {type : 'terminal', small:true }, function(err, QRTerminal){ + WAnode.log(`To Connect, Scan the QR Code through your Whatsapp Mobile App.`) + console.log(""); + console.log(QRTerminal); + }); + }); + client.on("ready", ()=>{ + WAnode.log(`Status : Whatsapp Connected`); + }); }; WAConnect(); - let WARestart = function(){ - WAnode.client.destroy(); - WAnode.client.initialize(); + async function WAClose(){ + await client.destroy(); + }; + + async function connectionSetup(){ + try { + whatsappConnectionStatus = await client.getState(); + if(whatsappConnectionStatus === "CONNECTED"){ + clearInterval(connectionSetupID); + } + else { + WAnode.log(`Status : Connecting to Whatsapp...`); + } + } + catch(e){ + WAnode.log(`Error : ${e}`); + WARestart(); + } + }; + let connectionSetupID = setInterval(connectionSetup, 10000); + + let WARestart = async function(){ + await client.destroy(); + await client.initialize(); } - + this.on('close', (removed, done)=>{ - if(!removed){ - console.log(`closing WA admin closing in new line also`) - async function distroyWA() { - await WAnode.client.destroy(); - }; - distroyWA(); + if(removed){ + clearInterval(connectionSetupID); + WAClose(); + } + else { + clearInterval(connectionSetupID); + WAClose(); } done(); + }); - //this.client = client ; + this.WAConnect = WAConnect; + this.client = client; + this.WARestart = WARestart; + this.whatsappConnectionStatus = whatsappConnectionStatus; } RED.nodes.registerType("whatsappLink",RemoteClientNode); } \ No newline at end of file