DEV Community

Cover image for πŸŽ‰ WhatsApp Message Automation for New Year Greetings with Node.jsπŸŽ‰
Deepak Padliya
Deepak Padliya

Posted on

πŸŽ‰ WhatsApp Message Automation for New Year Greetings with Node.jsπŸŽ‰

The new year is the perfect time to reach out to your friends, family, and contacts. But manually sending messages to dozens or hundreds of people can be exhausting.

In this tutorial, you’ll learn how to automate sending Happy New Year messages on WhatsApp using Node.js and whatsapp-web.js β€” entirely locally and securely.

⚠️ Important: This automation runs entirely on your computer. Your WhatsApp account remains private, and your credentials are never shared.


Step 1: Install Node.js

We’ll use Node.js v18 LTS or higher.

macOS

# Install Homebrew if not already
/bin/bash -c "$(curl -fsSL [https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh](https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh))"

# Install Node 18
brew install node@18
echo 'export PATH="/usr/local/opt/node@18/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

# Verify installation
node -v
npm -v

Enter fullscreen mode Exit fullscreen mode

Windows

Download Node.js LTS (v18 or higher) from nodejs.org
and run the installer.

Make sure β€œAdd to PATH” is checked.
Verify installation in PowerShell:

node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up Project Folder and Install Dependencies

macOS

mkdir ~/whatsapp-bot
cd ~/whatsapp-bot
npm init -y
npm install whatsapp-web.js qrcode-terminal formdata-node
Enter fullscreen mode Exit fullscreen mode

Windows

mkdir C:\whatsapp-bot
cd C:\whatsapp-bot
npm init -y
npm install whatsapp-web.js qrcode-terminal formdata-node
Enter fullscreen mode Exit fullscreen mode

Packages Installed:

  • whatsapp-web.js β†’ Automates WhatsApp Web
  • qrcode-terminal β†’ Displays QR codes in the terminal
  • formdata-node β†’ Provides File polyfill for Node.js environments

Step 3: Set Up Project Folder and Install Dependencies

Create a file named contacts2025_send.js in your project folder:

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

const client = new Client({
  authStrategy: new LocalAuth({ clientId: 'default' }),
  puppeteer: { headless: false } // Opens Chrome for authentication
});

client.on('ready', async () => {
  console.log('βœ… WhatsApp Web authenticated');

  const chats = await client.getChats();
  const contacts2025 = [];

  for (const chat of chats) {
    if (chat.isGroup) continue; // skip groups; remove this line to include groups

    const messages = await chat.fetchMessages({ limit: 50 });
    for (const msg of messages) {
      if (!msg.fromMe) {
        const msgDate = new Date(msg.timestamp * 1000);
        if (msgDate.getFullYear() === 2025) {
          contacts2025.push({ id: chat.id._serialized, name: chat.name || chat.id._serialized });
          break;
        }
      }
    }
  }

  console.log('Contacts who messaged you in 2025:');
  contacts2025.forEach(c => console.log(c.name));

  for (const contact of contacts2025) {
    await client.sendMessage(contact.id, 'πŸŽ‰ Happy New Year! πŸŽ‰');
    console.log(`Message sent to ${contact.name}`);
    await new Promise(r => setTimeout(r, 5000)); // 5-second delay
  }

  client.destroy();
});

client.initialize();

Enter fullscreen mode Exit fullscreen mode

Step 4: Set Up Project Folder and Install Dependencies

macOS

cd ~/whatsapp-bot
node contacts2025_send.js
Enter fullscreen mode Exit fullscreen mode

Windows

cd C:\whatsapp-bot
node contacts2025_send.js
Enter fullscreen mode Exit fullscreen mode

Workflow:

  • The first time you run it, a QR code will appear in the terminal. Open WhatsApp β†’ Linked Devices β†’ Link a Device β†’ Scan QR code.
  • The session is saved locally using LocalAuth. Future runs won’t require scanning the QR code again.
  • The script automatically sends Happy New Year messages to all contacts who messaged you in 2025.

Step 5: Optional Enhancements

  • Include Groups: Remove the line:

    if (chat.isGroup) continue;

  • Personalized Messages:

    await client.sendMessage(contact.id, `πŸŽ‰ Happy New Year, ${contact.name}! πŸŽ‰`);

  • Adjust Delays: Increase the setTimeout duration to avoid WhatsApp rate limits.

  • Logging: Save sent messages to a log file for tracking.

Top comments (0)