Skip to content

Commit 680c1a7

Browse files
feat: improve owpenbot WhatsApp onboarding (anomalyco#231)
1 parent 24f6a5d commit 680c1a7

12 files changed

Lines changed: 523 additions & 72 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ packages/desktop/src-tauri/sidecars/
1212
# Env
1313
.env
1414
.env.*
15+
!.env.example
1516

1617
# OS
1718
.DS_Store

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The goal: make “agentic work” feel like a product, not a terminal.
2424

2525
- **Owpenbot (WhatsApp bot)**: a lightweight WhatsApp bridge for a running OpenCode server. Install with:
2626
- `curl -fsSL https://raw.githubusercontent.com/different-ai/openwork/dev/packages/owpenbot/install.sh | bash`
27-
- then run `owpenbot`
27+
- run `owpenbot setup`, then `owpenbot whatsapp login`, then `owpenbot start`
2828
- full setup: [packages/owpenbot/README.md](./packages/owpenbot/README.md)
2929

3030

‎packages/owpenbot/.env.example‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
OPENCODE_URL=http://127.0.0.1:4096
2+
OPENCODE_DIRECTORY=
3+
OPENCODE_SERVER_USERNAME=
4+
OPENCODE_SERVER_PASSWORD=
5+
6+
TELEGRAM_BOT_TOKEN=
7+
TELEGRAM_ENABLED=true
8+
9+
OWPENBOT_DATA_DIR=~/.owpenbot
10+
OWPENBOT_DB_PATH=~/.owpenbot/owpenbot.db
11+
OWPENBOT_CONFIG_PATH=~/.owpenbot/owpenbot.json
12+
OWPENBOT_HEALTH_PORT=3005
13+
14+
WHATSAPP_ACCOUNT_ID=default
15+
WHATSAPP_AUTH_DIR=
16+
WHATSAPP_ENABLED=true
17+
WHATSAPP_DM_POLICY=
18+
WHATSAPP_SELF_CHAT=false
19+
20+
ALLOW_FROM=
21+
ALLOW_FROM_TELEGRAM=
22+
ALLOW_FROM_WHATSAPP=
23+
TOOL_UPDATES_ENABLED=false
24+
GROUPS_ENABLED=false
25+
TOOL_OUTPUT_LIMIT=1200
26+
PERMISSION_MODE=allow
27+
LOG_LEVEL=info

‎packages/owpenbot/README.md‎

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ One-command install (recommended):
1010
curl -fsSL https://raw.githubusercontent.com/different-ai/openwork/dev/packages/owpenbot/install.sh | bash
1111
```
1212

13-
Then follow the printed next steps (edit `.env`, run `owpenbot`).
13+
Then follow the printed next steps (run `owpenbot setup`, link WhatsApp, start the bridge).
1414

1515
1) One-command setup (installs deps, builds, creates `.env` if missing):
1616

1717
```bash
1818
pnpm -C packages/owpenbot setup
1919
```
2020

21-
2) Fill in `packages/owpenbot/.env` (see `.env.example`).
21+
2) (Optional) Fill in `packages/owpenbot/.env` (see `.env.example`).
2222

2323
Required:
2424
- `OPENCODE_URL`
@@ -29,29 +29,41 @@ Recommended:
2929
- `OPENCODE_SERVER_USERNAME`
3030
- `OPENCODE_SERVER_PASSWORD`
3131

32-
3) Run the bridge:
32+
3) Run setup (writes `~/.owpenbot/owpenbot.json`):
3333

3434
```bash
35-
owpenbot
35+
owpenbot setup
3636
```
3737

38-
Owpenbot prints a QR code if WhatsApp is not paired and keeps the session alive once connected.
38+
4) Link WhatsApp (QR):
3939

40-
5) Pair a user with the bot:
40+
```bash
41+
owpenbot whatsapp login
42+
```
43+
44+
5) Start the bridge:
45+
46+
```bash
47+
owpenbot start
48+
```
49+
50+
Owpenbot keeps the WhatsApp session alive once connected.
51+
52+
6) Pair a user with the bot (only if DM policy is pairing):
4153

42-
- Run `pnpm -C packages/owpenbot pairing-code` to get the code.
43-
- Send a WhatsApp message containing the code (e.g. `123456 hello`).
44-
- You should receive an OpenCode response in the same chat.
54+
- Run `owpenbot pairing list` to view pending codes.
55+
- Approve a code: `owpenbot pairing approve <code>`.
56+
- The user can then message again to receive OpenCode replies.
4557

4658
## Usage Flows
4759

4860
### One-person flow (personal testing)
4961

5062
Use your own WhatsApp account as the bot and test from a second number you control.
5163

52-
1) Pair WhatsApp using your personal number (just run `owpenbot` to show the QR).
53-
2) Send the pairing code from a second number (SIM/eSIM or another phone).
54-
3) Chat from that second number to receive OpenCode replies.
64+
1) Run `owpenbot setup` and choose “personal number.”
65+
2) Run `owpenbot whatsapp login` to scan the QR.
66+
3) Message yourself or from a second number; your number is already allowlisted.
5567

5668
Note: WhatsApp’s “message yourself” thread is not reliable for bot testing.
5769

@@ -60,9 +72,9 @@ Note: WhatsApp’s “message yourself” thread is not reliable for bot testing
6072
Use a separate WhatsApp number as the bot account so it stays independent from your personal chat history.
6173

6274
1) Create a new WhatsApp account for the dedicated number.
63-
2) Pair that account by running `owpenbot` and scanning the QR.
64-
3) Share the pairing code with the person who should use the bot.
65-
4) Optionally pre-allowlist specific numbers with `ALLOW_FROM_WHATSAPP=`.
75+
2) Run `owpenbot setup` and choose “dedicated number.”
76+
3) Run `owpenbot whatsapp login` to scan the QR.
77+
4) If DM policy is pairing, approve codes with `owpenbot pairing approve <code>`.
6678

6779
## Telegram (Untested)
6880

@@ -73,14 +85,19 @@ Telegram support is wired but not E2E tested yet. To try it:
7385
## Commands
7486

7587
```bash
76-
owpenbot
77-
pnpm -C packages/owpenbot pairing-code
88+
owpenbot setup
89+
owpenbot whatsapp login
90+
owpenbot start
91+
owpenbot pairing list
92+
owpenbot pairing approve <code>
93+
owpenbot status
7894
```
7995

8096
## Defaults
8197

8298
- SQLite at `~/.owpenbot/owpenbot.db` unless overridden.
83-
- Allowlist is enforced by default; a pairing code is generated if not provided.
99+
- Config stored at `~/.owpenbot/owpenbot.json` (created by `owpenbot setup`).
100+
- DM policy defaults to `pairing` unless changed in setup.
84101
- Group chats are disabled unless `GROUPS_ENABLED=true`.
85102

86103
## Tests

‎packages/owpenbot/install.sh‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ cat <<EOF
133133
Owpenbot installed.
134134
135135
Next steps:
136-
1) Edit $ENV_PATH
137-
2) Run owpenbot: owpenbot
136+
1) Edit $ENV_PATH (optional)
137+
2) Run setup: owpenbot setup
138+
3) Link WhatsApp: owpenbot whatsapp login
139+
4) Start bridge: owpenbot start
138140
139-
Owpenbot will print a QR code if WhatsApp is not paired.
141+
Owpenbot will print a QR code during login and keep the session alive.
140142
EOF

‎packages/owpenbot/src/bridge.ts‎

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { setTimeout as delay } from "node:timers/promises";
33
import type { Logger } from "pino";
44

55
import type { Config, ChannelName } from "./config.js";
6+
import { normalizeWhatsAppId } from "./config.js";
67
import { BridgeStore } from "./db.js";
78
import { normalizeEvent } from "./events.js";
89
import { startHealthServer, type HealthSnapshot } from "./health.js";
910
import { buildPermissionRules, createClient } from "./opencode.js";
10-
import { resolvePairingCode } from "./pairing.js";
1111
import { chunkText, formatInputSummary, truncateText } from "./text.js";
1212
import { createTelegramAdapter } from "./telegram.js";
1313
import { createWhatsAppAdapter } from "./whatsapp.js";
@@ -25,6 +25,7 @@ type InboundMessage = {
2525
peerId: string;
2626
text: string;
2727
raw: unknown;
28+
fromMe?: boolean;
2829
};
2930

3031
type RunState = {
@@ -52,10 +53,11 @@ export async function startBridge(config: Config, logger: Logger) {
5253
const client = createClient(config);
5354
const store = new BridgeStore(config.dbPath);
5455
store.seedAllowlist("telegram", config.allowlist.telegram);
55-
store.seedAllowlist("whatsapp", config.allowlist.whatsapp);
56-
57-
const pairingCode = resolvePairingCode(store, config.pairingCode);
58-
logger.info({ pairingCode }, "pairing code ready");
56+
store.seedAllowlist(
57+
"whatsapp",
58+
[...config.whatsappAllowFrom].filter((entry) => entry !== "*"),
59+
);
60+
store.prunePairingRequests();
5961

6062
const adapters = new Map<ChannelName, Adapter>();
6163
if (config.telegramEnabled && config.telegramToken) {
@@ -182,32 +184,57 @@ export async function startBridge(config: Config, logger: Logger) {
182184
{ channel: inbound.channel, peerId: inbound.peerId, length: inbound.text.length },
183185
"received message",
184186
);
187+
const peerKey = inbound.channel === "whatsapp" ? normalizeWhatsAppId(inbound.peerId) : inbound.peerId;
188+
if (inbound.channel === "whatsapp") {
189+
if (config.whatsappDmPolicy === "disabled") {
190+
return;
191+
}
185192

186-
const allowed = store.isAllowed(inbound.channel, inbound.peerId);
187-
if (!allowed) {
188-
const trimmed = inbound.text.trim();
189-
if (trimmed.includes(pairingCode)) {
190-
store.allowPeer(inbound.channel, inbound.peerId);
191-
const remaining = trimmed.replace(pairingCode, "").trim();
192-
if (remaining) {
193-
await sendText(inbound.channel, inbound.peerId, "Paired. Processing your message.");
194-
} else {
195-
await sendText(inbound.channel, inbound.peerId, "Paired. Send your message again.");
193+
const allowAll = config.whatsappDmPolicy === "open" || config.whatsappAllowFrom.has("*");
194+
const isSelf = Boolean(inbound.fromMe && config.whatsappSelfChatMode);
195+
const allowed = allowAll || isSelf || store.isAllowed("whatsapp", peerKey);
196+
if (!allowed) {
197+
if (config.whatsappDmPolicy === "allowlist") {
198+
await sendText(
199+
inbound.channel,
200+
inbound.peerId,
201+
"Access denied. Ask the owner to allowlist your number.",
202+
);
196203
return;
197204
}
198-
inbound = { ...inbound, text: remaining };
199-
} else {
205+
206+
store.prunePairingRequests();
207+
const active = store.getPairingRequest("whatsapp", peerKey);
208+
const pending = store.listPairingRequests("whatsapp");
209+
if (!active && pending.length >= 3) {
210+
await sendText(
211+
inbound.channel,
212+
inbound.peerId,
213+
"Pairing queue full. Ask the owner to approve pending requests.",
214+
);
215+
return;
216+
}
217+
218+
const code = active?.code ?? String(Math.floor(100000 + Math.random() * 900000));
219+
if (!active) {
220+
store.createPairingRequest("whatsapp", peerKey, code, 60 * 60_000);
221+
}
200222
await sendText(
201223
inbound.channel,
202224
inbound.peerId,
203-
`Pairing required. Reply with code: ${pairingCode}`,
225+
`Pairing required. Ask the owner to approve code: ${code}`,
204226
);
205227
return;
206228
}
229+
} else if (config.allowlist[inbound.channel].size > 0) {
230+
if (!store.isAllowed(inbound.channel, peerKey)) {
231+
await sendText(inbound.channel, inbound.peerId, "Access denied.");
232+
return;
233+
}
207234
}
208235

209-
const session = store.getSession(inbound.channel, inbound.peerId);
210-
const sessionID = session?.session_id ?? (await createSession(inbound));
236+
const session = store.getSession(inbound.channel, peerKey);
237+
const sessionID = session?.session_id ?? (await createSession({ ...inbound, peerId: peerKey }));
211238

212239
enqueue(sessionID, async () => {
213240
const runState: RunState = {

0 commit comments

Comments
 (0)