fix: normalize WhatsApp targets for groups and E.164 (#631) (thanks @imfing)

This commit is contained in:
Xin
2026-01-10 00:07:09 +00:00
committed by Peter Steinberger
parent a6822e1210
commit f0700e9778
8 changed files with 181 additions and 13 deletions

View File

@@ -126,6 +126,36 @@ describe("resolveHeartbeatDeliveryTarget", () => {
});
});
it("keeps WhatsApp group targets even with allowFrom set", () => {
const cfg: ClawdbotConfig = {
whatsapp: { allowFrom: ["+1555"] },
};
const entry = {
...baseEntry,
lastProvider: "whatsapp" as const,
lastTo: "120363401234567890@g.us",
};
expect(resolveHeartbeatDeliveryTarget({ cfg, entry })).toEqual({
provider: "whatsapp",
to: "120363401234567890@g.us",
});
});
it("normalizes prefixed WhatsApp group targets for heartbeat delivery", () => {
const cfg: ClawdbotConfig = {
whatsapp: { allowFrom: ["+1555"] },
};
const entry = {
...baseEntry,
lastProvider: "whatsapp" as const,
lastTo: "whatsapp:group:120363401234567890@G.US",
};
expect(resolveHeartbeatDeliveryTarget({ cfg, entry })).toEqual({
provider: "whatsapp",
to: "120363401234567890@g.us",
});
});
it("keeps explicit telegram targets", () => {
const cfg: ClawdbotConfig = {
agents: { defaults: { heartbeat: { target: "telegram", to: "123" } } },

View File

@@ -12,6 +12,15 @@ describe("resolveOutboundTarget", () => {
expect(res).toEqual({ ok: true, to: "+1555" });
});
it("normalizes whatsapp allowFrom fallback targets", () => {
const res = resolveOutboundTarget({
provider: "whatsapp",
to: "",
allowFrom: ["whatsapp:(555) 123-4567"],
});
expect(res).toEqual({ ok: true, to: "+5551234567" });
});
it("normalizes whatsapp target when provided", () => {
const res = resolveOutboundTarget({
provider: "whatsapp",
@@ -21,6 +30,14 @@ describe("resolveOutboundTarget", () => {
expect(res.to).toBe("+5551234567");
});
it("keeps whatsapp group targets", () => {
const res = resolveOutboundTarget({
provider: "whatsapp",
to: "120363401234567890@g.us",
});
expect(res).toEqual({ ok: true, to: "120363401234567890@g.us" });
});
it("rejects telegram with missing target", () => {
const res = resolveOutboundTarget({ provider: "telegram", to: " " });
expect(res.ok).toBe(false);

View File

@@ -4,7 +4,11 @@ import type {
DeliverableMessageProvider,
GatewayMessageProvider,
} from "../../utils/message-provider.js";
import { normalizeE164 } from "../../utils.js";
import {
isWhatsAppGroupJid,
normalizeE164,
normalizeWhatsAppTarget,
} from "../../utils.js";
export type OutboundProvider = DeliverableMessageProvider | "none";
@@ -28,16 +32,28 @@ export function resolveOutboundTarget(params: {
const trimmed = params.to?.trim() || "";
if (params.provider === "whatsapp") {
if (trimmed) {
return { ok: true, to: normalizeE164(trimmed) };
const normalized = normalizeWhatsAppTarget(trimmed);
if (!normalized) {
return {
ok: false,
error: new Error(
"Delivering to WhatsApp requires --to <E.164|group JID> or whatsapp.allowFrom[0]",
),
};
}
return { ok: true, to: normalized };
}
const fallback = params.allowFrom?.[0]?.trim();
if (fallback) {
return { ok: true, to: fallback };
const normalized = normalizeWhatsAppTarget(fallback);
if (normalized) {
return { ok: true, to: normalized };
}
}
return {
ok: false,
error: new Error(
"Delivering to WhatsApp requires --to <E.164> or whatsapp.allowFrom[0]",
"Delivering to WhatsApp requires --to <E.164|group JID> or whatsapp.allowFrom[0]",
),
};
}
@@ -194,6 +210,7 @@ export function resolveHeartbeatDeliveryTarget(params: {
return { provider: "none", reason: "no-target" };
}
if (rawAllow.includes("*")) return { provider, to: resolved.to };
if (isWhatsAppGroupJid(resolved.to)) return { provider, to: resolved.to };
const allowFrom = rawAllow
.map((val) => normalizeE164(val))
.filter((val) => val.length > 1);