Loading...
Loading...
Compare original and translation side by side
const { 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' eventpuppeteerffmpegconst { 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<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 })// 无持久化 - 每次重启都需要扫描QR码
new NoAuth()
// 本地文件系统 - 推荐用于单实例机器人
new LocalAuth({ clientId: 'bot1' })
// 远程存储 - 适用于云端/多实例部署
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));const client = new Client({
authStrategy: new LocalAuth(),
pairWithPhoneNumber: {
phoneNumber: '5511999999999', // 国家代码+号码,无特殊符号
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
});// 文本消息
await client.sendMessage('5511999999999@c.us', 'Hello!');
// 回复收到的消息
await msg.reply('Got it!');
// 提及用户
await client.sendMessage(chatId, 'Hi @5511999999999', {
mentions: ['5511999999999@c.us']
});
// 引用特定消息
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
}const { MessageMedia } = require('whatsapp-web.js');
// 从文件加载
const media = MessageMedia.fromFilePath('/path/to/image.png');
await client.sendMessage(chatId, media, { caption: 'Check this out' });
// 从URL加载
const media = await MessageMedia.fromUrl('https://example.com/image.png');
await client.sendMessage(chatId, media);
// 下载收到的媒体
if (msg.hasMedia) {
const media = await msg.downloadMedia();
// 可访问: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 });// 作为贴纸发送(需要ffmpeg)
await client.sendMessage(chatId, media, { sendMediaAsSticker: true });
// 作为语音笔记发送
await client.sendMessage(chatId, audio, { sendAudioAsVoice: true });
// 以高清质量发送
await client.sendMessage(chatId, media, { sendMediaAsHd: true });
// 作为一次性查看内容发送
await client.sendMessage(chatId, media, { isViewOnce: true });
// 作为文档发送
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' }));const { Poll, Location } = require('whatsapp-web.js');
// 投票(单选)
await client.sendMessage(chatId, new Poll('Question?', ['A', 'B']));
// 投票(多选)
await client.sendMessage(chatId, new Poll('Pick', ['A', 'B', 'C'],
{ allowMultipleAnswers: true }));
// 对消息添加反应
await msg.react('👍'); // 添加反应
await msg.react(''); // 移除反应
// 发送位置
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();// 编辑已发送的消息
const sent = await client.sendMessage(chatId, 'Original');
await sent.edit('Edited text');
// 删除消息
await sent.delete(true); // true = 为所有人删除
// 固定消息
await msg.pin(86400); // 固定24小时(可选时长:86400|604800|2592000秒)
await msg.unpin();client.initialize()readymessagemessage_create._serializedmsg.id._serializedfetchMessages()chat.syncHistory()status@broadcastclient.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 |
| 事件 | 使用场景 |
|---|---|
| 客户端已准备就绪可使用 |
| 仅针对 incoming 消息 |
| 所有消息(包括自己发送的) |
| 跟踪消息送达状态(0=待处理,1=已到服务器,2=已到设备,3=已读,4=已播放) |
| 用于认证的QR码 |
| 认证成功 |
| 处理重连逻辑 |