zenzxz
v0.0.8
Published
WhatsApp Web API Library
Maintainers
Readme
zenzxz/baileys
💝 Donation
Support the development of this project:
Solana Address:
8xN639anSq5q64793tseCjPaXNgXEPaKxr91CKEuggKdzenzxz/baileys is a modern WebSocket-based TypeScript library for interacting with the WhatsApp Web API. This library has been enhanced to address issues with WhatsApp group @lid and @jid handling, ensuring robust performance.
✨ Key Features
- 🚀 Modern & Fast - Built with TypeScript and cutting-edge technologies
- 🔧 Fixed @lid & @jid - Resolved WhatsApp group
@lidto @pn` issues - 📱 Multi-Device Support - Supports WhatsApp multi-device connections
- 🔐 End-to-End Encryption - Fully encrypted communications
- 📨 All Message Types - Supports all message types (text, media, polls, etc.)
- 🎯 Easy to Use - Simple and intuitive API
⚠️ Disclaimer
This project is not affiliated, associated, authorized, endorsed by, or in any way officially connected with WhatsApp or any of its subsidiaries or affiliates. The official WhatsApp website can be found at whatsapp.com.
The maintainers of zenzxz/baileys do not condone the use of this application in practices that violate WhatsApp's Terms of Service. We urge users to exercise personal responsibility and use this application fairly and responsibly.
Use wisely. Avoid spamming. Refrain from excessive automated usage.
📦 Installation
Stable Version (Recommended)
npm i zenzxz/baileysEdge Version (Latest Features)
npm i zenzxz/baileys@latest
# or
yarn add zenzxz/baileys@latestImport in Code
const { default: makeWASocket } = require("zenzxz/baileys")
// or ES6
import makeWASocket from "zenzxz/baileys"🚀 Quick Start
Example
Here is an example you can use: example.ts or follow this tutorial to run the zenzxz/baileys WhatsApp API code:
cd path/to/zenzxz/baileysnpm installnode example.js
Basic Example
const { default: makeWASocket, DisconnectReason, useMultiFileAuthState } = require('zenzxz/baileys')
const { Boom } = require('@hapi/boom')
async function connectToWhatsApp() {
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
const sock = makeWASocket({
auth: state,
printQRInTerminal: true,
browser: ['zenzxz/baileys', 'Desktop', '3.0']
})
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if(connection === 'close') {
const shouldReconnect = (lastDisconnect?.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut
console.log('Connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
if(shouldReconnect) {
connectToWhatsApp()
}
} else if(connection === 'open') {
console.log('✅ Successfully connected to WhatsApp!')
}
})
sock.ev.on('messages.upsert', async ({ messages }) => {
for (const m of messages) {
if (!m.message) continue
console.log('📱 New message:', JSON.stringify(m, undefined, 2))
// Auto reply
await sock.sendMessage(m.key.remoteJid!, {
text: 'Hello! I am a WhatsApp bot using zenzxz/baileys 🤖'
})
}
})
sock.ev.on('creds.update', saveCreds)
}
connectToWhatsApp()Index
- Connecting Account
- Important Notes About Socket Config
- Save Auth Info
- Handling Events
- Implementing a Data Store
- WhatsApp IDs Explained
- Utility Functions
- Sending Messages
- Modify Messages
- Manipulating Media Messages
- Reject Call
- Send States in Chat
- Modifying Chats
- User Queries
- Change Profile
- Groups
- Privacy
- Broadcast Lists & Stories
- Writing Custom Functionality
- Tips & Best Practices
- Troubleshooting
- Contributing
- License
- Acknowledgments
Connecting Account
WhatsApp provides a multi-device API that allows zenzxz/baileys to authenticate as a secondary WhatsApp client via QR code or pairing code.
Starting Socket with QR Code
[!TIP] Customize the browser name using the
Browsersconstant. See available configurations here.
const { default: makeWASocket, Browsers } = require("zenzxz/baileys")
const sock = makeWASocket({
browser: Browsers.ubuntu('My App'),
printQRInTerminal: true
})Upon successful connection, a QR code will be printed in the terminal. Scan it with WhatsApp on your phone to log in.
Starting Socket with Pairing Code
[!IMPORTANT] Pairing codes are not part of the Mobile API. They allow connecting WhatsApp Web without a QR code, but only one device can be connected. See WhatsApp FAQ.
Phone numbers must exclude +, (), or -, and include the country code.
const { default: makeWASocket } = require("zenzxz/baileys")
const sock = makeWASocket({
printQRInTerminal: false
})
// Normal Pairing
if (!sock.authState.creds.registered) {
const number = '6285134816783'
const code = await sock.requestPairingCode(number)
console.log('🔑 Pairing Code:', code)
}
// Custom Pairing
if (!sock.authState.creds.registered) {
const pair = "YP240125" // 8 characters
const number = '6285134816783'
const code = await sock.requestPairingCode(number, pair)
console.log('🔑 Custom Pairing Code:', code)
}Receive Full History
- Set
syncFullHistorytotrue. - By default, zenzxz/baileys uses Chrome browser config. For desktop-like connections (to receive more message history), use:
const { default: makeWASocket, Browsers } = require("zenzxz/baileys")
const sock = makeWASocket({
browser: Browsers.macOS('Desktop'),
syncFullHistory: true
})Important Notes About Socket Config
Caching Group Metadata (Recommended)
For group usage, implement caching for group metadata:
const { default: makeWASocket } = require("zenzxz/baileys")
const NodeCache = require('node-cache')
const groupCache = new NodeCache({ stdTTL: 5 * 60, useClones: false })
const sock = makeWASocket({
cachedGroupMetadata: async (jid) => groupCache.get(jid)
})
sock.ev.on('groups.update', async ([event]) => {
const metadata = await sock.groupMetadata(event.id)
groupCache.set(event.id, metadata)
})
sock.ev.on('group-participants.update', async (event) => {
const metadata = await sock.groupMetadata(event.id)
groupCache.set(event.id, metadata)
})Improve Retry System & Decrypt Poll Votes
Enhance message sending and poll vote decryption with a store:
const sock = makeWASocket({
getMessage: async (key) => await getMessageFromStore(key)
})Receive Notifications in WhatsApp App
Disable online status to receive notifications:
const sock = makeWASocket({
markOnlineOnConnect: false
})Saving & Restoring Sessions
Avoid rescanning QR codes by saving credentials:
const makeWASocket = require("zenzxz/baileys").default
const { useMultiFileAuthState } = require("zenzxz/baileys")
async function connect() {
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
const sock = makeWASocket({ auth: state })
sock.ev.on('creds.update', saveCreds)
}
connect()[!IMPORTANT]
useMultiFileAuthStatesaves auth state in a folder. For production, use SQL/no-SQL databases and carefully manage key updates.
Handling Events
zenzxz/baileys uses EventEmitter for events, fully typed for IDE support.
[!IMPORTANT] See all events here.
const sock = makeWASocket()
sock.ev.on('messages.upsert', ({ messages }) => {
console.log('Got messages:', messages)
})Example to Start
const makeWASocket = require("zenzxz/baileys").default
const { DisconnectReason, useMultiFileAuthState } = require("zenzxz/baileys")
const { Boom } = require('@hapi/boom')
async function connectToWhatsApp() {
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
const sock = makeWASocket({
auth: state,
printQRInTerminal: true
})
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if(connection === 'close') {
const shouldReconnect = (lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut
console.log('Connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
if(shouldReconnect) {
connectToWhatsApp()
}
} else if(connection === 'open') {
console.log('Opened connection')
}
})
sock.ev.on('messages.upsert', async ({ messages }) => {
for (const m of messages) {
console.log(JSON.stringify(m, undefined, 2))
console.log('Replying to', m.key.remoteJid)
await sock.sendMessage(m.key.remoteJid!, { text: 'Hello World' })
}
})
sock.ev.on('creds.update', saveCreds)
}
connectToWhatsApp()License
Distributed under the GPL-3.0 License. See LICENSE for more information.
Acknowledgments
- Group - Comunity
Fork from baileys modified by zenzxz/baileys
zenzxz/baileys - Modern WhatsApp Web API with Fixed @lid To @pn
