2025-12-23 03:00:27 +01:00
---
summary: "WhatsApp (web provider) integration: login, inbox, replies, media, and ops"
read_when:
- Working on WhatsApp/web provider behavior or inbox routing
---
# WhatsApp (web provider)
Updated: 2025-12-23
2026-01-06 18:25:52 +00:00
Status: WhatsApp Web via Baileys only. Gateway owns the session(s).
2025-12-23 03:00:27 +01:00
## Goals
2026-01-06 18:25:52 +00:00
- Multiple WhatsApp accounts (multi-account) in one Gateway process.
2025-12-23 03:00:27 +01:00
- Deterministic routing: replies return to WhatsApp, no model routing.
- Model sees enough context to understand quoted replies.
## Architecture (who owns what)
- **Gateway** owns the Baileys socket and inbox loop.
- **CLI / macOS app** talk to the gateway; no direct Baileys use.
- **Active listener** is required for outbound sends; otherwise send fails fast.
2026-01-02 19:58:44 +00:00
## Getting a phone number
WhatsApp requires a real mobile number for verification. VoIP and virtual numbers are usually blocked.
**Recommended approaches:**
- **Local eSIM** from your country's mobile carrier (most reliable)
- Austria: [hot.at ](https://www.hot.at )
- UK: [giffgaff ](https://www.giffgaff.com ) — free SIM, no contract
- **Prepaid SIM** — cheap, just needs to receive one SMS for verification
**Avoid:** TextNow, Google Voice, most "free SMS" services — WhatsApp blocks these aggressively.
**Tip:** The number only needs to receive one verification SMS. After that, WhatsApp Web sessions persist via `creds.json` .
2026-01-04 23:17:26 +00:00
**WhatsApp Business:** You can use WhatsApp Business on the same phone with a different number. This is a great option if you want to keep your personal WhatsApp separate — just install WhatsApp Business and register it with Clawdbot's dedicated number.
2025-12-23 03:00:27 +01:00
## Login + credentials
2026-01-04 14:32:47 +00:00
- Login command: `clawdbot login` (QR via Linked Devices).
2026-01-06 18:25:52 +00:00
- Multi-account login: `clawdbot login --account <id>` (`<id>` = `accountId` ).
- Default account (when `--account` is omitted): `default` if present, otherwise the first configured account id (sorted).
- Credentials stored in `~/.clawdbot/credentials/whatsapp/<accountId>/creds.json` .
2025-12-23 03:00:27 +01:00
- Backup copy at `creds.json.bak` (restored on corruption).
2026-01-06 18:25:52 +00:00
- Legacy compatibility: older installs stored Baileys files directly in `~/.clawdbot/credentials/` .
- Logout: `clawdbot logout` (or `--account <id>` ) deletes WhatsApp auth state (but keeps shared `oauth.json` ).
2025-12-23 03:00:27 +01:00
- Logged-out socket => error instructs re-link.
## Inbound flow (DM + group)
- WhatsApp events come from `messages.upsert` (Baileys).
2025-12-26 09:37:38 +00:00
- Inbox listeners are detached on shutdown to avoid accumulating event handlers in tests/restarts.
2025-12-23 03:00:27 +01:00
- Status/broadcast chats are ignored.
- Direct chats use E.164; groups use group JID.
2026-01-06 17:51:38 +01:00
- **DM policy**: `whatsapp.dmPolicy` controls direct chat access (default: `pairing` ).
- Pairing: unknown senders get a pairing code (approve via `clawdbot pairing approve --provider whatsapp <code>` ).
- Open: requires `whatsapp.allowFrom` to include `"*"` .
- Self messages are always allowed; “self-chat mode” still requires `whatsapp.allowFrom` to include your own number.
2026-01-06 06:40:42 +00:00
- **Group policy**: `whatsapp.groupPolicy` controls group handling (`open|disabled|allowlist` ).
- `allowlist` uses `whatsapp.groupAllowFrom` (fallback: explicit `whatsapp.allowFrom` ).
2025-12-23 03:00:27 +01:00
- **Self-chat mode**: avoids auto read receipts and ignores mention JIDs.
- Read receipts sent for non-self-chat DMs.
## Message normalization (what the model sees)
- `Body` is the current message body with envelope.
- Quoted reply context is **always appended** :
```
2026-01-02 23:18:41 +01:00
[Replying to +1555 id:ABC123]
2025-12-23 03:00:27 +01:00
< quoted text or < media: . . . > >
[/Replying]
```
- Reply metadata also set:
- `ReplyToId` = stanzaId
- `ReplyToBody` = quoted body or media placeholder
- `ReplyToSender` = E.164 when known
- Media-only inbound messages use placeholders:
- `<media:image|video|audio|document|sticker>`
## Groups
2026-01-06 18:25:52 +00:00
- Groups map to `agent:<agentId>:whatsapp:group:<jid>` sessions.
2026-01-06 06:40:42 +00:00
- Group policy: `whatsapp.groupPolicy = open|disabled|allowlist` (default `open` ).
2025-12-23 03:00:27 +01:00
- Activation modes:
- `mention` (default): requires @mention or regex match.
- `always` : always triggers.
- `/activation mention|always` is owner-only.
2026-01-02 12:59:47 +01:00
- Owner = `whatsapp.allowFrom` (or self E.164 if unset).
2025-12-23 03:00:27 +01:00
- **History injection**:
- Recent messages (default 50) inserted under:
`[Chat messages since your last reply - for context]`
- Current message under:
`[Current message - respond to this]`
- Sender suffix appended: `[from: Name (+E164)]`
- Group metadata cached 5 min (subject + participants).
## Reply delivery (threading)
2026-01-02 23:18:41 +01:00
- WhatsApp Web sends standard messages (no quoted reply threading in the current gateway).
2026-01-06 18:25:52 +00:00
- Reply tags are ignored on this provider.
2025-12-23 03:00:27 +01:00
## Outbound send (text + media)
- Uses active web listener; error if gateway not running.
- Text chunking: 4k max per message.
- Media:
- Image/video/audio/document supported.
- Audio sent as PTT; `audio/ogg` => `audio/ogg; codecs=opus` .
- Caption only on first media item.
- Media fetch supports HTTP(S) and local paths.
2026-01-03 23:56:36 +00:00
- Animated GIFs: WhatsApp expects MP4 with `gifPlayback: true` for inline looping.
2026-01-04 14:32:47 +00:00
- CLI: `clawdbot send --media <mp4> --gif-playback`
2026-01-03 23:56:36 +00:00
- Gateway: `send` params include `gifPlayback: true`
2025-12-23 03:00:27 +01:00
## Media limits + optimization
- Default cap: 5 MB (per media item).
2025-12-23 23:45:20 +00:00
- Override: `agent.mediaMaxMb` .
2025-12-23 03:00:27 +01:00
- Images are auto-optimized to JPEG under cap (resize + quality sweep).
- Oversize media => error; media reply falls back to text warning.
## Heartbeats
- **Gateway heartbeat** logs connection health (`web.heartbeatSeconds` , default 60s).
2025-12-26 02:35:21 +01:00
- **Agent heartbeat** is global (`agent.heartbeat.*` ) and runs in the main session.
- Uses `HEARTBEAT` prompt + `HEARTBEAT_OK` skip behavior.
2026-01-06 18:25:52 +00:00
- Delivery defaults to the last used provider (or configured target).
2025-12-23 03:00:27 +01:00
## Reconnect behavior
- Backoff policy: `web.reconnect` :
- `initialMs` , `maxMs` , `factor` , `jitter` , `maxAttempts` .
- If maxAttempts reached, web monitoring stops (degraded).
- Logged-out => stop and require re-link.
## Config quick map
2026-01-06 17:51:38 +01:00
- `whatsapp.dmPolicy` (DM policy: pairing/allowlist/open/disabled).
2026-01-02 12:59:47 +01:00
- `whatsapp.allowFrom` (DM allowlist).
2026-01-06 18:25:52 +00:00
- `whatsapp.accounts.<accountId>.*` (per-account settings + optional `authDir` ).
2026-01-06 06:40:42 +00:00
- `whatsapp.groupAllowFrom` (group sender allowlist).
- `whatsapp.groupPolicy` (group policy).
2026-01-06 03:30:33 +01:00
- `whatsapp.groups` (group allowlist + mention gating defaults; use `"*"` to allow all)
2025-12-24 00:22:57 +00:00
- `routing.groupChat.mentionPatterns`
- `routing.groupChat.historyLimit`
- `messages.messagePrefix` (inbound prefix)
- `messages.responsePrefix` (outbound prefix)
2025-12-23 23:45:20 +00:00
- `agent.mediaMaxMb`
2025-12-26 01:13:13 +01:00
- `agent.heartbeat.every`
- `agent.heartbeat.model` (optional override)
2025-12-26 02:35:21 +01:00
- `agent.heartbeat.target`
- `agent.heartbeat.to`
2026-01-06 18:25:52 +00:00
- `session.*` (scope, idle, store, mainKey)
2025-12-26 16:54:53 +00:00
- `web.enabled` (disable provider startup when false)
2025-12-23 03:00:27 +01:00
- `web.heartbeatSeconds`
- `web.reconnect.*`
## Logs + troubleshooting
- Subsystems: `whatsapp/inbound` , `whatsapp/outbound` , `web-heartbeat` , `web-reconnect` .
2026-01-04 14:32:47 +00:00
- Log file: `/tmp/clawdbot/clawdbot-YYYY-MM-DD.log` (configurable).
2026-01-06 18:59:06 +01:00
- Troubleshooting guide: [`docs/troubleshooting.md` ](https://docs.clawd.bot/troubleshooting ).
2025-12-23 03:00:27 +01:00
## Tests
- `src/web/auto-reply.test.ts` (mention gating, history injection, reply flow)
- `src/web/monitor-inbox.test.ts` (inbound parsing + reply context)
- `src/web/outbound.test.ts` (send mapping + media)