updating Node Names

This commit is contained in:
rawee 2022-12-01 21:51:33 +05:30
commit 210fcb9aae
12 changed files with 2886 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Whatsapp Bot
Simple Node for connecting Node-Red to Whatsapp.

2530
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

35
package.json Normal file
View File

@ -0,0 +1,35 @@
{
"name": "whatsapp-bot",
"version": "0.1.1",
"description": "Whatsapp connection with Node-Red",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/raweee/whatsapp-bot.git"
},
"keywords": [
"node-red",
"whatsapp",
"message"
],
"author": "Ravi Mishra",
"license": "ISC",
"node-red": {
"nodes": {
"whatsapp admin": "whatsappAdmin.js",
"whatsapp in": "whatsapp-in.js",
"whatsapp Out": "whatsapp-out.js",
"whatsapp Link": "whatsappLink.js"
}
},
"bugs": {
"url": "https://github.com/raweee/whatsapp-bot/issues"
},
"dependencies": {
"qrcode": "^1.5.1",
"whatsapp-web.js": "^1.18.4"
}
}

31
whatsapp-in.html Normal file
View File

@ -0,0 +1,31 @@
<script type="text/javascript">
RED.nodes.registerType('whatsapp-in',{
category: 'whatsapp',
color: '#25D366',
defaults: {
name: {value:"Chats In"},
whatsappLink: {value:"whatsapp-web", type:'whatsappLink'}
},
outputs:1,
icon: 'font-awesome/fa-commenting',
label: function() {
return this.name||"Whatsapp In";
}
});
</script>
<script type="text/html" data-template-name="whatsapp-in">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-whatsappLink"><i class="fa fa-tag"></i> Client</label>
<input type="text" id="node-input-whatsappLink" placeholder="Name">
</div>
</script>
<script type="text/html" data-help-name="whatsapp-in">
<p>A simple node to recive whatsapp messages.</p>
</script>

49
whatsapp-in.js Normal file
View File

@ -0,0 +1,49 @@
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 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);
};
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') {
var fromContact = msg.from.match(/\d+/);
msg.reply('pong from Node-Red');
msg.payload = `!ping recieved from ${fromContact}.`
node.send(msg);
}
});
node.on(`close`, ()=>{
SetStatus("Disconnected", "red");
});
}
RED.nodes.registerType("whatsapp-in", WhatsappIn);
}

38
whatsapp-out.html Normal file
View File

@ -0,0 +1,38 @@
<script type="text/javascript">
RED.nodes.registerType('whatsapp-out',{
category: 'whatsapp',
color: '#25D366',
defaults: {
name: {value:"Message Out"},
whatsappLink: {value:"whatsapp-web", type:'whatsappLink'},
number: {value: ""}
},
outputs:0,
inputs:1,
icon: 'font-awesome/fa-commenting',
align: 'right',
label: function() {
return this.name||"Whatsapp Out";
}
});
</script>
<script type="text/html" data-template-name="whatsapp-out">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-whatsappLink"><i class="fa fa-tag"></i> Client</label>
<input type="text" id="node-input-whatsappLink" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-number"><i class="fa fa-tag"></i> Number</label>
<input type="text" id="node-input-number" placeholder="Mobile Number..">
</div>
</script>
<script type="text/html" data-help-name="whatsapp-out">
<p>A simple node to recive whatsapp messages.</p>
</script>

30
whatsapp-out.js Normal file
View File

@ -0,0 +1,30 @@
module.exports = function(RED) {
function WhatsappOut(config) {
RED.nodes.createNode(this,config);
var node = this;
this.number = config.number;
this.number = this.number.includes('@c.us') ? this.number : `${this.number}@c.us`;
var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink);
node.waClient = whatsappLinkNode.client;
var whatsappConnectionStauts = null;
async function whatsappConnection(){
whatsappConnectionStauts = await node.waClient.getState();
};
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.on('input', (message)=> {
node.waClient.sendMessage(this.number, message.payload);
});
}
RED.nodes.registerType("whatsapp-out", WhatsappOut);
}

32
whatsappAdmin.html Normal file
View File

@ -0,0 +1,32 @@
<script type="text/javascript">
RED.nodes.registerType('whatsapp-admin',{
category: 'whatsapp',
color: '#25D366',
defaults: {
name: {value:"whatsapp-admin"},
whatsappLink: {value:"", type: "whatsappLink"}
},
outputs:1,
inputs:1,
icon: 'font-awesome/fa-comments',
label: function() {
return this.name||"Whatsapp Admin";
}
});
</script>
<script type="text/html" data-template-name="whatsapp-admin">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-whatsappLink"><i class="fa fa-tag"></i> Client</label>
<input type="text" id="node-input-whatsappLink" placeholder="Name">
</div>
</script>
<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>
</script>

72
whatsappAdmin.js Normal file
View File

@ -0,0 +1,72 @@
module.exports = function(RED) {
const QRCode = require('qrcode');
function WhatsappAdmin(config) {
RED.nodes.createNode(this,config);
var node = this;
var whatsappLinkNode = RED.nodes.getNode(config.whatsappLink);
node.waClient = whatsappLinkNode.client;
node.WARestart = whatsappLinkNode.WARestart;
node.WAConnect = whatsappLinkNode.WAConnect;
function SetStatus(WAStatus, color){
node.status({fill:color,shape:"dot",text:WAStatus});
msg = {payload : WAStatus};
node.send(msg);
};
// Commands recived for Whatsapp Client.
this.on('input', function(msg, send){
if (msg.payload === "destroy") {
node.waClient.destroy();
SetStatus("Disconnected","red");
}
else if (msg.payload==="logout") {
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 === "restart"){
node.WARestart();
SetStatus("Connecting...", "yellow");
};
});
this.on(`close`, ()=>{
SetStatus("Disconnected", "red");
});
//QR Code generation.
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('ready', () => {
console.log('Whatsapp Client is ready!');
SetStatus('Connected','green');
});
node.waClient.on('disconnected', () => {
console.log('WA Client is Disconnected!');
SetStatus("Disconeccted","red");
});
}
RED.nodes.registerType("whatsapp-admin", WhatsappAdmin);
}

21
whatsappLink.html Normal file
View File

@ -0,0 +1,21 @@
<script type="text/javascript">
RED.nodes.registerType('whatsappLink',{
category: 'config',
defaults: {
cName : {value:"whatsapp-web",required:true}
},
label: function() {
return this.cName ;
}
});
</script>
<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>
<input type="text" id="node-config-input-cName">
</div>
<div class="form-row">
<label> Don't put space in Name. </label>
</div>
</script>

44
whatsappLink.js Normal file
View File

@ -0,0 +1,44 @@
module.exports = function(RED) {
const { Client, LocalAuth } = require('whatsapp-web.js');
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
}),
puppeteer : {headless : true }
});
let WAConnect = function(){
WAnode.client.initialize();
WAnode.log("Connecting to Whatsapp..");
};
WAConnect();
let WARestart = function(){
WAnode.client.destroy();
WAnode.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();
}
done();
});
//this.client = client ;
this.WAConnect = WAConnect;
this.WARestart = WARestart;
}
RED.nodes.registerType("whatsappLink",RemoteClientNode);
}