
Product
Introducing Pull Request Stories to Help Security Teams Track Supply Chain Risks
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
mongo-baileys
Advanced tools
✨ Elevate your WhatsApp bot's reliability with seamless MongoDB session persistence! ✨
mongo-baileys is a powerful Node.js library that empowers your Baileys-powered WhatsApp bots with the ability to store and retrieve session data in MongoDB. This ensures your bot stays connected even after restarts, making it more resilient and user-friendly.
npm install mongo-baileys
import { MongoClient } from 'mongodb';
import { makeWASocket, AnyMessageContent } from '@whiskeysockets/baileys';
import { useMongoDBAuthState } from 'mongo-baileys';
import Boom from '@hapi/boom';
const url = "YOUR_MONGODB_URL"; // Replace with your MongoDB connection string // When Obtaining Mongodb URL Choose NodeJS Driver Version 2 or Later but don't 3 or it higher
const dbName = "whatsapp";
const collectionName = "authState";
async function connectToMongoDB() {
const client = new MongoClient(url);
await client.connect();
const db = client.db(dbName);
const collection = db.collection(collectionName);
return { client, collection };
}
async function startWhatsApp() {
const { collection } = await connectToMongoDB();
const { state, saveCreds } = await useMongoDBAuthState(collection);
const sock = makeWASocket({
auth: state,
printQRInTerminal: true,
});
sock.ev.on('creds.update', saveCreds);
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update;
if (connection === 'close') {
const shouldReconnect = lastDisconnect && lastDisconnect.error
? Boom.boomify(lastDisconnect.error).output.statusCode
: 500;
console.log('Connection closed due to', lastDisconnect?.error, ', reconnecting in', shouldReconnect, 'ms');
if (shouldReconnect) {
setTimeout(() => startWhatsApp(), shouldReconnect);
}
} else if (connection === 'open') {
console.log('Opened connection');
}
});
sock.ev.on('messages.upsert', async (m) => {
console.log(JSON.stringify(m, null, 2));
const message = m.messages[0];
if (message && !message.key.fromMe && m.type === 'notify') {
console.log('Replying to', message.key.remoteJid);
await sock.sendMessage(message.key.remoteJid, { text: 'Hello there!' });
}
});
// Graceful shutdown
process.on('SIGINT', async () => {
console.log('Received SIGINT. Closing connection...');
await sock.close();
process.exit();
});
process.on('SIGTERM', async () => {
console.log('Received SIGTERM. Closing connection...');
await sock.close();
process.exit();
});
}
startWhatsApp().catch(err => console.error("Unexpected error:", err));
import { MongoClient, Collection, Document } from "mongodb";
import { makeWASocket, AnyMessageContent } from '@whiskeysockets/baileys';
import { useMongoDBAuthState } from 'mongo-baileys';
import * as Boom from '@hapi/boom';
import { AuthenticationCreds } from "@whiskeysockets/baileys";
const url = "YOUR_MONGODB_URL"; // When Obtaining Mongodb URL Choose NodeJS Driver Version 2 or Later but don't 3 or it higher
const dbName = "whatsapp";
const collectionName = "authState";
interface AuthDocument extends Document {
_id: string;
creds?: AuthenticationCreds;
}
async function connectToMongoDB() {
const client = new MongoClient(url);
await client.connect();
const db = client.db(dbName);
const collection = db.collection<AuthDocument>(collectionName);
return { client, collection };
}
async function startWhatsApp() {
const { collection } = await connectToMongoDB();
const { state, saveCreds } = await useMongoDBAuthState(collection);
const sock = makeWASocket({
auth: state,
printQRInTerminal: true,
});
sock.ev.on('creds.update', saveCreds);
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update;
if (connection === 'close') {
const shouldReconnect = lastDisconnect && lastDisconnect.error
? Boom.boomify(lastDisconnect.error).output.statusCode
: 500;
console.log('Connection closed due to', lastDisconnect?.error, ', reconnecting in', shouldReconnect, 'ms');
if (shouldReconnect) {
setTimeout(() => startWhatsApp(), shouldReconnect);
}
} else if (connection === 'open') {
console.log('Opened connection');
}
});
sock.ev.on('messages.upsert', async (m) => {
console.log(JSON.stringify(m, undefined, 2));
const message = m.messages[0];
if (message && !message.key.fromMe && m.type === 'notify') {
console.log('Replying to', message.key.remoteJid);
await sock.sendMessage(message.key.remoteJid!, { text: 'Hello there!' } as AnyMessageContent);
}
});
}
startWhatsApp().catch(err => console.log("Unexpected error:", err));
Contributions are welcome! Feel free to open issues and submit pull requests to enhance mongo-baileys
.
FAQs
Save your baileys session in mongodb using this package
We found that mongo-baileys demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.