@rucas-code/baileys
v1.0.1
Published
A lightweight and clean wrapper around @itsliaaa/baileys for building WhatsApp bots with minimal code.
Downloads
3,385
Maintainers
Readme
✨ @rucas-code/baileys
A lightweight, clean, and developer-friendly wrapper around
@itsliaaa/baileysfor building WhatsApp bots with minimal boilerplate code.
✨ Features
- 🔑 Easy Pairing Code Authentication: Quick phone number login with automated session management.
- 📩 Simplified Message Listener: Easily extract sender details, chat IDs, message text, and media flags without digging into raw Baileys objects.
- 🔔 Group Participant Alerts: Track group joins, leaves, promotions, and demotions effortlessly.
- ⚙️ Dynamic Configuration: Customize CLI prompts and status messages at runtime.
📦 Installation
Install the package via npm:
npm install @rucas-code/baileys🚀 Quick Start
Here is a simple example to get your WhatsApp bot running in minutes:
const rucas = require('@rucas-code/baileys');
async function startBot() {
// 1. Establish WhatsApp connection
const { status, sock } = await rucas.wa.toConnect();
if (!status || !sock) {
console.error("Failed to connect to WhatsApp!");
return;
}
console.log("Bot connected successfully!");
// 2. Listen for incoming messages
rucas.wa.message(sock, async (data) => {
console.log(`[Message] From ${data.name} (${data.sender}): ${data.message}`);
if (data.message.toLowerCase() === 'ping') {
await sock.sendMessage(data.chatId, { text: 'pong 🏓' }, { quoted: data.msg });
}
});
// 3. Listen for group updates (join/leave/promote/demote)
rucas.wa.alert(sock, (update) => {
console.log(`[Group Update] JID: ${update.jid} | Action: ${update.action} | Members: ${update.participants.join(', ')}`);
});
}
startBot();📖 API Reference
1. rucas.wa.toConnect()
Establishes a connection to WhatsApp servers. If unauthenticated, prompts the user via terminal for a phone number to request a Pairing Code.
Parameters
None
Returns
Promise<Object>: Resolves to an object with connection status:status(Boolean):trueif connected successfully,falseotherwise.sock(WASocket | null): Active Baileys socket instance ornullif connection failed.
2. rucas.wa.message(sock, onMessageReceived)
Subscribes to incoming WhatsApp messages (messages.upsert) and parses common message attributes into a clean JavaScript object.
Parameters
| Parameter | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| sock | WASocket | Yes | Active Baileys socket instance returned by toConnect(). |
| onMessageReceived | Function | Yes | Callback function invoked whenever a new message is received. |
Callback Response Object Structure
The onMessageReceived callback receives a single object containing:
| Property | Type | Description |
| :--- | :--- | :--- |
| m | Object | Raw Baileys messages.upsert event payload. |
| msgType | String | Type of the message (e.g. conversation, extendedTextMessage, imageMessage). |
| msg | Object | Raw individual message object (useful for quoting replies). |
| id | String | Unique message ID. |
| sender | String | Sender JID (e.g., [email protected]). |
| name | String | Sender's WhatsApp push name (defaults to "WhatsApp User"). |
| message | String | Extracted text content or caption of the message. |
| chatId | String | Remote JID of the chat session. |
| hasMedia | Boolean | true if the message contains image, video, or document media. |
| timestamp | Date | Date object representing when the message event occurred. |
3. rucas.wa.alert(sock, onGroupUpdateReceived)
Subscribes to group participant updates (group-participants.update).
Parameters
| Parameter | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| sock | WASocket | Yes | Active Baileys socket instance returned by toConnect(). |
| onGroupUpdateReceived | Function | Yes | Callback function invoked on group member changes. |
Callback Response Object Structure
The onGroupUpdateReceived callback receives an object containing:
| Property | Type | Description |
| :--- | :--- | :--- |
| jid | String | Group Chat JID (e.g. [email protected]). |
| participants | Array<String> | List of participant JIDs affected by the update. |
| action | String | Update type: "add", "remove", "promote", or "demote". |
| sock | WASocket | Active socket instance. |
| rawUpdate | Object | Raw Baileys group update payload. |
4. rucas.configures(key, value)
Modifies default CLI log messages and prompt strings stored in config.json.
Parameters
| Parameter | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| key | String | Yes | Configuration key to update. |
| value | String | Yes | New string value to set. |
Available Configuration Keys
"start_connect"- Initial connection start prompt."enter_number"- Phone number entry instruction."question"- Input prompt for phone number."pairing_code"- Pairing code display label."try_connect"- Connection attempt log message."broker_connection"- Connection broken warning message."success_connect"- Connection success indicator.
Returns
Object: The updated configuration object.
🧪 Testing
Run the built-in test suite to verify all modules and functions:
npm test📝 License
This project is licensed under the ISC License.
