Site icon Coding is Love

How to send a PDF to multiple contacts/groups using WhatsApp API

I made a video explaining how to do Whatsapp automation using Javascript. Someone has asked this question about how to send a PDF to multiple contacts or groups using whatsapp-web.js in the comments of that video. Let’s find out!

Here’s the example source code to send a PDF to multiple contacts or groups using WhatsApp API

Important: If you abuse this API and send bulk messages to bulk contacts as spam then you might get banned by WhatsApp so be careful when you send any kind of bulk messages.

If you run this code then a PDF named “sample.pdf” from the project folder will be sent to the list of chats defined in the myChats array. Modify the myChats array and PDF attachment name and path as per your requirement.

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

const myChats = ["Contact1", "Contact2", "Group1"]; // Add a list of contact names or group names here

const client = new Client({
  authStrategy: new LocalAuth(),
});

client.on("qr", (qr) => {
  qrcode.generate(qr, { small: true });
});

client.on("ready", () => {
  console.log("Client is ready!");
  client.getChats().then((chats) => {
    myChats.forEach((chatName) => {
      const myChat = chats.find((chat) => chat.name === chatName);
      if(myChat){
        const attachmentPdf = MessageMedia.fromFilePath("./sample.pdf");
        client.sendMessage(myChat.id._serialized, attachmentPdf); 
      }
      else {
        console.log(`Chat ${chatName} not found`);
      }
    });
  });
});

client.initialize();

Exit mobile version