Readme updated and bug removed

This commit is contained in:
rawee 2022-12-06 02:04:24 +05:30
parent 95458a2bc1
commit a14e82514c
8 changed files with 146 additions and 75 deletions

View File

@ -1,3 +1,25 @@
# Whatsapp Bot
Simple Node for connecting Node-Red to Whatsapp.
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

View File

@ -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"

View File

@ -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);
}

View File

@ -34,5 +34,6 @@
<script type="text/html" data-help-name="whatsapp-out">
<p>A simple node to recive whatsapp messages.</p>
<p>A simple node to Send whatsapp messages.</p>
<p>Number should be with contry code. Example : +1 99999 99999</p>
</script>

View File

@ -28,5 +28,7 @@
<script type="text/html" data-help-name="whatsapp-admin">
<p>A simple node that create Whatsapp clinet and generate QR code to connect with WhatsApp.</p>
<p>A simple Admin node.</p>
<p>1. provide status of Whatsapp.</p>
<p>2. get commonds to control Whatsapp.</p>
</script>

View File

@ -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);

View File

@ -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 @@
<script type="text/html" data-template-name="whatsappLink">
<div class="form-row">
<label for="node-config-input-cNAme"><i class="fa fa-bookmark"></i> Host</label>
<label for="node-config-input-cName"><i class="fa fa-bookmark"></i> Host</label>
<input type="text" id="node-config-input-cName">
</div>
<div class="form-row">
<label> Don't put space in Name. </label>
<div>
<label><p> Don't put space in Name. </p></label>
</div>
</script>

View File

@ -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);
}