Loading...
Loading...
Expert guidance for whatsapp-web.js, a Puppeteer-based library providing high-level API to interact with WhatsApp Web. Use when building WhatsApp bots, automating WhatsApp messaging, integrating WhatsApp into applications, or working with WhatsApp Web automation, message handling, media sending, group management, or channel operations.
npx skill4agent add goncy/skills whatsapp-web-jsconst { Client, LocalAuth } = require('whatsapp-web.js');
const client = new Client({
authStrategy: new LocalAuth(),
puppeteer: { headless: true }
});
client.on('qr', (qr) => console.log('Scan QR:', qr));
client.on('ready', () => console.log('Client ready'));
client.on('message', async (msg) => {
if (msg.body === 'ping') await msg.reply('pong');
});
client.initialize(); // Non-blocking - listen for 'ready' eventpuppeteerffmpeg<country_code><phone>@c.us5511999999999@c.us<id>@g.us120363XXX@g.us<id>@newsletterstatus@broadcast+12345678901@c.us// No persistence - QR scan every restart
new NoAuth()
// Local filesystem - recommended for single-instance bots
new LocalAuth({ clientId: 'bot1' })
// Remote store - for cloud/multi-instance deployments
new RemoteAuth({ store: myStore, clientId: 'bot1', backupSyncIntervalMs: 60000 })const client = new Client({
authStrategy: new LocalAuth(),
pairWithPhoneNumber: {
phoneNumber: '5511999999999', // country code + number, no symbols
showNotification: true,
intervalMs: 180000
}
});
client.on('code', (code) => console.log('Pairing code:', code));// Text message
await client.sendMessage('5511999999999@c.us', 'Hello!');
// Reply to received message
await msg.reply('Got it!');
// With mentions
await client.sendMessage(chatId, 'Hi @5511999999999', {
mentions: ['5511999999999@c.us']
});
// Quote specific message
await client.sendMessage(chatId, 'Replying to this', {
quotedMessageId: msg.id._serialized
});const { MessageMedia } = require('whatsapp-web.js');
// From file
const media = MessageMedia.fromFilePath('/path/to/image.png');
await client.sendMessage(chatId, media, { caption: 'Check this out' });
// From URL
const media = await MessageMedia.fromUrl('https://example.com/image.png');
await client.sendMessage(chatId, media);
// Download received media
if (msg.hasMedia) {
const media = await msg.downloadMedia();
// Access: media.mimetype, media.data (base64), media.filename
}// As sticker (requires ffmpeg)
await client.sendMessage(chatId, media, { sendMediaAsSticker: true });
// As voice note
await client.sendMessage(chatId, audio, { sendAudioAsVoice: true });
// As HD quality
await client.sendMessage(chatId, media, { sendMediaAsHd: true });
// As view-once
await client.sendMessage(chatId, media, { isViewOnce: true });
// As document
await client.sendMessage(chatId, media, { sendMediaAsDocument: true });const { Poll, Location } = require('whatsapp-web.js');
// Poll (single choice)
await client.sendMessage(chatId, new Poll('Question?', ['A', 'B']));
// Poll (multiple choice)
await client.sendMessage(chatId, new Poll('Pick', ['A', 'B', 'C'],
{ allowMultipleAnswers: true }));
// React to message
await msg.react('👍'); // add reaction
await msg.react(''); // remove reaction
// Send location
await client.sendMessage(chatId,
new Location(37.422, -122.084, { name: 'Googleplex' }));// Edit sent message
const sent = await client.sendMessage(chatId, 'Original');
await sent.edit('Edited text');
// Delete message
await sent.delete(true); // true = delete for everyone
// Pin message
await msg.pin(86400); // Pin for 24h (86400|604800|2592000 seconds)
await msg.unpin();client.initialize()readymessagemessage_create._serializedmsg.id._serializedfetchMessages()chat.syncHistory()status@broadcast| Event | Use Case |
|---|---|
| Client ready to use |
| Incoming message only |
| All messages (including own) |
| Track delivery status (0=pending, 1=server, 2=device, 3=read, 4=played) |
| QR code for authentication |
| Auth successful |
| Handle reconnection logic |