Readme updated and bug removed
This commit is contained in:
parent
95458a2bc1
commit
a14e82514c
22
README.md
22
README.md
@ -1,3 +1,25 @@
|
|||||||
# Whatsapp Bot
|
# 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
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "node-red-contrib-whatsapp-link",
|
"name": "node-red-contrib-whatsapp-link",
|
||||||
"version": "0.1.1",
|
"version": "0.1.12",
|
||||||
"description": "Whatsapp connection with Node-Red",
|
"description": "Whatsapp connection with Node-Red",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
|||||||
@ -6,44 +6,50 @@ module.exports = function(RED) {
|
|||||||
var node = this;
|
var node = this;
|
||||||
var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink);
|
var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink);
|
||||||
node.waClient = whatsappLinkNode.client;
|
node.waClient = whatsappLinkNode.client;
|
||||||
var whatsappConnectionStauts = null;
|
|
||||||
|
|
||||||
async function whatsappConnection(){
|
|
||||||
whatsappConnectionStauts = await node.waClient.getState();
|
|
||||||
};
|
|
||||||
|
|
||||||
function SetStatus(WAStatus, color){
|
function SetStatus(WAStatus, color){
|
||||||
node.status({fill:color,shape:"dot",text:WAStatus});
|
node.status({fill:color,shape:"dot",text:WAStatus});
|
||||||
msg = {payload : WAStatus};
|
|
||||||
node.send(msg);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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 => {
|
node.waClient.on('message', async msg => {
|
||||||
msg.payload = msg.body;
|
msg.payload = msg.body;
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
|
|
||||||
// Whatsapp Chats testing text.
|
// Whatsapp Chats testing text.
|
||||||
if(msg.body === '!ping') {
|
if(msg.body === '!nodered') {
|
||||||
var fromContact = msg.from.match(/\d+/);
|
var fromContact = msg.from.match(/\d+/);
|
||||||
msg.reply('pong from Node-Red');
|
msg.reply('Hi from Node-Red');
|
||||||
msg.payload = `!ping recieved from ${fromContact}.`
|
msg.payload = `!nodered recieved from ${fromContact}.`
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
node.on(`close`, ()=>{
|
//whatsapp Status Parameters----
|
||||||
SetStatus("Disconnected", "red");
|
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);
|
RED.nodes.registerType("whatsapp-in", WhatsappIn);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,5 +34,6 @@
|
|||||||
|
|
||||||
|
|
||||||
<script type="text/html" data-help-name="whatsapp-out">
|
<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>
|
</script>
|
||||||
|
|||||||
@ -28,5 +28,7 @@
|
|||||||
|
|
||||||
|
|
||||||
<script type="text/html" data-help-name="whatsapp-admin">
|
<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>
|
</script>
|
||||||
|
|||||||
@ -17,21 +17,18 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
// Commands recived for Whatsapp Client.
|
// Commands recived for Whatsapp Client.
|
||||||
|
|
||||||
this.on('input', function(msg, send){
|
this.on('input', async function(msg, send){
|
||||||
if (msg.payload === "destroy") {
|
if (msg.payload === "destroy") {
|
||||||
node.waClient.destroy();
|
await node.waClient.destroy();
|
||||||
SetStatus("Disconnected","red");
|
SetStatus("Disconnected","red");
|
||||||
}
|
}
|
||||||
else if (msg.payload==="logout") {
|
else if (msg.payload==="logout") {
|
||||||
node.waClient.logout();
|
await node.waClient.logout();
|
||||||
SetStatus("Logged Out..","red");
|
SetStatus("Logged Out","red");
|
||||||
}
|
}
|
||||||
else if (msg.payload === "test"){
|
else if (msg.payload === "state"){
|
||||||
async function test() {
|
msg.payload = await node.waClient.getState();
|
||||||
msg.payload = await node.waClient.getState();
|
node.send(msg);
|
||||||
node.send(msg);
|
|
||||||
}
|
|
||||||
test();
|
|
||||||
}
|
}
|
||||||
else if (msg.payload === "restart"){
|
else if (msg.payload === "restart"){
|
||||||
node.WARestart();
|
node.WARestart();
|
||||||
@ -39,33 +36,33 @@ module.exports = function(RED) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
this.on(`close`, ()=>{
|
//whatsapp Status Parameters----
|
||||||
SetStatus("Disconnected", "red");
|
|
||||||
});
|
|
||||||
|
|
||||||
//QR Code generation.
|
|
||||||
node.waClient.on('qr', (qr) => {
|
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");
|
SetStatus("QR Code Generated", "yellow");
|
||||||
QRCode.toDataURL(qr, function(err, url){
|
QRCode.toDataURL(qr, function(err, url){
|
||||||
SetStatus("Scan QR in Terminal", "green");
|
|
||||||
msg = {payload : url};
|
msg = {payload : url};
|
||||||
node.send(msg);
|
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', () => {
|
node.waClient.on('ready', () => {
|
||||||
console.log('Whatsapp Client is ready!');
|
|
||||||
SetStatus('Connected','green');
|
SetStatus('Connected','green');
|
||||||
});
|
});
|
||||||
|
|
||||||
node.waClient.on('disconnected', () => {
|
node.waClient.on('disconnected', () => {
|
||||||
console.log('WA Client is Disconnected!');
|
SetStatus("Disconnected","red");
|
||||||
SetStatus("Disconeccted","red");
|
});
|
||||||
|
|
||||||
|
this.on(`close`, ()=>{
|
||||||
|
SetStatus("Disconnected", "red");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("whatsapp-admin", WhatsappAdmin);
|
RED.nodes.registerType("whatsapp-admin", WhatsappAdmin);
|
||||||
|
|||||||
@ -2,7 +2,8 @@
|
|||||||
RED.nodes.registerType('whatsappLink',{
|
RED.nodes.registerType('whatsappLink',{
|
||||||
category: 'config',
|
category: 'config',
|
||||||
defaults: {
|
defaults: {
|
||||||
cName : {value:"whatsapp-web",required:true}
|
cName : {value:"whatsapp-web",required:true},
|
||||||
|
name : {value : "Web"}
|
||||||
},
|
},
|
||||||
label: function() {
|
label: function() {
|
||||||
return this.cName ;
|
return this.cName ;
|
||||||
@ -12,10 +13,10 @@
|
|||||||
|
|
||||||
<script type="text/html" data-template-name="whatsappLink">
|
<script type="text/html" data-template-name="whatsappLink">
|
||||||
<div class="form-row">
|
<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">
|
<input type="text" id="node-config-input-cName">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div>
|
||||||
<label> Don't put space in Name. </label>
|
<label><p> Don't put space in Name. </p></label>
|
||||||
</div>
|
</div>
|
||||||
</script>
|
</script>
|
||||||
@ -1,44 +1,86 @@
|
|||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
const { Client, LocalAuth } = require('whatsapp-web.js');
|
const { Client, LocalAuth } = require('whatsapp-web.js');
|
||||||
|
const QRCode = require('qrcode');
|
||||||
|
|
||||||
function RemoteClientNode(n) {
|
function RemoteClientNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
let WAnode = this;
|
let WAnode = this;
|
||||||
WAnode.client = n.cName;
|
let whatsappConnectionStatus;
|
||||||
let WAClientID = `${n.cName}ID`;
|
|
||||||
|
|
||||||
WAnode.client = new Client({
|
const client = new Client({
|
||||||
authStrategy : new LocalAuth({
|
authStrategy : new LocalAuth(),
|
||||||
clientId : WAClientID
|
|
||||||
}),
|
|
||||||
puppeteer : {headless : true }
|
puppeteer : {headless : true }
|
||||||
});
|
});
|
||||||
|
// WAnode.log(`Client is generated`);
|
||||||
|
|
||||||
let WAConnect = function(){
|
let WAConnect = function(){
|
||||||
WAnode.client.initialize();
|
try {
|
||||||
WAnode.log("Connecting to Whatsapp..");
|
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();
|
WAConnect();
|
||||||
|
|
||||||
let WARestart = function(){
|
async function WAClose(){
|
||||||
WAnode.client.destroy();
|
await client.destroy();
|
||||||
WAnode.client.initialize();
|
};
|
||||||
|
|
||||||
|
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)=>{
|
this.on('close', (removed, done)=>{
|
||||||
if(!removed){
|
if(removed){
|
||||||
console.log(`closing WA admin closing in new line also`)
|
clearInterval(connectionSetupID);
|
||||||
async function distroyWA() {
|
WAClose();
|
||||||
await WAnode.client.destroy();
|
}
|
||||||
};
|
else {
|
||||||
distroyWA();
|
clearInterval(connectionSetupID);
|
||||||
|
WAClose();
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//this.client = client ;
|
|
||||||
this.WAConnect = WAConnect;
|
this.WAConnect = WAConnect;
|
||||||
|
this.client = client;
|
||||||
|
|
||||||
this.WARestart = WARestart;
|
this.WARestart = WARestart;
|
||||||
|
this.whatsappConnectionStatus = whatsappConnectionStatus;
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("whatsappLink",RemoteClientNode);
|
RED.nodes.registerType("whatsappLink",RemoteClientNode);
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user