# wa-whatsmeow — WhatsApp self-chat → Claude Haiku bridge (Go / whatsmeow)

Routes the owner's WhatsApp **self-chat** to **Claude Haiku** via the
Max-subscription `claude` CLI — **no API key, no Baileys, no Hermes/llama**.

- Library: [`go.mau.fi/whatsmeow`](https://github.com/tulir/whatsmeow) (Go)
- Brain: `claude -p "<msg>" --model claude-haiku-4-5 --output-format text --permission-mode bypassPermissions`
- Owner number (hard-coded gate): **+000000000000**
- Session store: `wa-whatsmeow.db` (SQLite, single file, created on first pair)

This bridge has its **own** whatsmeow session and does **not** touch the existing
Hermes/Baileys session under `C:\Users\iddo1\.hermes\whatsapp\session`. Running
both at once links two separate devices to your WhatsApp; that's fine, but if you
intend to cut over, stop Hermes first (your call — this bridge won't).

---

## What it does

1. Connects to WhatsApp; on first run shows a QR (or a pairing code) to link a device.
2. Listens for incoming messages.
3. **Self-chat gate** — only acts on messages where the sender is the owner number
   **and** the chat is the owner's own DM. Groups, newsletters, `status@broadcast`,
   and any other contact are ignored outright.
4. For each qualifying message, spawns the `claude` CLI with the message text as
   the Haiku prompt and sends the reply back into the same self-chat.

### Safety properties

- **Prompt-injection guard:** inbound text is treated as *untrusted data*. It is
  passed **only** as the `-p` prompt argument via `os/exec` (no shell, no string
  interpolation into other flags/model selection/routing). A message can never
  change the bridge's behavior, spawn extra processes, or alter gating.
- **Rate-limit pacing:** a minimum 3 s gap is enforced between CLI spawns.
- **Timeout:** each CLI call is capped at 120 s.
- **Prompt size:** inbound text is truncated to 8000 chars before forwarding.

---

## Build (already done, but to rebuild)

Go 1.26+ required (installed via `winget install GoLang.Go`).

```cmd
cd /d D:\CLAUDE\wa-whatsmeow
"C:\Program Files\Go\bin\go.exe" build -o wa-whatsmeow.exe .
```

Produces `wa-whatsmeow.exe` (~30 MB, statically linked; pure-Go SQLite driver
`modernc.org/sqlite`, so **no CGO / C compiler needed**).

---

## Pair + run (FIRST TIME — the user must do this)

The bridge cannot pair itself; WhatsApp requires you to link the device from
your phone. Two options:

### Option A — QR scan (default)

1. Double-click **`start-wa-whatsmeow.cmd`** (window titled *Claude — WA whatsmeow/Haiku*).
2. A QR code prints in the terminal (the raw pairing string is also printed under
   it as a fallback).
3. On your phone: **WhatsApp → Settings → Linked Devices → Link a Device** and
   scan the QR.
4. The terminal prints `PAIRED successfully` then `CONNECTED — bridge live`.

### Option B — Pairing code (link by phone number, no camera)

1. Edit `start-wa-whatsmeow.cmd` and uncomment `set WA_PAIR_CODE=1`
   (or run `set WA_PAIR_CODE=1` then `wa-whatsmeow.exe`).
2. The terminal prints an 8-character **PAIRING CODE**.
3. On your phone: **WhatsApp → Settings → Linked Devices → Link a Device →
   "Link with phone number instead"**, enter your number if asked, then type the code.
4. Terminal prints `PAIRED successfully` then `CONNECTED`.

### Test it

Open WhatsApp, go to the chat **with yourself** (message yourself / "You"),
and send any text, e.g. `hello haiku`. Within a few seconds the bridge replies
in that same chat with Haiku's answer.

### Subsequent runs

Just launch `start-wa-whatsmeow.cmd`. The session persists in `wa-whatsmeow.db`,
so no re-pairing is needed unless you unlink the device or delete the DB.

---

## Files

| File | Purpose |
|---|---|
| `main.go` | Bridge source |
| `wa-whatsmeow.exe` | Built binary |
| `start-wa-whatsmeow.cmd` | Launcher (sets window title) |
| `wa-whatsmeow.db` | WhatsApp session (created on first pair; treat as secret) |
| `go.mod` / `go.sum` | Module deps |

## Environment overrides (optional)

| Var | Default | Effect |
|---|---|---|
| `CLAUDE_BIN` | `%USERPROFILE%\.local\bin\claude.exe` then PATH | claude binary path |
| `WA_PAIR_CODE` | unset | `1` = use phone-number pairing code instead of QR |
| `WA_DB_DIR` | `.` | directory for `wa-whatsmeow.db` |

## Troubleshooting

- **`logged out by WhatsApp`** — the device was unlinked. Delete `wa-whatsmeow.db`
  and re-pair.
- **`claude CLI exit ...`** — check `claude auth status` shows `max` / `firstParty`
  and that `claude -p "hi" --model claude-haiku-4-5 --output-format text --permission-mode bypassPermissions` works in a plain terminal.
- **No reply to self-messages** — confirm you're messaging *yourself* (the
  owner number) and not a different chat; the gate is strict by design.
