Search code examples
javascriptnode.jsherokuserver

Deploy whatsapp-web bot on heroku


I'm creating a whatsapp bot using the node library whatsapp-web.js After I'm done with the script it looks something like (I just put a overview of the orignal script) -

index.js

const {Client, LocalAuth, MessageMedia } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');


const client = new Client({
  puppeteer: {
    args: ['--no-sandbox', "--disable-setuid-sandbox"]
  },
  authStrategy: new LocalAuth()
});
  
client.on('qr', (qr) => {
  console.log('qr received: ', qr);
qrcode.generate(qr, {small:true});
});
  
client.on('ready', () => {
    console.log('READY');
});

client.on('message', async msg => {
  let type = msg.type;
  let chat = await msg.getChat();
  if(chat.isGroup) {
    //do something
  }else {
    //
    if(msg.body === "ping") {
      msg.reply("pong");
    }
  }
});

Everything is fine with the script and it works good on linux or ubuntu (I already added puppeteer build pack on that Heroku app). As I need to run that script continuously I decided to put that on a worker process.

Procfile

worker: node index.js But now the problem comes in role, how can I authenticate here? I decided to remove that line from index.js

qrcode.generate(qr,{small:true}); And insted I thought I will print all the logs on heroku-cli

heroku logs -a wweb-bot #my app named as wweb-bot and from there access the key generated as qr. After that I'll turn it into a qrcode and scan it. When I did all setup and try it I was getting a continuously generating logs of qr keys. It's nonstop, and keep generating keys after every 15-20 sec. What's the problem here? Is it because Heroku has a read only environment or anything else is missing? Please help me how can i do it


Solution

  • Edit: now whatsapp-web.js added new functionality of doing this called RemoteAuthStatergy just go throughout it.