🎉 WhatsApp Message Automation for New Year Greetings with Node.js🎉
Source: Dev.to
Overview
Automate sending Happy New Year messages on WhatsApp using Node.js and whatsapp-web.js.
The automation runs entirely on your computer, keeping your WhatsApp account private and your credentials secure.
Prerequisites
- Node.js v18 LTS or higher
- (Optional) Homebrew on macOS for easy installation of Node.js
Installation
macOS (Homebrew)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install node@18
echo 'export PATH="/usr/local/opt/node@18/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
Verify the installation:
node -v
npm -v
Windows / Manual Install
Download Node.js LTS (v18 or higher) from nodejs.org and ensure “Add to PATH” is checked.
node -v
npm -v
Project Setup
Create a project folder and install the required packages.
macOS / Linux
mkdir ~/whatsapp-bot
cd ~/whatsapp-bot
npm init -y
npm install whatsapp-web.js qrcode-terminal formdata-node
Windows
mkdir C:\whatsapp-bot
cd C:\whatsapp-bot
npm init -y
npm install whatsapp-web.js qrcode-terminal formdata-node
Script: contacts2025_send.js
Create a file named contacts2025_send.js in the project folder and paste the following code:
// contacts2025_send.js
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();
Running the Script
macOS / Linux
cd ~/whatsapp-bot
node contacts2025_send.js
Windows
cd C:\whatsapp-bot
node contacts2025_send.js
The first run will display a QR code in the terminal. Scan it via WhatsApp → Linked Devices → Link a Device. The session is saved locally with LocalAuth, so subsequent runs won’t require scanning again.
Customization
Include Groups
Remove or comment out the line:
if (chat.isGroup) continue;
Personalized Messages
Replace the static message with a template:
await client.sendMessage(contact.id, `🎉 Happy New Year, ${contact.name}! 🎉`);
Adjust Delays
Increase the timeout to respect WhatsApp rate limits:
await new Promise(r => setTimeout(r, 8000)); // 8‑second delay
Logging
You can append sent messages to a log file for tracking:
const fs = require('fs');
...
await client.sendMessage(contact.id, '🎉 Happy New Year! ��');
fs.appendFileSync('sent_log.txt', `${new Date().toISOString()} - Sent to ${contact.name}\n`);