@Dutakey/WhatsMulti is a powerful wrapper for @whiskeysockets/baileys, designed to efficiently manage multiple WhatsApp Web sessions. It offers advanced session handling, flexible event listeners, and various storage options, making it ideal for developers integrating WhatsApp into their applications.
Install via npm:
npm install @dutakey/whatsmulti
Or using yarn:
yarn add @dutakey/whatsmulti
- Seamless Multi-Session Management: Effortlessly create, manage, and maintain multiple WhatsApp sessions.
- Flexible Storage Options: Store sessions locally, in memory, or integrate with databases like MongoDB.
- Robust Event Handling: Easily handle events across multiple sessions for real-time monitoring.
- QR Code Generation: Automatically generate QR codes for session authentication.
- Message Sending: Send various types of messages (text, images, etc.) across sessions.
- Session Lifecycle Management: Start, stop, restart, and delete sessions programmatically.
- Quick Start
- Configuration
- Session Management
- Event Handling
- Message Operations
- Storage Options
- Contributing
- License
import WhatsMulti from '@dutakey/whatsmulti';
const client = new WhatsMulti({
mongoUri: 'mongodb://localhost:27017/whatsmulti-db', // Optional
defaultConnectionType: 'local', // 'local' | 'mongodb' | 'memory'
});
// Create and start a session
await client.createSession('my-session', 'local', {
printQR: true, // Print QR code to console
});
await client.startSession('my-session');
// Listen for events
client.on('open', (_, { sessionId }) => {
console.log(`Session ${sessionId} is connected!`);
});
client.on('messages.upsert', async (data, { sessionId }) => {
const message = data.messages[0];
console.log('New message:', message);
});
interface ConfigType {
defaultConnectionType?: 'local' | 'mongodb' | 'memory';
localConnectionPath?: string;
LoggerLevel?: 'silent' | 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
BaileysLoggerLevel?: 'silent' | 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
mongoUri?: string;
startWhenSessionCreated?: boolean;
}
interface SockConfig {
disableQRRetry?: boolean;
qrMaxWaitMs?: number;
printQR?: boolean;
// ... other Baileys SocketConfig options
}
Sessions can be created with different storage types:
// Local storage (files)
await client.createSession('session-1', 'local');
// MongoDB storage
await client.createSession('session-2', 'mongodb');
// Memory storage (temporary)
await client.createSession('session-3', 'memory');
// Create and start
await client.createSession('my-session', 'local', {
printQR: true,
qrMaxWaitMs: 60000,
});
await client.startSession('my-session');
// Stop temporarily
await client.stopSession('my-session');
// Restart
await client.restartSession('my-session');
// Logout and clean up
await client.logoutSession('my-session');
// Completely remove
await client.deleteSession('my-session');
// Load all previously created sessions
await client.loadSessions();
WhatsMulti extends EventEmitter and provides all Baileys events plus custom events:
// Connection events
client.on('open', (data, { sessionId, socket }) => {
console.log(`Session ${sessionId} connected`);
});
client.on('close', (data, { sessionId }) => {
console.log(`Session ${sessionId} disconnected`);
});
client.on('connecting', (data, { sessionId }) => {
console.log(`Session ${sessionId} is connecting...`);
});
// QR Code event
client.on('qr', (data, { sessionId }) => {
console.log(`QR Code for ${sessionId}:`, data.qr);
// data.image contains base64 image data
});
// Message events
client.on('messages.upsert', (data, { sessionId, socket }) => {
data.messages.forEach((message) => {
console.log(`New message in ${sessionId}:`, message);
});
});
// Contact events
client.on('contacts.update', (contacts, { sessionId }) => {
console.log(`Contacts updated in ${sessionId}:`, contacts);
});
// Group events
client.on('groups.update', (groups, { sessionId }) => {
console.log(`Groups updated in ${sessionId}:`, groups);
});
You can also use the process
method to handle all events in one place:
client.process((events, { sessionId, socket }) => {
if (events['messages.upsert']) {
// Handle messages
}
if (events.qr) {
// Handle QR code
}
// Handle other events...
});
Selain sendMessage
, WhatsMulti menyediakan helper method yang lebih sederhana untuk mengirim berbagai tipe pesan melalui client.message
.
await client.sendMessage('session-1', '[email protected]', { text: 'Hello World!' });
WhatsMulti expose client.message
untuk mempermudah pengiriman pesan.
await client.message.sendText('session-1', '[email protected]', 'Hello World!');
await client.message.sendQuote('session-1', msg, 'This is a reply', msg);
await client.message.sendMention('session-1', '[email protected]', 'Hello @user', [
'[email protected]',
]);
await client.message.forwardMessage('session-1', '[email protected]', msg);
await client.message.sendLocation('session-1', '[email protected]', -6.2, 106.816666);
const vcard = `BEGIN:VCARD
VERSION:3.0
FN:John Doe
TEL:+1234567890
END:VCARD`;
await client.message.sendContact('session-1', '[email protected]', 'John Doe', vcard);
await client.message.sendReaction('session-1', msg.key.remoteJid!, 'π', msg);
await client.message.sendPoll('session-1', '[email protected]', 'Favorite Fruit?', ['Apple', 'Banana'], 1);
await client.message.sendLinkPreview('session-1', '[email protected]', 'Check this out: https://example.com');
await client.message.sendImage(
'session-1',
'[email protected]',
'https://example.com/image.jpg',
'Nice image!'
);
await client.message.sendVideo(
'session-1',
'[email protected]',
'https://example.com/video.mp4',
'Watch this!',
true
);
await client.message.sendAudio('session-1', '[email protected]', 'https://example.com/audio.mp3', 'audio/mp3');
Sessions are stored as files in the local filesystem:
const client = new WhatsMulti({
localConnectionPath: './sessions', // Default: './whatsmulti_sessions'
});
await client.createSession('my-session', 'local');
Sessions are stored in a MongoDB database:
const client = new WhatsMulti({
mongoUri: 'mongodb://localhost:27017/whatsmulti-db',
});
await client.createSession('my-session', 'mongodb');
Sessions are stored in memory (lost on restart):
await client.createSession('my-session', 'memory');
Contributions are welcome! If you find a bug, have feature suggestions, or want to improve the project, feel free to open an issue or submit a pull request.
- Clone the repository
- Install dependencies:
npm install
- Build the project:
npm run build
- Run linting:
npm run lint
- Run the example:
npm run example
This project is licensed under MIT, allowing free use, modification, and distribution.