Skip to content

Commit a6da88a

Browse files
feat: guide owpenwork setup flow (anomalyco#242)
1 parent 78c0ea7 commit a6da88a

5 files changed

Lines changed: 151 additions & 121 deletions

File tree

‎packages/owpenbot/README.md‎

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ npm install -g owpenwork
1919
Quick run without install:
2020

2121
```bash
22-
npx owpenwork setup
23-
npx owpenwork whatsapp login
24-
npx owpenwork start
22+
npx owpenwork
2523
```
2624

27-
Then follow the printed next steps (run `owpenbot setup`, link WhatsApp, start the bridge).
25+
Then follow the prompts (setup, QR login, start).
2826

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

@@ -43,25 +41,13 @@ Recommended:
4341
- `OPENCODE_SERVER_USERNAME`
4442
- `OPENCODE_SERVER_PASSWORD`
4543

46-
3) Run setup (writes `~/.owpenbot/owpenbot.json`):
44+
3) Run owpenwork and follow the guided setup:
4745

4846
```bash
49-
owpenwork setup
47+
owpenwork
5048
```
5149

52-
4) Link WhatsApp (QR):
53-
54-
```bash
55-
owpenwork whatsapp login
56-
```
57-
58-
5) Start the bridge:
59-
60-
```bash
61-
owpenwork start
62-
```
63-
64-
Owpenbot keeps the WhatsApp session alive once connected.
50+
Owpenwork keeps the WhatsApp session alive once connected.
6551

6652
6) Pair a user with the bot (only if DM policy is pairing):
6753

@@ -75,8 +61,8 @@ Owpenbot keeps the WhatsApp session alive once connected.
7561

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

78-
1) Run `owpenwork setup` and choose “personal number.”
79-
2) Run `owpenwork whatsapp login` to scan the QR.
64+
1) Run `owpenwork` and choose “personal number.”
65+
2) Scan the QR when prompted.
8066
3) Message yourself or from a second number; your number is already allowlisted.
8167

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

8874
1) Create a new WhatsApp account for the dedicated number.
89-
2) Run `owpenwork setup` and choose “dedicated number.”
90-
3) Run `owpenwork whatsapp login` to scan the QR.
75+
2) Run `owpenwork` and choose “dedicated number.”
76+
3) Scan the QR when prompted.
9177
4) If DM policy is pairing, approve codes with `owpenwork pairing approve <code>`.
9278

9379
## Telegram (Untested)
@@ -99,9 +85,8 @@ Telegram support is wired but not E2E tested yet. To try it:
9985
## Commands
10086

10187
```bash
102-
owpenwork setup
103-
owpenwork whatsapp login
104-
owpenwork start
88+
owpenwork
89+
owpenwork --non-interactive
10590
owpenwork pairing list
10691
owpenwork pairing approve <code>
10792
owpenwork status
@@ -110,7 +95,7 @@ owpenwork status
11095
## Defaults
11196

11297
- SQLite at `~/.owpenbot/owpenbot.db` unless overridden.
113-
- Config stored at `~/.owpenbot/owpenbot.json` (created by `owpenbot setup`).
98+
- Config stored at `~/.owpenbot/owpenbot.json` (created by `owpenwork` or `owpenwork setup`).
11499
- DM policy defaults to `pairing` unless changed in setup.
115100
- Group chats are disabled unless `GROUPS_ENABLED=true`.
116101

‎packages/owpenbot/install.sh‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,8 @@ cat <<EOF
140140
Owpenbot installed.
141141
142142
Next steps:
143-
1) Run setup: owpenwork setup
144-
2) Link WhatsApp: owpenwork whatsapp login
145-
3) Start bridge: owpenwork start
143+
1) Run owpenwork: owpenwork
144+
2) Follow the guided setup + QR login
146145
147146
Owpenbot will print a QR code during login and keep the session alive.
148147
EOF

‎packages/owpenbot/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "owpenwork",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "WhatsApp bridge for a running OpenCode server",
55
"private": false,
66
"type": "module",

‎packages/owpenbot/scripts/test-cli.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async function runSetupNonInteractive() {
6464
OWPENBOT_CONFIG_PATH: path.join(tempDir, "owpenbot.json"),
6565
OPENCODE_DIRECTORY: tempDir,
6666
};
67-
const result = await run("node", [cliPath, "setup", "--non-interactive"], {
67+
const result = await run("node", [cliPath, "--non-interactive"], {
6868
env,
6969
timeoutMs: 5000,
7070
});
@@ -87,7 +87,7 @@ async function runSetupInteractive() {
8787
OPENCODE_DIRECTORY: tempDir,
8888
};
8989
const input = "1\n+15551234567\n";
90-
const result = await run("node", ["--no-warnings", cliPath, "setup"], {
90+
const result = await run("node", ["--no-warnings", cliPath], {
9191
env,
9292
timeoutMs: 1500,
9393
expectTimeout: true,

‎packages/owpenbot/src/cli.ts‎

Lines changed: 134 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22
import fs from "node:fs";
3+
import path from "node:path";
34
import { createInterface } from "node:readline/promises";
45

56
import { Command } from "commander";
@@ -23,7 +24,9 @@ const program = new Command();
2324
program
2425
.name("owpenbot")
2526
.description("OpenCode WhatsApp + Telegram bridge")
26-
.argument("[path]");
27+
.argument("[path]")
28+
.option("--non-interactive", "Run setup defaults and exit", false)
29+
.option("--check", "Validate config/auth and exit", false);
2730

2831
const runStart = async (pathOverride?: string) => {
2932
if (pathOverride?.trim()) {
@@ -35,7 +38,7 @@ const runStart = async (pathOverride?: string) => {
3538
process.env.OPENCODE_DIRECTORY = config.opencodeDirectory;
3639
}
3740
const bridge = await startBridge(config, logger);
38-
logger.info("Commands: owpenbot whatsapp login, owpenbot pairing list, owpenbot status");
41+
logger.info("Commands: owpenwork whatsapp login, owpenwork pairing list, owpenwork status");
3942

4043
const shutdown = async () => {
4144
logger.info("shutting down");
@@ -47,6 +50,132 @@ const runStart = async (pathOverride?: string) => {
4750
process.on("SIGTERM", shutdown);
4851
};
4952

53+
async function runSetupWizard(config: ReturnType<typeof loadConfig>, nonInteractive: boolean) {
54+
const { config: existing } = readConfigFile(config.configPath);
55+
const next: OwpenbotConfigFile = existing ?? { version: 1 };
56+
57+
if (nonInteractive) {
58+
next.version = 1;
59+
next.channels = next.channels ?? {};
60+
next.channels.whatsapp = {
61+
dmPolicy: "pairing",
62+
allowFrom: [],
63+
selfChatMode: false,
64+
accounts: {
65+
[config.whatsappAccountId]: {
66+
authDir: config.whatsappAuthDir,
67+
},
68+
},
69+
};
70+
writeConfigFile(config.configPath, next);
71+
console.log(`Wrote ${config.configPath}`);
72+
return;
73+
}
74+
75+
const rl = createInterface({ input: process.stdin, output: process.stdout });
76+
const phoneMode = await rl.question(
77+
"WhatsApp setup: (1) Personal number (2) Dedicated number [1]: ",
78+
);
79+
const mode = phoneMode.trim() === "2" ? "dedicated" : "personal";
80+
81+
let dmPolicy: DmPolicy = "pairing";
82+
let allowFrom: string[] = [];
83+
let selfChatMode = false;
84+
85+
if (mode === "personal") {
86+
let normalized = "";
87+
while (!normalized) {
88+
const number = await rl.question("Your WhatsApp number (E.164, e.g. +15551234567): ");
89+
const candidate = normalizeWhatsAppId(number);
90+
if (!/^\+\d+$/.test(candidate)) {
91+
console.log("Invalid number. Try again.");
92+
continue;
93+
}
94+
normalized = candidate;
95+
}
96+
allowFrom = [normalized];
97+
dmPolicy = "allowlist";
98+
selfChatMode = true;
99+
} else {
100+
const policyInput = await rl.question(
101+
"DM policy: (1) Pairing (2) Allowlist (3) Open (4) Disabled [1]: ",
102+
);
103+
const policyChoice = policyInput.trim();
104+
if (policyChoice === "2") dmPolicy = "allowlist";
105+
else if (policyChoice === "3") dmPolicy = "open";
106+
else if (policyChoice === "4") dmPolicy = "disabled";
107+
else dmPolicy = "pairing";
108+
109+
const listInput = await rl.question(
110+
"Allowlist numbers (comma-separated, optional): ",
111+
);
112+
if (listInput.trim()) {
113+
allowFrom = listInput
114+
.split(",")
115+
.map((item) => normalizeWhatsAppId(item))
116+
.filter(Boolean);
117+
}
118+
if (dmPolicy === "open") {
119+
allowFrom = allowFrom.length ? allowFrom : ["*"];
120+
}
121+
}
122+
123+
rl.close();
124+
125+
next.version = 1;
126+
next.channels = next.channels ?? {};
127+
next.channels.whatsapp = {
128+
dmPolicy,
129+
allowFrom,
130+
selfChatMode,
131+
accounts: {
132+
[config.whatsappAccountId]: {
133+
authDir: config.whatsappAuthDir,
134+
},
135+
},
136+
};
137+
writeConfigFile(config.configPath, next);
138+
console.log(`Wrote ${config.configPath}`);
139+
}
140+
141+
async function runGuidedFlow(pathArg: string | undefined, opts: { nonInteractive: boolean; check: boolean }) {
142+
if (pathArg?.trim()) {
143+
process.env.OPENCODE_DIRECTORY = pathArg.trim();
144+
}
145+
let config = loadConfig(process.env, { requireOpencode: false });
146+
if (!process.env.OPENCODE_DIRECTORY) {
147+
process.env.OPENCODE_DIRECTORY = config.opencodeDirectory;
148+
}
149+
150+
if (opts.check) {
151+
const authPath = path.join(config.whatsappAuthDir, "creds.json");
152+
const linked = fs.existsSync(authPath);
153+
const cfgExists = fs.existsSync(config.configPath);
154+
console.log(`Config: ${cfgExists ? "yes" : "no"}`);
155+
console.log(`WhatsApp linked: ${linked ? "yes" : "no"}`);
156+
process.exit(linked && cfgExists ? 0 : 1);
157+
}
158+
159+
const cfgExists = fs.existsSync(config.configPath);
160+
const cfgHasWhatsApp = config.configFile.channels?.whatsapp;
161+
if (!cfgExists || !cfgHasWhatsApp) {
162+
await runSetupWizard(config, opts.nonInteractive);
163+
if (opts.nonInteractive) {
164+
console.log("Next: owpenwork whatsapp login");
165+
return;
166+
}
167+
}
168+
169+
config = loadConfig(process.env, { requireOpencode: false });
170+
171+
const authPath = path.join(config.whatsappAuthDir, "creds.json");
172+
if (!fs.existsSync(authPath)) {
173+
await loginWhatsApp(config, createLogger(config.logLevel));
174+
}
175+
176+
await runStart(pathArg);
177+
}
178+
50179
program
51180
.command("start")
52181
.description("Start the bridge")
@@ -57,7 +186,8 @@ program.action((pathArg: string | undefined) => {
57186
program.outputHelp();
58187
return;
59188
}
60-
return runStart(pathArg);
189+
const opts = program.opts<{ nonInteractive: boolean; check: boolean }>();
190+
return runGuidedFlow(pathArg, { nonInteractive: Boolean(opts.nonInteractive), check: Boolean(opts.check) });
61191
});
62192

63193
program
@@ -66,91 +196,7 @@ program
66196
.option("--non-interactive", "Write defaults without prompts", false)
67197
.action(async (opts) => {
68198
const config = loadConfig(process.env, { requireOpencode: false });
69-
const { config: existing } = readConfigFile(config.configPath);
70-
const next: OwpenbotConfigFile = existing ?? { version: 1 };
71-
72-
if (opts.nonInteractive) {
73-
next.version = 1;
74-
next.channels = next.channels ?? {};
75-
next.channels.whatsapp = {
76-
dmPolicy: "pairing",
77-
allowFrom: [],
78-
selfChatMode: false,
79-
accounts: {
80-
[config.whatsappAccountId]: {
81-
authDir: config.whatsappAuthDir,
82-
},
83-
},
84-
};
85-
writeConfigFile(config.configPath, next);
86-
console.log(`Wrote ${config.configPath}`);
87-
return;
88-
}
89-
90-
const rl = createInterface({ input: process.stdin, output: process.stdout });
91-
const phoneMode = await rl.question(
92-
"WhatsApp setup: (1) Personal number (2) Dedicated number [1]: ",
93-
);
94-
const mode = phoneMode.trim() === "2" ? "dedicated" : "personal";
95-
96-
let dmPolicy: DmPolicy = "pairing";
97-
let allowFrom: string[] = [];
98-
let selfChatMode = false;
99-
100-
if (mode === "personal") {
101-
let normalized = "";
102-
while (!normalized) {
103-
const number = await rl.question("Your WhatsApp number (E.164, e.g. +15551234567): ");
104-
const candidate = normalizeWhatsAppId(number);
105-
if (!/^\+\d+$/.test(candidate)) {
106-
console.log("Invalid number. Try again.");
107-
continue;
108-
}
109-
normalized = candidate;
110-
}
111-
allowFrom = [normalized];
112-
dmPolicy = "allowlist";
113-
selfChatMode = true;
114-
} else {
115-
const policyInput = await rl.question(
116-
"DM policy: (1) Pairing (2) Allowlist (3) Open (4) Disabled [1]: ",
117-
);
118-
const policyChoice = policyInput.trim();
119-
if (policyChoice === "2") dmPolicy = "allowlist";
120-
else if (policyChoice === "3") dmPolicy = "open";
121-
else if (policyChoice === "4") dmPolicy = "disabled";
122-
else dmPolicy = "pairing";
123-
124-
const listInput = await rl.question(
125-
"Allowlist numbers (comma-separated, optional): ",
126-
);
127-
if (listInput.trim()) {
128-
allowFrom = listInput
129-
.split(",")
130-
.map((item) => normalizeWhatsAppId(item))
131-
.filter(Boolean);
132-
}
133-
if (dmPolicy === "open") {
134-
allowFrom = allowFrom.length ? allowFrom : ["*"];
135-
}
136-
}
137-
138-
rl.close();
139-
140-
next.version = 1;
141-
next.channels = next.channels ?? {};
142-
next.channels.whatsapp = {
143-
dmPolicy,
144-
allowFrom,
145-
selfChatMode,
146-
accounts: {
147-
[config.whatsappAccountId]: {
148-
authDir: config.whatsappAuthDir,
149-
},
150-
},
151-
};
152-
writeConfigFile(config.configPath, next);
153-
console.log(`Wrote ${config.configPath}`);
199+
await runSetupWizard(config, Boolean(opts.nonInteractive));
154200
});
155201

156202
program

0 commit comments

Comments
 (0)