@chatunity/baileytest
v1.4.2
Published
whatsapp api multidevice by ChatUnity
Maintainers
Readme
ChatUnity Baileys
TypeScript & Node.js WhatsApp Web API
Lightweight WhatsApp Web API • No Selenium • WebSocket-based • Multi-device Support
Based on @itsukichann's original work
🎯 Why Baileys?
Baileys connects to WhatsApp Web using WebSockets instead of Selenium/Chromium, saving ~500MB of RAM [web:8]. It supports both multi-device and web versions of WhatsApp with full TypeScript support.
[!IMPORTANT]
The original repository was removed by the main author. Development continues officially in this community-maintained version.
📦 Installation
# Stable version
yarn add @chatunitycenter/baileys
# Edge version (latest features)
yarn add github:chatunitycenter/baileysimport makeWASocket from '@chatunitycenter/baileys'🚀 Quick Start
import makeWASocket, { useMultiFileAuthState, Browsers } from '@chatunitycenter/baileys'
const { state, saveCreds } = await useMultiFileAuthState('./auth_info_baileys')
const sock = makeWASocket({
auth: state,
browser: Browsers.ubuntu('ChatUnity'),
printQRInTerminal: true
})
sock.ev.on('creds.update', saveCreds)[!NOTE] Phone number format: country code + number (e.g.,
393123456789)
import makeWASocket from '@chatunitycenter/baileys'
const sock = makeWASocket({ printQRInTerminal: false })
if (!sock.authState.creds.registered) {
const number = '393123456789' // Your number
const code = await sock.requestPairingCode(number)
console.log(`Pairing code: ${code}`)
}const sock = makeWASocket({
browser: Browsers.macOS('Desktop'),
syncFullHistory: true
})⚙️ Configuration
import makeWASocket, { useMultiFileAuthState } from '@chatunitycenter/baileys'
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
const sock = makeWASocket({ auth: state })
sock.ev.on('creds.update', saveCreds)Alternative storage methods:
useSingleFileAuthState- Single JSON fileuseMongoFileAuthState- MongoDB storage
import NodeCache from '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)
})const sock = makeWASocket({
markOnlineOnConnect: false
})🎪 Event Handling
import { Boom } from '@hapi/boom'
import makeWASocket, { DisconnectReason } from '@chatunitycenter/baileys'
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
if(shouldReconnect) connectToWhatsApp()
} else if(connection === 'open') {
console.log('Connected!')
}
})
sock.ev.on('messages.upsert', async ({ messages }) => {
for (const m of messages) {
console.log('New message:', m.key.remoteJid)
await sock.sendMessage(m.key.remoteJid!, { text: 'Hello World!' })
}
})
sock.ev.on('creds.update', saveCreds)
}
connectToWhatsApp()View all events: BaileysEventMap Documentation
import { makeInMemoryStore, getAggregateVotesInPollMessage } from '@chatunitycenter/baileys'
const store = makeInMemoryStore({})
sock.ev.on("messages.update", async (updates) => {
for(const { key, update } of updates) {
if(update.pollUpdates && key.fromMe) {
const pollCreation = await store.loadMessage(key.remoteJid, key.id)
if(pollCreation) {
const pollUpdate = await getAggregateVotesInPollMessage({
message: pollCreation.message,
pollUpdates: update.pollUpdates,
})
console.log('Poll votes:', pollUpdate)
}
}
}
})💬 Sending Messages
// Simple text
await sock.sendMessage(jid, { text: 'Hello World' })
// With quote
await sock.sendMessage(jid, { text: 'Reply' }, { quoted: message })
// Mention users
await sock.sendMessage(jid, {
text: '@393123456789',
mentions: ['[email protected]']
})
// Reaction
await sock.sendMessage(jid, {
react: { text: '💖', key: message.key }
})
// Location
await sock.sendMessage(jid, {
location: {
degreesLatitude: 24.121231,
degreesLongitude: 55.1121221
}
})// Image
await sock.sendMessage(jid, {
image: { url: './image.png' },
caption: 'Beautiful image'
})
// Video
await sock.sendMessage(jid, {
video: { url: './video.mp4' },
caption: 'Cool video'
})
// Audio (convert to OGG first)
await sock.sendMessage(jid, {
audio: { url: './audio.ogg' },
mimetype: 'audio/ogg; codecs=opus'
})
// GIF
await sock.sendMessage(jid, {
video: fs.readFileSync('gif.mp4'),
gifPlayback: true
})
// Album (multiple images/videos)
await sock.sendMessage(jid, {
album: [
{ image: { url: 'img1.jpg' }, caption: 'Photo 1' },
{ video: { url: 'vid1.mp4' }, caption: 'Video 1' }
]
})// Poll
await sock.sendMessage(jid, {
poll: {
name: 'Favorite color?',
values: ['Red', 'Blue', 'Green'],
selectableCount: 1
}
})
// Buttons
await sock.sendMessage(jid, {
text: 'Choose an option',
footer: 'Powered by ChatUnity',
buttons: [
{ buttonId: 'id1', buttonText: { displayText: 'Option 1' } },
{ buttonId: 'id2', buttonText: { displayText: 'Option 2' } }
]
})
// List
await sock.sendMessage(jid, {
text: 'Select from menu',
buttonText: 'View Menu',
sections: [{
title: 'Section 1',
rows: [
{ title: 'Option 1', rowId: 'opt1' },
{ title: 'Option 2', rowId: 'opt2', description: 'Description' }
]
}]
})// Contact card
const vcard = 'BEGIN:VCARD\nVERSION:3.0\nFN:John Doe\nTEL;type=CELL:+393123456789\nEND:VCARD'
await sock.sendMessage(jid, {
contacts: {
displayName: 'John',
contacts: [{ vcard }]
}
})
// Forward
await sock.sendMessage(jid, { forward: message, force: true })
// Delete message
await sock.sendMessage(jid, { delete: message.key })
// Edit message
await sock.sendMessage(jid, { edit: message.key, text: 'Edited text' })🔧 Utility Functions
Message Helpers:
getContentType(message)- Get message typedownloadMediaMessage(message)- Download media contentgetDevice(message)- Get sender's device info
Full documentation: Baileys Types
📋 WhatsApp ID Format
- Personal:
[country_code][number]@s.whatsapp.net(e.g.,[email protected]) - Groups:
[group_id]@g.us - Channels:
[channel_id]@newsletter - Broadcast:
status@broadcast
📚 Additional Resources
- Socket Configuration
- Event Handling
- Message Types
- Send Options
- Working with Media
- Group Management
- Privacy Settings
- Profile Management
- Broadcast Lists & Stories
⚖️ License & Responsibility
Licensed under MIT License. The developers cannot be held responsible for misuse. Please respect WhatsApp's Terms of Service [web:8].
Credits: Based on the original Baileys library by @itsukichann
Maintained by: ChatUnity Center
Made with ❤️ by the community
