mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 15:08:25 +00:00
refactor(onboarding): dedupe whatsapp allowlist prompt
This commit is contained in:
@@ -37,6 +37,48 @@ async function detectWhatsAppLinked(cfg: OpenClawConfig, accountId: string): Pro
|
|||||||
return await pathExists(credsPath);
|
return await pathExists(credsPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function promptWhatsAppOwnerAllowFrom(params: {
|
||||||
|
prompter: WizardPrompter;
|
||||||
|
existingAllowFrom: string[];
|
||||||
|
}): Promise<{ normalized: string; allowFrom: string[] }> {
|
||||||
|
const { prompter, existingAllowFrom } = params;
|
||||||
|
|
||||||
|
await prompter.note(
|
||||||
|
"We need the sender/owner number so OpenClaw can allowlist you.",
|
||||||
|
"WhatsApp number",
|
||||||
|
);
|
||||||
|
const entry = await prompter.text({
|
||||||
|
message: "Your personal WhatsApp number (the phone you will message from)",
|
||||||
|
placeholder: "+15555550123",
|
||||||
|
initialValue: existingAllowFrom[0],
|
||||||
|
validate: (value) => {
|
||||||
|
const raw = String(value ?? "").trim();
|
||||||
|
if (!raw) {
|
||||||
|
return "Required";
|
||||||
|
}
|
||||||
|
const normalized = normalizeE164(raw);
|
||||||
|
if (!normalized) {
|
||||||
|
return `Invalid number: ${raw}`;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const normalized = normalizeE164(String(entry).trim());
|
||||||
|
if (!normalized) {
|
||||||
|
throw new Error("Invalid WhatsApp owner number (expected E.164 after validation).");
|
||||||
|
}
|
||||||
|
const merged = [
|
||||||
|
...existingAllowFrom
|
||||||
|
.filter((item) => item !== "*")
|
||||||
|
.map((item) => normalizeE164(item))
|
||||||
|
.filter(Boolean),
|
||||||
|
normalized,
|
||||||
|
];
|
||||||
|
const allowFrom = [...new Set(merged.filter(Boolean))];
|
||||||
|
return { normalized, allowFrom };
|
||||||
|
}
|
||||||
|
|
||||||
async function promptWhatsAppAllowFrom(
|
async function promptWhatsAppAllowFrom(
|
||||||
cfg: OpenClawConfig,
|
cfg: OpenClawConfig,
|
||||||
_runtime: RuntimeEnv,
|
_runtime: RuntimeEnv,
|
||||||
@@ -48,38 +90,13 @@ async function promptWhatsAppAllowFrom(
|
|||||||
const existingLabel = existingAllowFrom.length > 0 ? existingAllowFrom.join(", ") : "unset";
|
const existingLabel = existingAllowFrom.length > 0 ? existingAllowFrom.join(", ") : "unset";
|
||||||
|
|
||||||
if (options?.forceAllowlist) {
|
if (options?.forceAllowlist) {
|
||||||
await prompter.note(
|
const { normalized, allowFrom } = await promptWhatsAppOwnerAllowFrom({
|
||||||
"We need the sender/owner number so OpenClaw can allowlist you.",
|
prompter,
|
||||||
"WhatsApp number",
|
existingAllowFrom,
|
||||||
);
|
|
||||||
const entry = await prompter.text({
|
|
||||||
message: "Your personal WhatsApp number (the phone you will message from)",
|
|
||||||
placeholder: "+15555550123",
|
|
||||||
initialValue: existingAllowFrom[0],
|
|
||||||
validate: (value) => {
|
|
||||||
const raw = String(value ?? "").trim();
|
|
||||||
if (!raw) {
|
|
||||||
return "Required";
|
|
||||||
}
|
|
||||||
const normalized = normalizeE164(raw);
|
|
||||||
if (!normalized) {
|
|
||||||
return `Invalid number: ${raw}`;
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
const normalized = normalizeE164(String(entry).trim());
|
|
||||||
const merged = [
|
|
||||||
...existingAllowFrom
|
|
||||||
.filter((item) => item !== "*")
|
|
||||||
.map((item) => normalizeE164(item))
|
|
||||||
.filter(Boolean),
|
|
||||||
normalized,
|
|
||||||
];
|
|
||||||
const unique = [...new Set(merged.filter(Boolean))];
|
|
||||||
let next = setWhatsAppSelfChatMode(cfg, true);
|
let next = setWhatsAppSelfChatMode(cfg, true);
|
||||||
next = setWhatsAppDmPolicy(next, "allowlist");
|
next = setWhatsAppDmPolicy(next, "allowlist");
|
||||||
next = setWhatsAppAllowFrom(next, unique);
|
next = setWhatsAppAllowFrom(next, allowFrom);
|
||||||
await prompter.note(
|
await prompter.note(
|
||||||
["Allowlist mode enabled.", `- allowFrom includes ${normalized}`].join("\n"),
|
["Allowlist mode enabled.", `- allowFrom includes ${normalized}`].join("\n"),
|
||||||
"WhatsApp allowlist",
|
"WhatsApp allowlist",
|
||||||
@@ -110,38 +127,13 @@ async function promptWhatsAppAllowFrom(
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (phoneMode === "personal") {
|
if (phoneMode === "personal") {
|
||||||
await prompter.note(
|
const { normalized, allowFrom } = await promptWhatsAppOwnerAllowFrom({
|
||||||
"We need the sender/owner number so OpenClaw can allowlist you.",
|
prompter,
|
||||||
"WhatsApp number",
|
existingAllowFrom,
|
||||||
);
|
|
||||||
const entry = await prompter.text({
|
|
||||||
message: "Your personal WhatsApp number (the phone you will message from)",
|
|
||||||
placeholder: "+15555550123",
|
|
||||||
initialValue: existingAllowFrom[0],
|
|
||||||
validate: (value) => {
|
|
||||||
const raw = String(value ?? "").trim();
|
|
||||||
if (!raw) {
|
|
||||||
return "Required";
|
|
||||||
}
|
|
||||||
const normalized = normalizeE164(raw);
|
|
||||||
if (!normalized) {
|
|
||||||
return `Invalid number: ${raw}`;
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
const normalized = normalizeE164(String(entry).trim());
|
|
||||||
const merged = [
|
|
||||||
...existingAllowFrom
|
|
||||||
.filter((item) => item !== "*")
|
|
||||||
.map((item) => normalizeE164(item))
|
|
||||||
.filter(Boolean),
|
|
||||||
normalized,
|
|
||||||
];
|
|
||||||
const unique = [...new Set(merged.filter(Boolean))];
|
|
||||||
let next = setWhatsAppSelfChatMode(cfg, true);
|
let next = setWhatsAppSelfChatMode(cfg, true);
|
||||||
next = setWhatsAppDmPolicy(next, "allowlist");
|
next = setWhatsAppDmPolicy(next, "allowlist");
|
||||||
next = setWhatsAppAllowFrom(next, unique);
|
next = setWhatsAppAllowFrom(next, allowFrom);
|
||||||
await prompter.note(
|
await prompter.note(
|
||||||
[
|
[
|
||||||
"Personal phone mode enabled.",
|
"Personal phone mode enabled.",
|
||||||
|
|||||||
Reference in New Issue
Block a user