Skip to content

Instantly share code, notes, and snippets.

@DavAlbert
Created January 2, 2020 10:01
Show Gist options
  • Save DavAlbert/a14c5c37d7fa35be1d2b8297eecf67ab to your computer and use it in GitHub Desktop.
Save DavAlbert/a14c5c37d7fa35be1d2b8297eecf67ab to your computer and use it in GitHub Desktop.
const WebSocket = require('ws');
const Crypto = require('crypto');
const EC = require('elliptic').ec;
const ec = new EC('curve25519');
var whatsAppRunning = false;
var ws = null;
var qrCodeString = null;
var clientId = null;
var messageTag = null;
const whatsAppKey = ec.genKeyPair();
const userInformation = {
phoneNumber: '',
locales: '',
waVersion: '',
phone: '',
name: ''
};
function isWhatsAppAlive() {
return whatsAppRunning;
}
function getQrCodeString() {
return qrCodeString;
}
function getUserInformation() {
return userInformation;
}
function initWebsocket() {
ws = new WebSocket('wss://web.whatsapp.com/ws', {
origin: 'https://web.whatsapp.com'
});
ws.on('open', () => {
ws.send(getQrCodeRequestMessage());
});
ws.on('close', (e) => {
console.log('CLOOSED!!');
console.log(e);
ws = null;
whatsAppRunning = false;
});
ws.on('message', (data) => {
if (data != null) {
try {
var messageType = data.split(',')[0];
if (messageType.includes('.')) {
messageType = messageType.split('.')[1]
}
switch (messageType) {
case '--0':
generateQrCodeString(data);
break;
case 's1':
whatsAppRunning = true;
subscribeNumber('49123123123');
receiveProfileInformation(data);
break;
default:
const payload = JSON.parse(data.substring(data.indexOf(",") + 1));
switch (payload[0]) {
case 'Presence':
const phone = payload[1].id.split('@')[0];
const type = payload[1].type;
console.log(phone + ' = ' + type);
break;
default:
}
}
} catch { }
}
});
}
function subscribeNumber(number) {
ws.send(`812.--26,,["action","presence","subscribe","${number}@c.us"]`)
}
function generateQrCodeString(data) {
const qrMessage = JSON.parse(data.split('--0,')[1]);
const ref = qrMessage.ref;
const pubs64 = whatsAppKey.getPrivate().toBuffer().toString('base64');
qrCodeString = ref + ',' + pubs64 + ',' + clientId;
}
function getQrCodeRequestMessage() {
clientId = Crypto.randomBytes(16).toString('base64');
messageTag = Math.floor(Date.now() / 1000) + '';
return `${messageTag}.--0,["admin","init",[0,3,9309],["Muschel","","10.13.6"],"${clientId}",true]`;
}
function receiveProfileInformation(data) {
const connection = JSON.parse(data.split('s1,')[1])[1];
userInformation.phoneNumber = connection.wid.split('@')[0];
userInformation.locales = connection.locales;
userInformation.name = connection.pushname;
userInformation.waVersion = connection.phone.wa_version;
userInformation.phone = connection.phone.device_manufacturer + ', ' + connection.phone.device_model;
}
module.exports = {
initWebsocket: initWebsocket,
getQrCodeString: getQrCodeString,
isWhatsAppAlive: isWhatsAppAlive,
getUserInformation: getUserInformation,
subscribeNumber: subscribeNumber
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment