Venom-bot for WhatsApp

Hi all,
I am now testing Venom-bot for WhatsApp and was really easy to setup. Of course there might be some Cons but as this is just a learning curve and testing might be worth sharing…

NodeJs Server

const express = require(‘express’);
const app = express();
const http = require(‘http’);
const server = http.createServer(app);
//const { Server } = require(“socket.io”); Was trying to use for message exchange between Nodejs and Frappe
const io = new Server(server);
const path = require(‘path’);

const porta = 4400;

const venom = require(‘venom-bot’);

app.get(‘/api/whatsapp-web’, whatsappweb_url);

function whatsappweb_url(request, response, next) {
console.log(request.params);
console.log(__dirname);
response.sendFile(path.join(__dirname+‘/whazapp.html’));

venom
.create(
    'sessionName',
    (base64Qr, asciiQR) => {
        console.log(asciiQR); // Optional to log the QR in the terminal
        
        var matches = base64Qr.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
            response = {};

        if (matches.length !== 3) {
            return new Error('Invalid input string');
        }
        
        response.type = matches[1];
        response.data = new Buffer.from(matches[2], 'base64');

        var imageBuffer = response;

        require('fs').writeFile(
            'out.png',
            imageBuffer['data'],
            'binary',
            function (err) {
            if (err != null) {
                console.log(err);
            }
            }
        );

    },
    // statusFind
    (statusSession, session) => {
        console.log('Status Session: ', statusSession); //return isLogged || notLogged || browserClose || qrReadSuccess || qrReadFail || autocloseCalled || desconnectedMobile || deleteToken
        //Create session wss return "serverClose" case server for close
        console.log('Session name: ', session);
        if (statusSession == "notLogged"){
            //To QRCode page
           console.log('statusSession');
           io.sockets.emit('message', "NAO ESTA LIGADO!");
            
        }
    },
    undefined,
    { logQR: false }
)
.then((client) => {
    start(client);
})
.catch((erro) => {
    console.log(erro);
});

async function start(client) {
    client.onMessage(async (message) => {
        if (message.body === 'Hi' && message.isGroupMsg === false) {
            client
            .sendText(message.from, 'Welcome Venom 🕷')
            .then((result) => {
                console.log('Result: ', result); //retorna o object com successo
            })
            .catch((erro) => {
                console.error('Error when sending: ', erro); //retorna o object error
            });
        } else if (message.body === '!factura' && message.isGroupMsg === false){
            console.log('Enviar Factura PDF... ', "/home/frappe/frappe-bench/" + pastapublico + 'FT-21-2.pdf');
            await client.sendFile(message.from,"/home/frappe/frappe-bench/" + pastapublico + 'FT-21-2.pdf');
        }

    });

    client.onStateChange((state) => {
        console.log('Status');
        console.log(state);
        if (state == "CONNECTED"){
            io.emit('chat message', 'Cliente esta pronto para enviar Mensagens!');	
        } else if (state == "DISCONNECTED"){
            io.emit('chat message', 'Cliente Desligado!');	
        }
        
    });

server.listen(porta, () => {
console.log(listening on *:${porta});
});

3 Likes

How to send qrCode gerenate to index.html ?

Looking forward for more updates.