Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const randomBytes = require('randombytes');
- const WebSocket = require('ws');
- const QrCode = require('qrcode');
- const createCounter = start => {
- let count = start || 0;
- return () => {
- return count++;
- }
- }
- const incrementCount = createCounter(1);
- const createMessageTag = () => {
- const date = new Date();
- const dateTime = date.getTime();
- const textTime = dateTime.toString().substring(0, 10);
- const count = incrementCount();
- const messageTag = `${textTime}.--${count}`;
- return messageTag;
- }
- const msgTag = createMessageTag();
- const clientId = randomBytes(16);
- const WHATSAPP_WEB_VERSION = '0,4,2088';
- let msg = `${msgTag},[\"admin\",\"init\",[${WHATSAPP_WEB_VERSION}],[\"Windows\",\"Chrome\",\"10\"],\"${clientId}\",true]`;
- const getJsonText = (text) => {
- const indexOfFirstComma = text.indexOf(',');
- const newText = text.substring(indexOfFirstComma + 1, text.length);
- const jsonText = JSON.parse(newText);
- return jsonText;
- }
- const isStatusOk = text => {
- const jsonText = getJsonText(text);
- const { status } = jsonText;
- return status === 200
- }
- const isConnected = text => {
- const jsonText = getJsonText(text);
- const { connected } = jsonText;
- return connected || false;
- }
- const whatsappHost = 'wss://web.whatsapp.com/ws';
- const ws = new WebSocket(whatsappHost, {
- origin: 'https://web.whatsapp.com'
- });
- ws.on('open', () => {
- ws.send(`web socket connected on ${whatsappHost}`);
- console.log('connected websocket');
- ws.send(msg);
- });
- ws.on('message', (data) => {
- console.log('ws data: ', data);
- const statusConnection = isStatusOk(data);
- console.log('Connection status:', statusConnection);
- if(statusConnection) {
- const connectionInfo = getJsonText(data);
- console.log('connection info:', connectionInfo);
- const qrCodeOptions = { type: 'terminal', errorCorrectionLevel: 'Q' };
- const { ref, } = connectionInfo;
- const codeToGenerate = ``;
- QrCode.toString(codeToGenerate, qrCodeOptions, (err, url) => {
- if(err) {
- console.log('error:', err);
- return;
- }
- console.log('QRCODE:');
- console.log(url);
- })
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement