Notice: Redis Adapter has been added and is now the default adapter in the examples below.
Connecting Your app with Whatsapp Messaging
Lightweight library for whatsapp. Not require Selenium or any other browser.
Stand above Baileys Library.
Install package using npm
npm install wa-multi-session@latestThen import your code
Using JS Module
import { Whatsapp, RedisAdapter } from "wa-multi-session";or using CommonJS
const { Whatsapp, RedisAdapter } = require("wa-multi-session");Create new Whatsapp Instance with Redis Adapter
Or you can use SQLite Adapter by replacing
RedisAdapterwithSQLiteAdapter
const whatsapp = new Whatsapp({
adapter: new RedisAdapter({
url: "redis://localhost:6379",
}),
// Optional: Add Listener/Callback
onConnecting: (sessionId) => {
console.log(`[${sessionId}] Connecting...`);
},
onConnected: (sessionId) => {
console.log(`[${sessionId}] Connected`);
},
onDisconnected: (sessionId) => {
console.log(`[${sessionId}] Disconnected`);
},
});Start New Session with QR Code
// create session with ID : session1
const session = await whatsapp.startSession("session1", {
printQR: true, // print QR on terminal
});
// Then, scan QR on terminalor Start Session with Pairing Code (Phone Number) (Experimental)
const session = await whatsapp.startSessionWithPairingCode("mysessionid", {
phoneNumber: "6281234567890",
onPairingCode(code) {
console.log(`Pairing Code: ${code}`);
},
});Get All Session ID
const sessions = await whatsapp.getSessionsIds();
// returning all session ID that has been createdGet Session Data By ID
const session = await whatsapp.getSessionById("session1");
// returning session dataSend Text Message
await whatsapp.sendText({
sessionId: "session1", // session ID
to: "6281234567890",
text: "Hi There, This is Message from Server!",
});Send Image
const image = fs.readFileSync("./myimage.png"); // Buffer
const send = await whatsapp.sendImage({
sessionId: "session1",
to: "6281234567890",
text: "My Image Caption",
media: image, // can from URL too
});Send Video
const video = fs.readFileSync("./myvideo.mp4"); // Buffer
const send = await whatsapp.sendVideo({
sessionId: "session1",
to: "6281234567890",
text: "My Video Caption",
media: video, // can from URL too
});Send Document File
const filename = "mydocument.docx";
const document = fs.readFileSync(filename); // Buffer
const send = await whatsapp.sendDocument({
sessionId: "session1",
to: "6281234567890",
filename: filename,
media: document,
text: "Hei, Check this Document",
});Send Voice Note
const filename = "myaudio.mp3";
const audio = fs.readFileSync(filename); // Buffer
const send = await whatsapp.sendAudio({
sessionId: "session1",
to: "6281234567890",
media: audio,
asVoiceNote: true, // send as voice note (ptt)
});Read a Message
await whatsapp.readMessage({
sessionId: "session1",
key: msg.key,
});Send Typing Effect
await whatsapp.sendTypingIndicator({
sessionId: "session1",
to: "6281234567890",
duration: 3000,
});Send Poll
await whatsapp.sendPoll({
sessionId: "session1",
to: "6281234567890",
poll: {
name: "Your favorite programming language?",
values: ["JavaScript", "Python", "Go", "Rust"],
selectableCount: 1, // number of values can be selected
},
});const whatsapp = new Whatsapp({
adapter: new RedisAdapter({
url: "redis://localhost:6379",
}),
onMessageReceived: async (msg) => {
if (msg.key.fromMe || msg.key.remoteJid?.includes("status")) return;
const sender = msg.key.participant || msg.key.remoteJid!;
await whatsapp.readMessage({
sessionId: msg.sessionId,
key: msg.key,
});
await whatsapp.sendTypingIndicator({
sessionId: msg.sessionId,
to: sender,
duration: 3000,
});
await whatsapp.sendText({
sessionId: msg.sessionId,
to: sender,
text: `You said: ${msg.message?.conversation || ""}`,
answering: msg, // for quoting message
});
},
});const whatsapp = new Whatsapp({
adapter: new RedisAdapter({
url: "redis://localhost:6379",
}),
onMessageReceived: async (msg) => {
if (msg.message?.imageMessage) {
// save image
msg.saveImage("./myimage.jpg");
}
if (msg.message?.videoMessage) {
// save video
msg.saveVideo("./myvideo.mp4");
}
if (msg.message?.documentMessage) {
// save document
msg.saveDocument("./mydocument"); // without extension
}
},
});Set custom credentials directory
// use adapter to set custom directory
const adapter = new SQLiteAdapter({
databasePath: "my_custom_dir/database.db",
});Or use Redis adapter (default) with custom key prefix
const adapter = new RedisAdapter({
url: "redis://localhost:6379",
keyPrefix: "my_app:",
});Redis is the default adapter in examples. You can still use SQLite by replacing
RedisAdapter with SQLiteAdapter in the snippets above.
This is implementation of Whatsapp Multi Session that can be used as headless whatsapp gateway API, you can self host it and connect with your app using HTTP API or Websocket API.
If you have any feedback or support, please reach out to me at mimamch28@gmail.com
- Added Redis adapter (default in examples).