From 2823eb349dd84a5957063ad5f9eff08bef8896de Mon Sep 17 00:00:00 2001 From: rawee Date: Sun, 12 Feb 2023 01:23:18 +0530 Subject: [PATCH] Buttons Added --- README.md | 188 ++++++++++++++++++++++++++- admin.html | 10 +- admin.js | 99 ++++++++++---- chats-in.js | 4 +- chats-out.js | 72 ++++++++--- group-out.js | 125 ++++++++---------- package-lock.json | 120 ++++++++++------- package.json | 6 +- whatsappLink.js | 319 ++++++++++++++++++++++++---------------------- 9 files changed, 610 insertions(+), 333 deletions(-) diff --git a/README.md b/README.md index 9d27dd7..61361cb 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Admin Node generate QR Code just below the node for easy connection with whatsap 3. **Chats Out** : As simple as mention on name, node will send `msg.payload` recived at input to the number mentioned in node. - MultiMedia Message Out: Requirments- + MultiMedia Message: Requirments- | Input | Description | |--------|-------------| | `msg.image` | Base64 (encoded image) | @@ -82,9 +82,192 @@ Admin Node generate QR Code just below the node for easy connection with whatsap The node will send recived `msg.payload` to a group chat. +## Button, List and TemplateButton +Supported in Whatsapp-Lite only, `TODO for Whatsapp-Web`. + +* Simple Button For simple 3 Bottons your `msg.paylod` should be... + +```js +msg.payload = { + text: "Hi it's button message", //String + footer: 'Hello World', //String + headerType: 1, //keep it "1" only. + buttons: [ // Array of buttons. + {buttonId: 'id1', buttonText: {displayText: 'Button 1'}, type: 1}, + {buttonId: 'id2', buttonText: {displayText: 'Button 2'}, type: 1}, + {buttonId: 'id3', buttonText: {displayText: 'Button 3'}, type: 1} + ] +} +``` +* TemplateButton combination of link button, Call button and Normal buttons. Your `msg.paylod` should look similar to-- + +```js +msg.payload = { + text: "Hi it's a template message by Node-RED 👍 to Test", + footer: 'Hello I am footer of message.', + templateButtons: [ + {index: 1, urlButton: {displayText: '⭐ Vist Node-RED', url: 'https://nodered.org/'}}, + {index: 2, callButton: {displayText: 'Call me!', phoneNumber: '+1 (234) 5678-901'}}, + {index: 3, quickReplyButton: {displayText: 'Click me I am Button', id: 'I-am-button-id-without-space'}}, + {index: 4, quickReplyButton: {displayText: '🖱️ Sample Button 2', id: 'button-2-was-clicked'}} + ] +} +``` +* List Message combination of link button, List button and Selectors. Your `msg.paylod` should look similar to-- + +```js +msg.payload = { + text: "This is a list", + footer: "nice footer, link: https://google.com", + title: "Amazing boldfaced list title", + buttonText: "Required, Tap to see List", + sections : [{ + title: "Section 1", + rows: [ + {title: "Option 1", rowId: "option1"}, + {title: "Option 2", rowId: "option2", description: "This is a description"} + ]}, + { + title: "Section 2", + rows: [ + {title: "Option 3", rowId: "option3"}, + {title: "Option 4", rowId: "option4", description: "This is a description V2"} + ] + }] +} + +``` +Yes its lot require for buttons, A node will come soon to minimize these effors. +You may direct import these test button with bellow code. + +```json +[ + { + "id": "6fe81f4418014185", + "type": "inject", + "z": "a133618d7af8d486", + "name": "Sample button Text", + "props": [ + { + "p": "payload" + } + ], + "repeat": "", + "crontab": "", + "once": false, + "onceDelay": 0.1, + "topic": "", + "payload": "", + "payloadType": "date", + "x": 130, + "y": 180, + "wires": [ + [ + "ad52615eb46ed55b" + ] + ] + }, + { + "id": "cf275a62edbc347f", + "type": "inject", + "z": "a133618d7af8d486", + "name": "Smart Button Test", + "props": [ + { + "p": "topic", + "v": "", + "vt": "date" + } + ], + "repeat": "", + "crontab": "", + "once": false, + "onceDelay": 0.1, + "topic": "", + "x": 130, + "y": 140, + "wires": [ + [ + "f5fbed87687de5b7" + ] + ] + }, + { + "id": "f5fbed87687de5b7", + "type": "function", + "z": "a133618d7af8d486", + "name": "TempButton", + "func": "msg.payload = {\n text: \"Hi it's a template message by Node-RED 👍 to Test\",\n footer: 'Hello I am footer of message.',\n templateButtons: [\n {\n index: 1,\n urlButton: {\n displayText: '⭐ Vist Node-RED',\n url: 'https://nodered.org/'\n }\n },\n {\n index: 2,\n callButton: {\n displayText: 'Call me!',\n phoneNumber: '+1 (234) 5678-901'\n }\n },\n {\n index: 3,\n quickReplyButton: {\n displayText: 'Click me I am Button',\n id: 'id-like-buttons-message'\n }\n },\n {\n index: 4,\n quickReplyButton: {\n displayText: '🖱️ Sample Button',\n id: 'button-id-no-space'\n }\n }] \n}\nreturn msg;", + "outputs": 1, + "noerr": 0, + "initialize": "", + "finalize": "", + "libs": [], + "x": 370, + "y": 140, + "wires": [ + [ + "9caf4dd912ca82f6" + ] + ] + }, + { + "id": "ad52615eb46ed55b", + "type": "function", + "z": "a133618d7af8d486", + "name": "Button", + "func": "msg.payload = {\n text: \"Hi it's button message\",\n footer: 'Hello World',\n headerType: 1,\n buttons: [\n {buttonId: 'id1', buttonText: {displayText: 'Button 1'}, type: 1},\n {buttonId: 'id2', buttonText: {displayText: 'Button 2'}, type: 1},\n {buttonId: 'id3', buttonText: {displayText: 'Button 3'}, type: 1}\n ]\n }\nreturn msg;", + "outputs": 1, + "noerr": 0, + "initialize": "", + "finalize": "", + "libs": [], + "x": 350, + "y": 180, + "wires": [ + [ + "9caf4dd912ca82f6" + ] + ] + }, + { + "id": "670571eb76e4d237", + "type": "comment", + "z": "a133618d7af8d486", + "name": "Connect them to Chats-Out Node. ", + "info": "Dont foget mobile no.", + "x": 160, + "y": 100, + "wires": [] + }, + { + "id": "9caf4dd912ca82f6", + "type": "chats-out", + "z": "a133618d7af8d486", + "name": "Chats Out", + "whatsappLink": "whatsapp-web", + "number": "", + "x": 620, + "y": 160, + "wires": [] + }, + { + "id": "3e0550933ed06e84", + "type": "comment", + "z": "a133618d7af8d486", + "name": "Mention Your Phone No. in node.", + "info": "", + "x": 610, + "y": 120, + "wires": [] + } +] +``` + + 5. **Reply Node** : In Beta mode. -Node will reply(the `payload`) on each message starting with string mentioned in instruction coloum or defaults `!red`. +Node will reply(the `payload`) on each message starting with string mentioned in instruction coloum or defaults `!red`. Avoid using it please. ## Issues & Updates @@ -96,6 +279,7 @@ Issues and Suggestions are welcome [here.](https://github.com/raweee/node-red-co * `Ver-0.1.28` : Now QR Codes are directlly avilable in run time on Whatsapp-Admin-Node. * `Ver-0.1.30` : Message can be send to an Array of contacts provided at `msg.toNumber`. * `Ver-0.1.32` : Socket based `Whatsapp Lite` config node added in beta mode. Image message sending support added in chats-out node. +* `Ver-0.1.33` : Button and list support added, Minnor bugs fixed. ## Future Nodes Currently working on more Whatsapp Node and will be avilable soon - diff --git a/admin.html b/admin.html index 25257b5..4047986 100644 --- a/admin.html +++ b/admin.html @@ -24,15 +24,10 @@ } }; - var adminNodeId = null; - RED.comms.subscribe("whatsappLinkNodeID", function (e, currentID) { - adminNodeId = currentID; - }) - var creatImageContainer = function(NodeID) { let img = document.getElementById("whatsappLink-QRcode-" + NodeID) if (!img) { - const container = document.getElementById(adminNodeId) + const container = document.getElementById(NodeID) if (!container) { return } const img = document.createElementNS("http://www.w3.org/2000/svg", 'image') img.setAttribute('id', "whatsappLink-QRcode-" + NodeID) @@ -50,9 +45,10 @@ qrImage.addEventListener("click", ()=> removeQrCode(id), {once:true}) }; + RED.comms.subscribe("whatsappLinkQrCode", function (e, msg) { if (msg.image === null){ - removeQrCode(msg.id); + removeQrCode(adminNodeId); } renderQrCode(msg.id, msg.image); }); diff --git a/admin.js b/admin.js index 8d0083e..5fb8d20 100644 --- a/admin.js +++ b/admin.js @@ -6,7 +6,7 @@ module.exports = function(RED) { var node = this; var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink); node.client = whatsappLinkNode.client; - RED.comms.publish("whatsappLinkNodeID", node.id) + // RED.comms.publish("whatsappLinkNodeID", node.id) function SetStatus(WAStatus, color){ node.status({fill:color,shape:"dot",text:WAStatus}); @@ -15,33 +15,53 @@ module.exports = function(RED) { }; - // 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 if (msg.payload==="logout") { - if(node.client.clientType === "waWebClient"){ - node.client.logout(); - SetStatus("Logged Out","red"); - } + // 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 === "test"){ - if(node.client.clientType === "waWebClient"){ - msg.payload = await node.client.getState(); - node.send(msg); - } - } - else if (msg.payload === "restart"){ - if(node.client.clientType === "waWebClient"){ - node.client.WARestart(); - SetStatus("Connecting...", "yellow"); - } - }; - }); + } + 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"){ @@ -110,6 +130,29 @@ module.exports = function(RED) { async function clientFromWhatsappLite(){ client = await node.client; client.ev.on('connection.update', (updates)=>{ + + if(updates.qr){ + QRCode.toDataURL(updates.qr, function(err, url){ + var qrImageWithID = {}; + qrImageWithID.id = node.id; + qrImageWithID.image = url; + RED.comms.publish("whatsappLinkQrCode", qrImageWithID); + }); + + 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); + }); + } + if (connection === 'open') { + var qrImageWithID = {}; + qrImageWithID.id = node.id; + qrImageWithID.image = null; + RED.comms.publish("whatsappLinkQrCode", qrImageWithID); + } + + //console.log(updates); var {connection} = updates //Setting conncetion status indication if(connection === 'open'){ @@ -124,7 +167,7 @@ module.exports = function(RED) { else if(connection === 'connecting'){ SetStatus("Connecting...", "yellow"); } - else if(updates.is){ + else if(updates.qr){ SetStatus("Scan QR Code to Connect.", "yellow"); } }) diff --git a/chats-in.js b/chats-in.js index ac1fe05..75067d8 100644 --- a/chats-in.js +++ b/chats-in.js @@ -49,7 +49,7 @@ module.exports = function(RED) { client.ev.on('messages.upsert', msgs =>{ msgs.messages.forEach(async msg =>{ - msg.payload = msg.message.conversation; + 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 ; @@ -73,7 +73,7 @@ module.exports = function(RED) { else if(connection === 'connecting'){ SetStatus("Connecting...", "yellow"); } - else if(updates.is){ + else if(updates.qr){ SetStatus("Scan QR Code to Connect.", "yellow"); } }) diff --git a/chats-out.js b/chats-out.js index 40d29e5..2bc4c48 100644 --- a/chats-out.js +++ b/chats-out.js @@ -5,36 +5,57 @@ module.exports = function(RED) { node.number = config.number; var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink); node.waClient = whatsappLinkNode.client; - const { MessageMedia } = require('whatsapp-web.js') + 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 whatsappMessage(numb , inputMessage){ + function webNubmerSeteing(numb){ numb = typeof numb ==='number' ? numb : numb.replace(/\D/g, ''); + numb = `${numb}@c.us`; + return numb + } + + function socNubmerSeteing(numb){ + numb = typeof numb ==='number' ? numb : numb.replace(/\D/g, ''); + numb = `${numb}@s.whatsapp.net` + return numb + } + + async function whatsappMessage(numb , inputMessage){ if (node.waClient.clientType === "waWebClient"){ try { - numb = `${numb}@c.us`; + numb = webNubmerSeteing(numb); + if(typeof inputMessage === "object"){ + // inputMessage = new Buttons(inputMessage.text, inputMessage.buttons, "text" ,inputMessage.footer) + let myBtn = new Buttons('Button body',[{body:'bt1'},{body:'bt2'},{body:'bt3'}],'title','footer'); + console.log(myBtn) + node.waClient.sendMessage(numb, inputMessage); + inputMessage = myBtn + } node.waClient.sendMessage(numb, inputMessage); } - catch(e) { - node.log(`Error Sending Msg: ${e}`); + catch(e){ + node.error(`Error Sending Msg: ${e}`); } } else if (node.waClient.clientType === "waSocketClient"){ try { let client = await node.waClient; - numb = `${numb}@s.whatsapp.net`; - const msgStatus = await client.sendMessage(numb, {text : inputMessage}); + numb = socNubmerSeteing(numb) + if (typeof inputMessage ==="string"){ + inputMessage = {text : inputMessage}; + } + const msgStatus = await client.sendMessage(numb, inputMessage); } catch(e) { - node.log(`Error Sending Msg: ${e}`); + node.error(`Error Sending Msg: ${e}`); } } else { - node.log(`Error Sending Msg: ${e}`) + node.error(`Error Sending Msg: ${e}`) } SetStatus("Message Send.", "green"); setTimeout(()=>{ @@ -42,20 +63,31 @@ module.exports = function(RED) { }, 2000) }; - function whatsappMultiMediaMessage(numb, whatsappImage, whatsappCaption){ + async function whatsappMultiMediaMessage(numb, whatsappImage, whatsappCaption){ + var whatsappImageBase64 = whatsappImage.split(',')[1] || whatsappImage; try { - numb = node.number; - whatsappImage = whatsappImage.split(',')[1] || whatsappImage; - var myMessage = new MessageMedia('image/png', whatsappImage, null, null); - numb = typeof numb ==='number' ? numb : numb.replace(/\D/g, ''); - numb = `${numb}@c.us`; - node.waClient.sendMessage(numb, myMessage, {caption : whatsappCaption || "Image from Node-Red"}); + if (node.waClient.clientType === "waWebClient"){ + numb = 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 = 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.log(`Error sending MultiMedia Message : ${e}`) + node.error(`Error sending MultiMedia Message : ${e}`) } }; @@ -63,8 +95,8 @@ module.exports = function(RED) { if (node.number){ if (message.image){ whatsappMultiMediaMessage(node.number, message.image, message.payload); - } - else { + } + else { whatsappMessage(node.number, message.payload); } @@ -127,7 +159,7 @@ module.exports = function(RED) { else if(connection === 'connecting'){ SetStatus("Connecting...", "yellow"); } - else if(updates.is){ + else if(updates.qr){ SetStatus("Scan QR Code to Connect.", "yellow"); } }) diff --git a/group-out.js b/group-out.js index 54b4a60..5a7b5c0 100644 --- a/group-out.js +++ b/group-out.js @@ -5,36 +5,58 @@ module.exports = function(RED) { node.number = config.gID; var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink); node.waClient = whatsappLinkNode.client; - const { MessageMedia } = require('whatsapp-web.js') + + 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 whatsappMessage(numb , inputMessage){ + function webNubmerSeteing(numb){ numb = typeof numb ==='number' ? numb : numb.replace(/\D/g, ''); + numb = `${numb}@g.us`; + return numb + } + + function socNubmerSeteing(numb){ + numb = typeof numb ==='number' ? numb : numb.replace(/\D/g, ''); + numb = `${numb}@g.us` + return numb + } + + async function whatsappMessage(numb , inputMessage){ if (node.waClient.clientType === "waWebClient"){ try { - numb = `${numb}@g.us`; + numb = webNubmerSeteing(numb); + if(typeof inputMessage === "object"){ + // inputMessage = new Buttons(inputMessage.text, inputMessage.buttons, "text" ,inputMessage.footer) + let myBtn = new Buttons('Button body',[{body:'bt1'},{body:'bt2'},{body:'bt3'}],'title','footer'); + console.log(myBtn) + node.waClient.sendMessage(numb, inputMessage); + inputMessage = myBtn + } node.waClient.sendMessage(numb, inputMessage); } - catch(e) { - node.log(`Error Sending Msg: ${e}`); + catch(e){ + node.error(`Error Sending Msg: ${e}`); } } else if (node.waClient.clientType === "waSocketClient"){ try { let client = await node.waClient; - numb = `${numb}@g.us`; - const msgStatus = await client.sendMessage(numb, {text : inputMessage}); + numb = socNubmerSeteing(numb) + if (typeof inputMessage ==="string"){ + inputMessage = {text : inputMessage}; + } + const msgStatus = await client.sendMessage(numb, inputMessage); } catch(e) { - node.log(`Error Sending Msg: ${e}`); + node.error(`Error Sending Msg: ${e}`); } } else { - node.log(`Error Sending Msg: ${e}`) + node.error(`Error Sending Msg: ${e}`) } SetStatus("Message Send.", "green"); setTimeout(()=>{ @@ -42,20 +64,31 @@ module.exports = function(RED) { }, 2000) }; - function whatsappMultiMediaMessage(numb, whatsappImage, whatsappCaption){ + async function whatsappMultiMediaMessage(numb, whatsappImage, whatsappCaption){ + var whatsappImageBase64 = whatsappImage.split(',')[1] || whatsappImage; try { - numb = node.number; - whatsappImage = whatsappImage.split(',')[1] || whatsappImage; - var myMessage = new MessageMedia('image/png', whatsappImage, null, null); - numb = typeof numb ==='number' ? numb : numb.replace(/\D/g, ''); - numb = `${numb}@g.us`; - node.waClient.sendMessage(numb, myMessage, {caption : whatsappCaption || "Image from Node-Red"}); + if (node.waClient.clientType === "waWebClient"){ + numb = 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 = 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.log(`Error sending MultiMedia Message : ${e}`) + node.error(`Error sending MultiMedia Message : ${e}`) } }; @@ -63,8 +96,8 @@ module.exports = function(RED) { if (node.number){ if (message.image){ whatsappMultiMediaMessage(node.number, message.image, message.payload); - } - else { + } + else { whatsappMessage(node.number, message.payload); } @@ -88,6 +121,8 @@ module.exports = function(RED) { } }); + + //whatsapp Status Parameters---- if (node.waClient.clientType === "waWebClient"){ node.waClient.on('qr', (qr) => { @@ -127,7 +162,7 @@ module.exports = function(RED) { else if(connection === 'connecting'){ SetStatus("Connecting...", "yellow"); } - else if(updates.is){ + else if(updates.qr){ SetStatus("Scan QR Code to Connect.", "yellow"); } }) @@ -136,56 +171,6 @@ module.exports = function(RED) { } - - - - - - - // node.on('input', (message)=> { - // if(node.gID){ - // try { - // node.gID = node.gID.replace(/\D/g, ''); - // node.gID = `${node.gID}@g.us`; - // node.waClient.sendMessage(node.gID, message.payload); - // SetStatus("Message Send.", "green"); - // setTimeout(()=>{ - // SetStatus('Connected','green'); - // }, 3000) - // } - // catch(e) { - // node.log(`Error Sending Msg: ${e}`); - // }; - // } - // else { - // SetStatus("No Chat-ID","red"); - // setTimeout(()=>{ - // SetStatus('Connected','green'); - // }, 5000) - // }; - // }); - - //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"); - }); - } RED.nodes.registerType("group-out", WhatsappGroupOut); } diff --git a/package-lock.json b/package-lock.json index af1f3c0..1cd6ea8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "node-red-contrib-whatsapp-link", - "version": "0.1.31", + "version": "0.1.32-A", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "node-red-contrib-whatsapp-link", - "version": "0.1.31", + "version": "0.1.32-A", "license": "ISC", "dependencies": { - "@adiwajshing/baileys": "^5.0.0", + "@adiwajshing/baileys": "latest", "qrcode": "^1.5.1", "whatsapp-web.js": "latest" }, @@ -140,9 +140,9 @@ "integrity": "sha512-MqTGEo5bj5t157U6fA/BiDynNkn0YknVdh48CMPkTSpFTVmvao5UQmm7uEF6xBEo7qIMAlY/JSleYaE6VOdpaA==" }, "node_modules/@types/node": { - "version": "18.11.18", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz", - "integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==" + "version": "18.13.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.13.0.tgz", + "integrity": "sha512-gC3TazRzGoOnoKAhUx+Q0t8S9Tzs74z7m0ipwGpSqQrleP14hKxP4/JUeEQcD3W1/aIpnWl8pHowI7WokuZpXg==" }, "node_modules/@types/yauzl": { "version": "2.10.0", @@ -476,9 +476,9 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, "node_modules/content-type": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", - "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==", + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", "engines": { "node": ">= 0.6" } @@ -1093,9 +1093,9 @@ } }, "node_modules/minimist": { - "version": "1.2.7", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", - "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==", + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", "optional": true, "funding": { "url": "https://github.com/sponsors/ljharb" @@ -1156,9 +1156,9 @@ } }, "node_modules/node-fetch": { - "version": "2.6.8", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.8.tgz", - "integrity": "sha512-RZ6dBYuj8dRSfxpUSu+NsdF1dpPpluJxwOp+6IoDp/sH2QNDSvurYsAa+F1WxY2RjA1iP93xhcsUoYbF2XBqVg==", + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.9.tgz", + "integrity": "sha512-DJm/CJkZkRjKKj4Zi4BsKVZh3ValV5IR5s7LVZnW+6YMh0W1BfNA8XSs6DLMGYlId5F3KnA70uu2qepcR08Qqg==", "dependencies": { "whatwg-url": "^5.0.0" }, @@ -1403,6 +1403,26 @@ "node": ">=10.18.1" } }, + "node_modules/puppeteer/node_modules/ws": { + "version": "8.5.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.5.0.tgz", + "integrity": "sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/qrcode": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/qrcode/-/qrcode-1.5.1.tgz", @@ -1472,9 +1492,9 @@ } }, "node_modules/readdir-glob/node_modules/minimatch": { - "version": "5.1.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.4.tgz", - "integrity": "sha512-U0iNYXt9wALljzfnGkhFSy5sAC6/SCR3JrHrlsdJz4kF8MvhTRQNiC59iUi1iqsitV7abrNAJWElVL9pdnoUgw==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", "optional": true, "dependencies": { "brace-expansion": "^2.0.1" @@ -1771,9 +1791,9 @@ "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, "node_modules/whatsapp-web.js": { - "version": "1.19.2", - "resolved": "https://registry.npmjs.org/whatsapp-web.js/-/whatsapp-web.js-1.19.2.tgz", - "integrity": "sha512-ibEKy0KJsmH1rH9DKXfWk9vCnP/Dw/nZF9Ax3M7LCYtumSC+fiXMRF3BMVu7E75NRdv1pzjw4v05/Oq9aYY6aA==", + "version": "1.19.3", + "resolved": "https://registry.npmjs.org/whatsapp-web.js/-/whatsapp-web.js-1.19.3.tgz", + "integrity": "sha512-+qPLVpSjyGMHS68znIXHAX6XhaAEh3MYmsrzzT8d+XDGy/Bw2g4z1VqiLBJbceiGT4zvKwJD7XKKFokV+ozh+Q==", "dependencies": { "@pedroslopez/moduleraid": "^5.0.2", "fluent-ffmpeg": "^2.1.2", @@ -1836,15 +1856,15 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, "node_modules/ws": { - "version": "8.5.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.5.0.tgz", - "integrity": "sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==", + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.12.0.tgz", + "integrity": "sha512-kU62emKIdKVeEIOIKVegvqpXMSTAMLJozpHZaJNDYqBjzlSYXQGviYwN1osDLJ9av68qHd4a2oSjd7yD4pacig==", "engines": { "node": ">=10.0.0" }, "peerDependencies": { "bufferutil": "^4.0.1", - "utf-8-validate": "^5.0.2" + "utf-8-validate": ">=5.0.2" }, "peerDependenciesMeta": { "bufferutil": { @@ -2017,9 +2037,9 @@ "integrity": "sha512-MqTGEo5bj5t157U6fA/BiDynNkn0YknVdh48CMPkTSpFTVmvao5UQmm7uEF6xBEo7qIMAlY/JSleYaE6VOdpaA==" }, "@types/node": { - "version": "18.11.18", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz", - "integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==" + "version": "18.13.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.13.0.tgz", + "integrity": "sha512-gC3TazRzGoOnoKAhUx+Q0t8S9Tzs74z7m0ipwGpSqQrleP14hKxP4/JUeEQcD3W1/aIpnWl8pHowI7WokuZpXg==" }, "@types/yauzl": { "version": "2.10.0", @@ -2276,9 +2296,9 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, "content-type": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", - "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==" }, "core-util-is": { "version": "1.0.3", @@ -2759,9 +2779,9 @@ } }, "minimist": { - "version": "1.2.7", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", - "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==", + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", "optional": true }, "mkdirp": { @@ -2806,9 +2826,9 @@ } }, "node-fetch": { - "version": "2.6.8", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.8.tgz", - "integrity": "sha512-RZ6dBYuj8dRSfxpUSu+NsdF1dpPpluJxwOp+6IoDp/sH2QNDSvurYsAa+F1WxY2RjA1iP93xhcsUoYbF2XBqVg==", + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.9.tgz", + "integrity": "sha512-DJm/CJkZkRjKKj4Zi4BsKVZh3ValV5IR5s7LVZnW+6YMh0W1BfNA8XSs6DLMGYlId5F3KnA70uu2qepcR08Qqg==", "requires": { "whatwg-url": "^5.0.0" } @@ -2990,6 +3010,14 @@ "tar-fs": "2.1.1", "unbzip2-stream": "1.4.3", "ws": "8.5.0" + }, + "dependencies": { + "ws": { + "version": "8.5.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.5.0.tgz", + "integrity": "sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==", + "requires": {} + } } }, "qrcode": { @@ -3045,9 +3073,9 @@ } }, "minimatch": { - "version": "5.1.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.4.tgz", - "integrity": "sha512-U0iNYXt9wALljzfnGkhFSy5sAC6/SCR3JrHrlsdJz4kF8MvhTRQNiC59iUi1iqsitV7abrNAJWElVL9pdnoUgw==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", "optional": true, "requires": { "brace-expansion": "^2.0.1" @@ -3284,9 +3312,9 @@ "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, "whatsapp-web.js": { - "version": "1.19.2", - "resolved": "https://registry.npmjs.org/whatsapp-web.js/-/whatsapp-web.js-1.19.2.tgz", - "integrity": "sha512-ibEKy0KJsmH1rH9DKXfWk9vCnP/Dw/nZF9Ax3M7LCYtumSC+fiXMRF3BMVu7E75NRdv1pzjw4v05/Oq9aYY6aA==", + "version": "1.19.3", + "resolved": "https://registry.npmjs.org/whatsapp-web.js/-/whatsapp-web.js-1.19.3.tgz", + "integrity": "sha512-+qPLVpSjyGMHS68znIXHAX6XhaAEh3MYmsrzzT8d+XDGy/Bw2g4z1VqiLBJbceiGT4zvKwJD7XKKFokV+ozh+Q==", "requires": { "@pedroslopez/moduleraid": "^5.0.2", "archiver": "^5.3.1", @@ -3338,9 +3366,9 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, "ws": { - "version": "8.5.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.5.0.tgz", - "integrity": "sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==", + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.12.0.tgz", + "integrity": "sha512-kU62emKIdKVeEIOIKVegvqpXMSTAMLJozpHZaJNDYqBjzlSYXQGviYwN1osDLJ9av68qHd4a2oSjd7yD4pacig==", "requires": {} }, "y18n": { diff --git a/package.json b/package.json index 0e97f41..d619921 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-whatsapp-link", - "version": "0.1.32", + "version": "0.1.32-B", "description": "Node to send and receive whatsapp messages in groups and chats. | No third party APIs", "repository": { "type": "git", @@ -30,9 +30,9 @@ } }, "dependencies": { + "@adiwajshing/baileys": "latest", "qrcode": "^1.5.1", - "whatsapp-web.js": "latest", - "@adiwajshing/baileys": "^5.0.0" + "whatsapp-web.js": "latest" }, "engines": { "node": ">=8.0.0" diff --git a/whatsappLink.js b/whatsappLink.js index 668cc0b..da5a509 100644 --- a/whatsappLink.js +++ b/whatsappLink.js @@ -1,22 +1,23 @@ module.exports = function(RED) { - const { Client, LocalAuth } = require('whatsapp-web.js'); - const makeWASocket = require('@adiwajshing/baileys'); - const { useMultiFileAuthState } = makeWASocket const QRCode = require('qrcode'); const FS = require('node:fs') const OS = require('os'); const Path = require('path'); - - let userDir = OS.homedir(); - let whatsappLinkDir = Path.join(userDir, '.node-red', 'Whatsapp-Link'); - let whatsappLinkDirSocket = Path.join(whatsappLinkDir, 'WA-Sockets') - function RemoteClientNode(n) { - RED.nodes.createNode(this,n); - var WAnode = this; - var clientType = n.clientType; - var whatsappConnectionStatus; - var client + let userDir = OS.homedir(); + let whatsappLinkDir = Path.join(userDir, '.node-red', 'Whatsapp-Link'); + let whatsappLinkDirSocket = Path.join(whatsappLinkDir, 'WA-Sockets'); + // let whatsappLinkDirSocketLogs = Path.join(whatsappLinkDir, 'WA-Sockets-logs'); + function RemoteClientNode(n) { + RED.nodes.createNode(this,n); + var WAnode = this; + var clientType = n.clientType; + var whatsappConnectionStatus; + var client + + if (clientType ==="waWebClient"){ + const { Client, LocalAuth } = require('whatsapp-web.js'); + var WAConnect = function(){ const webClient = new Client({ authStrategy : new LocalAuth({ @@ -37,171 +38,179 @@ module.exports = function(RED) { }; return webClient ; }; + client = WAConnect(); + WAnode.connectionSetupID = setInterval(connectionSetup, 10000); - if (clientType ==="waWebClient"){ - client = WAConnect(); - WAnode.connectionSetupID = setInterval(connectionSetup, 10000); - - function WAClose(){ - try { - client.destroy(); - } - catch(e){ - WAnode.err(`Error : Too many instructions! Try again.`) - } - }; - - var WARestart = function(){ - WAClose(); - WAConnect(); + function WAClose(){ + try { + client.destroy(); } + catch(e){ + WAnode.err(`Error : Too many instructions! Try again.`) + } + }; + + var WARestart = function(){ + WAClose(); + WAConnect(); + } - async function connectionSetup(){ - try { - whatsappConnectionStatus = await client.getState(); - if(whatsappConnectionStatus === "CONNECTED"){ - clearInterval(WAnode.connectionSetupID); - } - else { - WAnode.log(`Status : Connecting to Whatsapp...`); - } + async function connectionSetup(){ + try { + whatsappConnectionStatus = await client.getState(); + if(whatsappConnectionStatus === "CONNECTED"){ + clearInterval(WAnode.connectionSetupID); } - catch(e){ - WAnode.log(`Error : Waiting for Initializion...`); + else { + WAnode.log(`Status : Connecting to Whatsapp...`); } - }; - - //QR-Code on Terminal and Ready Status. - client.on("qr", (qr)=>{ - clearInterval(WAnode.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`); - }); - - //Whatsapp-Link Test Features (For Status and Testing Only.) - client.on('message_create', async (msg)=> { - if (msg.body.startsWith('!nodered')){ - let chat = await msg.getChat(); - let contact = await msg.getContact(); - if (chat.isGroup){ - let msgReply = - `Hi From Node-Red. - ------------------ - Group Name : ${chat.name}, - Group Id : ${chat.id.user}, - Group Admin : ${chat.groupMetadata.owner.user}, - Participants : ${chat.groupMetadata.size}` - msg.reply(msgReply); - } - else { - let msgReply = `Hi @${contact.number} From Node-Red.` - chat.sendMessage(msgReply, { - mentions : [contact] - }); - } - - } - - }); - - client.WAConnect = WAConnect; - client.WARestart = WARestart; - client.WAClose = WAClose; - client.clientType = clientType; - WAnode.client = client; + } + catch(e){ + WAnode.log(`Error : Waiting for Initializion...`); + } }; - if (clientType === "waSocketClient"){ - - async function connectSocketClient() { - const { state, saveCreds } = await useMultiFileAuthState(whatsappLinkDirSocket); - const socketClient = makeWASocket.default({ - printQRInTerminal: false, - auth : state - }) + //QR-Code on Terminal and Ready Status. + client.on("qr", (qr)=>{ + clearInterval(WAnode.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`); + }); - socketClient.ev.on('creds.update', saveCreds) - - socketClient.ev.on('connection.update', (update) => { - const { connection, lastDisconnect } = update - if (connection === 'close') { - // reconnect if not logged out + //Whatsapp-Link Test Features (For Status and Testing Only.) + client.on('message_create', async (msg)=> { + msg.body = `${msg.body}`; + if (msg.body.startsWith('!nodered')){ + let chat = await msg.getChat(); + let contact = await msg.getContact(); + if (chat.isGroup){ + let msgReply = +`Hi From Node-Red. +------------------ +Group Name : ${chat.name}, +Group Id : ${chat.id.user}, +Group Admin : ${chat.groupMetadata.owner.user}, +Participants : ${chat.groupMetadata.size}` + msg.reply(msgReply); + } + else { + let msgReply = `Hi @${contact.number} From Node-Red.` + chat.sendMessage(msgReply, { + mentions : [contact] + }); + } + + } + + }); + + client.WAConnect = WAConnect; + client.WARestart = WARestart; + client.WAClose = WAClose; + client.clientType = clientType; + WAnode.client = client; + }; + + if (clientType === "waSocketClient"){ + const makeWASocket = require('@adiwajshing/baileys'); + const { useMultiFileAuthState } = makeWASocket; + const pino = require('pino'); + async function connectSocketClient() { + const { state, saveCreds } = await useMultiFileAuthState(whatsappLinkDirSocket); + // const loggerFile = pino.destination(whatsappLinkDirSocketLogs); + const socketClient = makeWASocket.default({ + printQRInTerminal: false, + logger:pino({level: "fatal"}), + auth : state, + browser: ["Node-RED", "Chrome", "4.0.0"], + markOnlineOnConnect: true, + patchMessageBeforeSending: (message) => { + const requiresPatch = !!( + message.buttonsMessage || message.templateMessage || message.listMessage + ); + if (requiresPatch) { + message = { + viewOnceMessage: { + message: { + messageContextInfo: { + deviceListMetadataVersion: 2, + deviceListMetadata: {}, + }, + ...message, + }, + }, + }; + } + return message; + }, + }) + + socketClient.ev.on('creds.update', saveCreds) + + socketClient.ev.on('connection.update', (update) => { + const { connection, lastDisconnect } = update + if (connection === 'close') { + // reconnect if not logged out + + if ( + lastDisconnect && + lastDisconnect.error && + lastDisconnect.error.output && + (lastDisconnect.error.output.statusCode === 410 || + lastDisconnect.error.output.statusCode === 428 || + lastDisconnect.error.output.statusCode === 515) + ) { + connectSocketClient() + } else { if ( lastDisconnect && lastDisconnect.error && lastDisconnect.error.output && - (lastDisconnect.error.output.statusCode === 410 || - lastDisconnect.error.output.statusCode === 428 || - lastDisconnect.error.output.statusCode === 515) + lastDisconnect.error.output.statusCode === 401 ) { connectSocketClient() + FS.rmSync(whatsappLinkDirSocket, {recursive : true, force: true}) } else { - if ( - lastDisconnect && - lastDisconnect.error && - lastDisconnect.error.output && - lastDisconnect.error.output.statusCode === 401 - ) { - FS.rmSync(whatsappLinkDirSocket, {recursive : true, force: true}) - connectSocketClient() - - } else { - console.log('Error unexpected', update) - } + WAnode.log('Node Refressed') } } - if(update.qr){ - QRCode.toDataURL(update.qr, function(err, url){ - var qrImageWithID = {}; - qrImageWithID.id = WAnode.id; - qrImageWithID.image = url; - RED.comms.publish("whatsappLinkQrCode", qrImageWithID); - }); - - QRCode.toString(update.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); - }); - } - if (connection === 'open') { - var qrImageWithID = {}; - qrImageWithID.id = WAnode.id; - qrImageWithID.image = null; - RED.comms.publish("whatsappLinkQrCode", qrImageWithID); - } - }) - return socketClient - }; - client = connectSocketClient(); - client.clientType = clientType; - WAnode.client = client + } + }) + return socketClient }; + client = connectSocketClient(); + client.clientType = clientType; + client.clientStartFunction = connectSocketClient; + WAnode.client = client + }; - this.on('close', (removed, done)=>{ - if(removed){ - if(clientType === "waWebClient"){ - clearInterval(WAnode.connectionSetupID); - WAnode.client.WAClose(); - } + this.on('close', (removed, done)=>{ + if(removed){ + if(clientType === "waWebClient"){ + clearInterval(WAnode.connectionSetupID); + WAnode.client.WAClose(); + } else { + WAnode.client.end() } - else { - if(clientType === "waWebClient"){ - clearInterval(WAnode.connectionSetupID); - WAnode.client.WAClose(); - } - } - done(); - }); + } + else { + if(clientType === "waWebClient"){ + clearInterval(WAnode.connectionSetupID); + WAnode.client.WAClose(); + } else { WAnode.client.end() } + } + done(); + + }); } RED.nodes.registerType("whatsappLink",RemoteClientNode);