Discover the Power of Whatsapp-web.js for Safe and Easy Automation


Discover the Power of Whatsapp-web.js for Safe and Easy Automation

Automate WhatsApp with ease using Whatsapp-web.js, a NodeJS client library that connects through the official WhatsApp Web app, reducing ban risks. Perfect for user or business accounts.

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app whatsapp-web.js - https://wwebjs.dev, with the Github repository

For this project I have set a Github repository @boobo94/whatsapp-sender.

How to setup the project

yarn init
yarn add whatsapp-web.js qrcode-terminal

then should look like:

{
  "name": "whatsapp-sender",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "qrcode-terminal": "^0.12.0",
    "whatsapp-web.js": "^1.22.2-alpha.4"
  }
}

Create an index.js file and add the code:

import { Client } from "whatsapp-web.js";
import qrcode  from 'qrcode-terminal';
import sendMessages from "./src/sendMessages.js";

const client = new Client();

client.on('qr', (qr) => {
    console.log('QR RECEIVED', qr);
    qrcode.generate(qr, { small: true });
});

client.on('ready', () => {
    console.log('Client is ready!');
});

client.on('authenticated', (session) => {    
    // Save the session object however you prefer.
    // Convert it to json, save it to a file, store it in a database...
    console.log('Authenticated')
});

client.on('message', message => {
    if (message.body === '!ping') {
        message.reply('pong');
    }
});

client.initialize();

Test it how it works

Start the codep project

yarn start

and you should see something like:

% yarn start
yarn run v1.22.19
$ node index.js
QR RECEIVED 2@+z3db4ckMTxcHkZDwWQ1Giql/nHg+J3orBdcPjik+Da8grWvNU80rx+MmZa2ow==,lZReDwpuYK+nakVa8B9/i1WhslxxbG3a2Nm+InE=,MwSE0qcS/nJ3k1yAU=,Xu2utPNJlPKbGEbvAyrvXI+igWG7QBZnNmcEnU=,1
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
█ ▄▄▄▄▄ █▀▄  ▀ █▄███ █▄▀ ▄▄▀▄█▄▄█▀▀▀▄▀ ██▄▄ ▀██ ▄▄▄▄▄ █
█ █   █ █▀█▄█▄ ▄▄█ ▀▄ ▄█▄▄▄█▀ ▄  ▄ ▀▀▀▀▄██▀▀▄▀█ █   █ █
█ █▄▄▄█ █▀ ▄█ ▀ ███▀ ███▄ ▄▄▄    █▄▀██▄█  █ ███ █▄▄▄█ █
█▄▄▄▄▄▄▄█▄▀▄▀ █ ▀▄▀▄█▄█ █ █▄█ ▀ ▀ █ ▀▄█▄█▄█▄█ █▄▄▄▄▄▄▄█
█ ▄▄  █▄▄ ▄ ▀▄▄▄▄▀▄ ▀▄▄▀▀▄  ▄  ▀▀██▄█▄▄██▀▀ ▄▄▄█ █▄█▄▀█
█▄ ▄▄▀ ▄▀█▀█▀ █▄▄ ▄▀▀      █▀▀▄▀▀▀▀▄██  ██▀▄  ▄█▀▄▀▄ ▀█
█▀▀▄█▄ ▄█▄▀▄▄█ ▀▀▄▄ ▀▄██ ▀█▄ ▀▀ ▄▀█▄ ▄ ▄█▀▄▀▀██▄  ██▄ █
█▄ ▄█▀▀▄ █▄█▀ ▀█▀▀  ▀ █▀█▀ █ ██ █▀ ▄▄▀██▄ ▄█▀ ▀▀▄█ ▄▄██
██▄▀█  ▄▄█ █▀▀█▄▀█▀ █ ▀█▀██▀█▄▀▄▀▄  ▀█▄ ▀▄█▀█▀▄▀ ▄ ▄▀▀█
█▄▀ █▄ ▄▀█▀ ▄▀ ▀▀▄  ▀██▀▀  ▀ █▀ ▄████▄ ██▀ ▀█▄██  ▀█ ██
█▀▀▄▄█ ▄ █▄█▄█▄  ▄ ▀▄▀ ▀█▀▀█▄█ ▄▄  ██ ██▀▀  ▄ ▀▀ ▄▀█▀▀█
█▄▀▀ ▀█▄█ ▀▄▄▄▄▄▄▀█▄ ▄▄▀ █▀▀ ▄▀▀ ██▄▄▄ ██▀  ▀▄ █▄▀█ ▄▀█
█▄ ▀▄▄▄▄▄█   ▄█ ▄ ▄▄█▄█▀▀█▄▀▀▄▄ █ ▀█ ██ █▀█ ▀ █▀   █▄▀█
████▄██▄▄  ▄▄ ▄▄▄█▄▄ ██▀▄ ▄▄▄ ▀ ▀███ ▄ ▄▄█  █ ▄▄▄ █▄  █
█ ▄▄▄▄▄ █▄██████ ▀▄▀ ███▀ █▄█ ▀ ▄ ▀███▀ ▄▄▄▄█ █▄█  ▄▀██
█ █   █ █ ▄▀ ▀ ▀ █▀▄ ████  ▄ ▄▄ ▄▀▄█ ▄██▀█▀ █ ▄▄▄ ▀ ▄▄█
█ █▄▄▄█ █ █▄▄▄▄█▀█▀ █▀ ███▄█  ▄█▀ ▄▄▄█▀ ▀▀▄  █▄▄ ▄▀▄▀▄█
█▄▄▄▄▄▄▄█▄██▄▄▄▄█▄█▄▄▄██▄▄█▄██▄█████▄▄█▄██▄▄███▄███████

Authenticated
Client is ready!

Let’s test the implementation

As you can see, we have a code block:

client.on('message', message => {
    if (message.body === '!ping') {
        message.reply('pong');
    }
});

Which listen for every message received. If the message is !ping, then it automatically replies with pong.

Now all you have to do is to send a message to the connected phone number, type !ping

How to automatically send messages to multiple numbers

Maybe you need to inform multiple clients or friends about the same topic or different messages. In this example, below I show you how to send the same message to all, but feel free to change the script and send different messages to each individual.

First of all I need to create the function which sends my notifications. Add it in your index.js file:

function sendMessages(client, data) {
    for (const contact of data.contacts) {
        // get the number id from whatsapp of this phone number
        const numberDetails = await client.getNumberId(contact);
        // if the phone number is a valid whatsapp account, send the message
        if (numberDetails) {
            const sendMessageData = await client.sendMessage(numberDetails._serialized, data.message); // send message
            console.log(`Message sent to ${contact} successfully`);
        } else {
            console.log(`Mobile number <${contact}> is not registered`);
        }

        // add a delay of 5s to don't trigger any whatsapp api blocker
        // !!! this is not necessary a safe method, but it may help
        await new Promise(resolve => setTimeout(resolve, 5000));
    }
}

As you can see in function definition it has two params:

  • client - the whatsapp client created above
  • data - where are stored the contacts and the message

The object looks like below and it’s stored in data.js file, imported in index.js.

// data.js

export default {
    message: "test message ",
    contacts: [
        "4072XYZXYZX",
        "4072AXYZXYZ",
    ]
}

!!! Very important to use the international prefix for your country.

Then I have to call that function inside of ready event, so the events now looks like:

// index.js
import data from "./data.js";

client.on('ready', () => {
    console.log('Client is ready!');

    sendMessages(client, data)
});

Known issues & fixies

On whatsapp-web.js v1.22.1, after QR scanning is stucked.

In order to fix it, go to node_modules/whatsapp-web.js/src/CLient.js#175 (line 175) and replace

const INTRO_IMG_SELECTOR = '[data-testid="intro-md-beta-logo-dark"], [data-testid="intro-md-beta-logo-light"], [data-asset-intro-image-light="true"], [data-asset-intro-image-dark="true"]';

with

const INTRO_IMG_SELECTOR = 'div[role=\'textbox\']';

Source

Newsletter


Related Posts

A Week in the Life of an Invoice Wrangler: Navigating Ridesharing and Food Delivery Chaos

As an app founder in the ridesharing and food delivery industry, I found myself knee-deep in invoice reports from companies like Bolt, Uber, Glovo, and Bolt Food

Free HTML templates list for Startups

Free HTML templates list for startup. A complete list with free resources to build your next startup's website and gain the traction to the sky.

Deal with client requests in SaaS

How to deal with client requests in Saas which are seeing only their interests, not the product interest.

The first client of LoyalXpert is not answering anymore

Trying to implement LoyalXpert app, I lost my first customer, he's not answering anymore

Experiments with Tiktok Ads

Recently tried out TikTok ads for the first time and here are some of my learnings and challenges

People don’t care about you, until they know you care about them.

People don’t care about you, until they know you care about them. The same happens in business, you need to take care of your clients.

The One Word That Can Ruin Your SaaS Business Anyone

As a SaaS founder, you probably know how important it is to have a clear and specific target market for your product.

How I got my digital certificate connected it with ANAF

How I got my digital certificate from certSIGN and connected it with ANAF

The Ultimate List of Company Directories to Boost Your Networking

Discover a wide range of company directories to boost your business's visibility and connect with potential clients.

Discover the Best Free AI Art Tools for Your Next Masterpiece

Explore a curated collection of the finest free AI art tools, designed to help you bring your artistic vision to life.