π Node.jsμ ν¨κ»νλ WhatsApp λ©μμ§ μλνλ‘ μν΄ μΈμ¬ π
Source: Dev.to
κ°μ
Node.jsμ whatsapp-web.jsλ₯Ό μ¬μ©νμ¬ WhatsAppμμ μν΄ μΈμ¬ λ©μμ§λ₯Ό μλμΌλ‘ μ μ‘ν©λλ€.
μλνλ μ μ μΌλ‘ μ»΄ν¨ν°μμ μ€νλλ©°, WhatsApp κ³μ μ λΉκ³΅κ°λ‘ μ μ§νκ³ μ격 μ¦λͺ
μ μμ νκ² λ³΄νΈν©λλ€.
μ¬μ μꡬ μ¬ν
- Node.js v18 LTS μ΄μ
- (μ ν μ¬ν) macOSμμ Node.jsλ₯Ό μ½κ² μ€μΉνκΈ° μν Homebrew
μ€μΉ
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
μ€μΉ νμΈ:
node -v
npm -v
Windows / μλ μ€μΉ
Node.js LTS (v18 μ΄μ)λ₯Ό λ€μ΄λ‘λνκ³ βAdd to PATHβ μ΅μ μ 체ν¬ν©λλ€.
node -v
npm -v
νλ‘μ νΈ μ€μ
νλ‘μ νΈ ν΄λλ₯Ό λ§λ€κ³ νμν ν¨ν€μ§λ₯Ό μ€μΉν©λλ€.
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
μ€ν¬λ¦½νΈ: contacts2025_send.js
νλ‘μ νΈ ν΄λμ contacts2025_send.js νμΌμ λ§λ€κ³ μλ μ½λλ₯Ό λΆμ¬λ£μ΅λλ€:
// 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();
μ€ν¬λ¦½νΈ μ€ν
macOS / Linux
cd ~/whatsapp-bot
node contacts2025_send.js
Windows
cd C:\whatsapp-bot
node contacts2025_send.js
첫 μ€ν μ ν°λ―Έλμ QR μ½λκ° νμλ©λλ€. WhatsApp β Linked Devices β Link a Deviceμμ μ€μΊνμΈμ. μΈμ
μ LocalAuthλ‘ λ‘컬μ μ μ₯λλ―λ‘ μ΄ν μ€νμμλ λ€μ μ€μΊν νμκ° μμ΅λλ€.
컀μ€ν°λ§μ΄μ§
κ·Έλ£Ή ν¬ν¨
λ€μ λΌμΈμ μ κ±°νκ±°λ μ£Όμ μ²λ¦¬ν©λλ€:
if (chat.isGroup) continue;
κ°μΈν λ©μμ§
μ μ λ©μμ§λ₯Ό ν νλ¦ΏμΌλ‘ κ΅μ²΄ν©λλ€:
await client.sendMessage(contact.id, `π Happy New Year, ${contact.name}! π`);
μ§μ° μκ° μ‘°μ
WhatsApp μλ μ νμ κ³ λ €νμ¬ νμμμμ λ립λλ€:
await new Promise(r => setTimeout(r, 8000)); // 8βsecond delay
λ‘κΉ
λ³΄λΈ λ©μμ§λ₯Ό λ‘κ·Έ νμΌμ μΆκ°νμ¬ μΆμ ν μ μμ΅λλ€:
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`);