Wileys
💝 Donation
Support the development of this project:
Solana Address:
8xN639anSq5q64793tseCjPaXNgXEPaKxr91CKEuggKd
Wileys 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
@lid to @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 wileys 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 wileys
Edge Version (Latest Features)
npm i wileys@latest
yarn add wileys@latest
Import in Code
const { default: makeWASocket } = require("wileys")
import makeWASocket from "wileys"
🚀 Quick Start
Example
Here is an example you can use: example.ts or follow this tutorial to run the Wileys WhatsApp API code:
-
cd path/to/wileys
-
npm install
-
node example.js
Basic Example
const { default: makeWASocket, DisconnectReason, useMultiFileAuthState } = require('wileys')
const { Boom } = require('@hapi/boom')
async function connectToWhatsApp() {
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
const sock = makeWASocket({
auth: state,
printQRInTerminal: true,
browser: ['Wileys', '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))
await sock.sendMessage(m.key.remoteJid!, {
text: 'Hello! I am a WhatsApp bot using Wileys 🤖'
})
}
})
sock.ev.on('creds.update', saveCreds)
}
connectToWhatsApp()
Index
Connecting Account
WhatsApp provides a multi-device API that allows wileys 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 Browsers constant. See available configurations here.
const { default: makeWASocket, Browsers } = require("wileys")
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("wileys")
const sock = makeWASocket({
printQRInTerminal: false
})
if (!sock.authState.creds.registered) {
const number = '6285134816783'
const code = await sock.requestPairingCode(number)
console.log('🔑 Pairing Code:', code)
}
if (!sock.authState.creds.registered) {
const pair = "YP240125"
const number = '6285134816783'
const code = await sock.requestPairingCode(number, pair)
console.log('🔑 Custom Pairing Code:', code)
}
Receive Full History
- Set
syncFullHistory to true.
- By default, Wileys uses Chrome browser config. For desktop-like connections (to receive more message history), use:
const { default: makeWASocket, Browsers } = require("wileys")
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("wileys")
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("wileys").default
const { useMultiFileAuthState } = require("wileys")
async function connect() {
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
const sock = makeWASocket({ auth: state })
sock.ev.on('creds.update', saveCreds)
}
connect()
[!IMPORTANT]
useMultiFileAuthState saves auth state in a folder. For production, use SQL/no-SQL databases and carefully manage key updates.
Handling Events
Wileys 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("wileys").default
const { DisconnectReason, useMultiFileAuthState } = require("wileys")
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
Fork from baileys modified by wileys
Wileys - Modern WhatsApp Web API with Fixed @lid To @pn