Auto-Reply & Chat Control
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Auto-Reply & Chat Control system in the Baileys project provides a comprehensive framework for managing automated interactions and fine-grained chat modifications. The system consists of an AutoReplyHandler for pattern-based responses and a suite of "Chat Control" methods integrated into the socket layer for modifying chat states, privacy settings, and metadata.
This module allows developers to define complex response rules, manage per-user cooldowns, and simulate human behavior such as typing indicators. Simultaneously, the Chat Control features enable structural changes to the WhatsApp environment, including archiving, pinning, blocking, and privacy adjustments.
Auto-Reply System Architecture
The Auto-Reply system is centered around the AutoReplyHandler class, which manages a collection of rules and processes incoming messages to determine if a response should be triggered.
Auto-Reply Components
The system uses three primary data structures to manage logic:
- Rules Map: Stores
AutoReplyRuleobjects indexed by a unique ID. - Cooldowns Map: Tracks per-rule, per-JID cooldown expirations.
- Global Cooldown Map: Tracks the last interaction time for each JID to prevent spam.
Sources: [lib/Utils/auto-reply.js:20-25](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Utils/auto-reply.js#L20-L25)
Processing Logic
When a message is received, the handler extracts text from various message types (conversation, extended text, image/video/document captions) and evaluates it against active rules sorted by priority.
The diagram shows the logical flow of the processMessage function, including matching, filtering, and optional typing simulation.
Sources: [lib/Utils/auto-reply.js:132-192](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Utils/auto-reply.js#L132-L192)
Auto-Reply Configuration
The AutoReplyOptions interface defines the behavior of the handler.
| Option | Type | Default | Description |
|---|---|---|---|
globalCooldown | number | 1000 | Min time in ms between any two replies to the same JID. |
simulateTyping | boolean | false | Whether to show "typing..." before sending a reply. |
typingDuration | number | 1000 | Duration to show the typing indicator. |
multiMatch | boolean | false | If true, processes all matching rules instead of just the first. |
Sources: [lib/Utils/auto-reply.js:28-35](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Utils/auto-reply.js#L28-L35), [lib/Utils/auto-reply.d.ts:34-47](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Utils/auto-reply.d.ts#L34-L47)
Chat Control & Management
Chat Control refers to a set of functions exported by makeChatsSocket that interact directly with the WhatsApp protocol to modify chat properties or account settings.
Privacy and Presence Management
Baileys supports updating various privacy categories and sending presence updates to specific JIDs.
- Privacy IQ Queries: Methods like
updateLastSeenPrivacy,updateOnlinePrivacy, andupdateReadReceiptsPrivacysendiqnodes withxmlns="privacy"tos.whatsapp.net. - Presence Updates:
sendPresenceUpdateallows switching betweenavailable,unavailable,composing(typing), andrecording(audio) states.
Sources: [lib/Socket/chats.js:68-124](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Socket/chats.js#L68-L124), [lib/Socket/chats.js:501-546](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Socket/chats.js#L501-L546)
Chat Modification (App State Sync)
Modifying a chat (e.g., pinning, archiving, marking as read) is performed via the chatModify function, which wraps modifications into "App Patches" for synchronization across devices.
This diagram illustrates how a chat modification request is transformed into a patch and synchronized.
Sources: [lib/Socket/chats.js:633-640](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Socket/chats.js#L633-L640), [lib/Socket/chats.js:578-620](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Socket/chats.js#L578-L620)
Key Chat Control Functions
| Function | Description | Source |
|---|---|---|
star | Stars or unstars specific messages in a chat. | [lib/Socket/chats.js:655-663](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Socket/chats.js#L655-L663) |
updateBlockStatus | Blocks or unblocks a specific JID. | [lib/Socket/chats.js:338-356](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Socket/chats.js#L338-L356) |
chatModify | General method for archiving, pinning, and read status. | [lib/Socket/chats.js:637-640](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Socket/chats.js#L637-L640) |
clearMessage | Deletes/clears messages in a chat. | [lib/Socket/chats.js:744-752](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Socket/chats.js#L744-L752) |
addLabel / removeChatLabel | Manages WhatsApp Business labels for chats or messages. | [lib/Socket/chats.js:688-740](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Socket/chats.js#L688-L740) |
Template Management
The system includes a TemplateManager that allows defining reusable message structures with variables.
Template Logic
Templates use a double-curly brace syntax {{variable:defaultValue}}. The TemplateManager handles extraction of variables and rendering using provided data objects.
// Example of rendering a template
const content = "Hi {{name}}, welcome to {{company:our store}}!";
const data = { name: "John" };
// Result: "Hi John, welcome to our store!"
Sources: [lib/Utils/templates.js:106-116](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Utils/templates.js#L106-L116)
Preset Templates
The project provides several preset templates for common business scenarios:
ORDER_CONFIRMATION: IncludesorderId,customerName,total.APPOINTMENT: Includesservice,date,time.SUPPORT_TICKET: IncludesticketId,subject.
Sources: [lib/Utils/templates.js:142-205](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Utils/templates.js#L142-L205)
Implementation Example
The following snippet demonstrates how to integrate the AutoReplyHandler with the main socket connection.
const autoReply = createAutoReply(
(jid, content, opts) => sock.sendMessage(jid, content, opts),
(jid, presence) => sock.sendPresenceUpdate(presence, jid),
{ simulateTyping: true, typingDuration: 1500 }
);
// Add a simple rule
autoReply.addRule({
keywords: ['ping'],
response: { text: 'pong!' },
quoted: true
});
// Process messages in the upsert event
sock.ev.on('messages.upsert', async ({ messages }) => {
for (const msg of messages) {
if (!msg.key.fromMe) {
await autoReply.processMessage(msg);
}
}
});
Sources: [lib/Utils/auto-reply.js:218-245](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/lib/Utils/auto-reply.js#L218-L245), [assets/examples/example.js:143-155](https://github.com/innovatorssoft/Baileys/blob/000e5a14951e89b9ac56d4e08bef5524b8299f45/assets/examples/example.js#L143-L155)
Summary
The Auto-Reply & Chat Control features provide a dual-layered approach to automation. The AutoReplyHandler offers a high-level API for interactive bot behavior, while the chat control methods in makeChatsSocket provide low-level protocol access for managing the WhatsApp environment. Together, they enable robust automation and management of WhatsApp accounts using the Baileys library.