mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-29 07:01:40 +03:00
refactor(whatsapp): share target resolver
This commit is contained in:
@@ -7,19 +7,18 @@ import {
|
|||||||
escapeRegExp,
|
escapeRegExp,
|
||||||
formatPairingApproveHint,
|
formatPairingApproveHint,
|
||||||
getChatChannelMeta,
|
getChatChannelMeta,
|
||||||
isWhatsAppGroupJid,
|
|
||||||
listWhatsAppAccountIds,
|
listWhatsAppAccountIds,
|
||||||
listWhatsAppDirectoryGroupsFromConfig,
|
listWhatsAppDirectoryGroupsFromConfig,
|
||||||
listWhatsAppDirectoryPeersFromConfig,
|
listWhatsAppDirectoryPeersFromConfig,
|
||||||
looksLikeWhatsAppTargetId,
|
looksLikeWhatsAppTargetId,
|
||||||
migrateBaseNameToDefaultAccount,
|
migrateBaseNameToDefaultAccount,
|
||||||
missingTargetError,
|
|
||||||
normalizeAccountId,
|
normalizeAccountId,
|
||||||
normalizeE164,
|
normalizeE164,
|
||||||
normalizeWhatsAppMessagingTarget,
|
normalizeWhatsAppMessagingTarget,
|
||||||
normalizeWhatsAppTarget,
|
normalizeWhatsAppTarget,
|
||||||
readStringParam,
|
readStringParam,
|
||||||
resolveDefaultWhatsAppAccountId,
|
resolveDefaultWhatsAppAccountId,
|
||||||
|
resolveWhatsAppOutboundTarget,
|
||||||
resolveWhatsAppAccount,
|
resolveWhatsAppAccount,
|
||||||
resolveWhatsAppGroupRequireMention,
|
resolveWhatsAppGroupRequireMention,
|
||||||
resolveWhatsAppGroupToolPolicy,
|
resolveWhatsAppGroupToolPolicy,
|
||||||
@@ -289,45 +288,8 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
|
|||||||
chunkerMode: "text",
|
chunkerMode: "text",
|
||||||
textChunkLimit: 4000,
|
textChunkLimit: 4000,
|
||||||
pollMaxOptions: 12,
|
pollMaxOptions: 12,
|
||||||
resolveTarget: ({ to, allowFrom, mode }) => {
|
resolveTarget: ({ to, allowFrom, mode }) =>
|
||||||
const trimmed = to?.trim() ?? "";
|
resolveWhatsAppOutboundTarget({ to, allowFrom, mode }),
|
||||||
const allowListRaw = (allowFrom ?? []).map((entry) => String(entry).trim()).filter(Boolean);
|
|
||||||
const hasWildcard = allowListRaw.includes("*");
|
|
||||||
const allowList = allowListRaw
|
|
||||||
.filter((entry) => entry !== "*")
|
|
||||||
.map((entry) => normalizeWhatsAppTarget(entry))
|
|
||||||
.filter((entry): entry is string => Boolean(entry));
|
|
||||||
|
|
||||||
if (trimmed) {
|
|
||||||
const normalizedTo = normalizeWhatsAppTarget(trimmed);
|
|
||||||
if (!normalizedTo) {
|
|
||||||
return {
|
|
||||||
ok: false,
|
|
||||||
error: missingTargetError("WhatsApp", "<E.164|group JID>"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (isWhatsAppGroupJid(normalizedTo)) {
|
|
||||||
return { ok: true, to: normalizedTo };
|
|
||||||
}
|
|
||||||
if (mode === "implicit" || mode === "heartbeat") {
|
|
||||||
if (hasWildcard || allowList.length === 0) {
|
|
||||||
return { ok: true, to: normalizedTo };
|
|
||||||
}
|
|
||||||
if (allowList.includes(normalizedTo)) {
|
|
||||||
return { ok: true, to: normalizedTo };
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
ok: false,
|
|
||||||
error: missingTargetError("WhatsApp", "<E.164|group JID>"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return { ok: true, to: normalizedTo };
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
ok: false,
|
|
||||||
error: missingTargetError("WhatsApp", "<E.164|group JID>"),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
sendText: async ({ to, text, accountId, deps, gifPlayback }) => {
|
sendText: async ({ to, text, accountId, deps, gifPlayback }) => {
|
||||||
const send = deps?.sendWhatsApp ?? getWhatsAppRuntime().channel.whatsapp.sendMessageWhatsApp;
|
const send = deps?.sendWhatsApp ?? getWhatsAppRuntime().channel.whatsapp.sendMessageWhatsApp;
|
||||||
const result = await send(to, text, {
|
const result = await send(to, text, {
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import type { ChannelOutboundAdapter } from "../types.js";
|
import type { ChannelOutboundAdapter } from "../types.js";
|
||||||
import { chunkText } from "../../../auto-reply/chunk.js";
|
import { chunkText } from "../../../auto-reply/chunk.js";
|
||||||
import { shouldLogVerbose } from "../../../globals.js";
|
import { shouldLogVerbose } from "../../../globals.js";
|
||||||
import { missingTargetError } from "../../../infra/outbound/target-errors.js";
|
|
||||||
import { sendPollWhatsApp } from "../../../web/outbound.js";
|
import { sendPollWhatsApp } from "../../../web/outbound.js";
|
||||||
import { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "../../../whatsapp/normalize.js";
|
import { resolveWhatsAppOutboundTarget } from "../../../whatsapp/resolve-outbound-target.js";
|
||||||
|
|
||||||
export const whatsappOutbound: ChannelOutboundAdapter = {
|
export const whatsappOutbound: ChannelOutboundAdapter = {
|
||||||
deliveryMode: "gateway",
|
deliveryMode: "gateway",
|
||||||
@@ -11,46 +10,8 @@ export const whatsappOutbound: ChannelOutboundAdapter = {
|
|||||||
chunkerMode: "text",
|
chunkerMode: "text",
|
||||||
textChunkLimit: 4000,
|
textChunkLimit: 4000,
|
||||||
pollMaxOptions: 12,
|
pollMaxOptions: 12,
|
||||||
resolveTarget: ({ to, allowFrom, mode }) => {
|
resolveTarget: ({ to, allowFrom, mode }) =>
|
||||||
const trimmed = to?.trim() ?? "";
|
resolveWhatsAppOutboundTarget({ to, allowFrom, mode }),
|
||||||
const allowListRaw = (allowFrom ?? []).map((entry) => String(entry).trim()).filter(Boolean);
|
|
||||||
const hasWildcard = allowListRaw.includes("*");
|
|
||||||
const allowList = allowListRaw
|
|
||||||
.filter((entry) => entry !== "*")
|
|
||||||
.map((entry) => normalizeWhatsAppTarget(entry))
|
|
||||||
.filter((entry): entry is string => Boolean(entry));
|
|
||||||
|
|
||||||
if (trimmed) {
|
|
||||||
const normalizedTo = normalizeWhatsAppTarget(trimmed);
|
|
||||||
if (!normalizedTo) {
|
|
||||||
return {
|
|
||||||
ok: false,
|
|
||||||
error: missingTargetError("WhatsApp", "<E.164|group JID>"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (isWhatsAppGroupJid(normalizedTo)) {
|
|
||||||
return { ok: true, to: normalizedTo };
|
|
||||||
}
|
|
||||||
if (mode === "implicit" || mode === "heartbeat") {
|
|
||||||
if (hasWildcard || allowList.length === 0) {
|
|
||||||
return { ok: true, to: normalizedTo };
|
|
||||||
}
|
|
||||||
if (allowList.includes(normalizedTo)) {
|
|
||||||
return { ok: true, to: normalizedTo };
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
ok: false,
|
|
||||||
error: missingTargetError("WhatsApp", "<E.164|group JID>"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return { ok: true, to: normalizedTo };
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
ok: false,
|
|
||||||
error: missingTargetError("WhatsApp", "<E.164|group JID>"),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
sendText: async ({ to, text, accountId, deps, gifPlayback }) => {
|
sendText: async ({ to, text, accountId, deps, gifPlayback }) => {
|
||||||
const send =
|
const send =
|
||||||
deps?.sendWhatsApp ?? (await import("../../../web/outbound.js")).sendMessageWhatsApp;
|
deps?.sendWhatsApp ?? (await import("../../../web/outbound.js")).sendMessageWhatsApp;
|
||||||
|
|||||||
@@ -366,6 +366,7 @@ export {
|
|||||||
type ResolvedWhatsAppAccount,
|
type ResolvedWhatsAppAccount,
|
||||||
} from "../web/accounts.js";
|
} from "../web/accounts.js";
|
||||||
export { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "../whatsapp/normalize.js";
|
export { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "../whatsapp/normalize.js";
|
||||||
|
export { resolveWhatsAppOutboundTarget } from "../whatsapp/resolve-outbound-target.js";
|
||||||
export { whatsappOnboardingAdapter } from "../channels/plugins/onboarding/whatsapp.js";
|
export { whatsappOnboardingAdapter } from "../channels/plugins/onboarding/whatsapp.js";
|
||||||
export { resolveWhatsAppHeartbeatRecipients } from "../channels/plugins/whatsapp-heartbeat.js";
|
export { resolveWhatsAppHeartbeatRecipients } from "../channels/plugins/whatsapp-heartbeat.js";
|
||||||
export {
|
export {
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { missingTargetError } from "../infra/outbound/target-errors.js";
|
||||||
|
import { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "./normalize.js";
|
||||||
|
|
||||||
|
export type WhatsAppOutboundTargetResolution =
|
||||||
|
| { ok: true; to: string }
|
||||||
|
| { ok: false; error: Error };
|
||||||
|
|
||||||
|
export function resolveWhatsAppOutboundTarget(params: {
|
||||||
|
to: string | null | undefined;
|
||||||
|
allowFrom: Array<string | number> | null | undefined;
|
||||||
|
mode: string | null | undefined;
|
||||||
|
}): WhatsAppOutboundTargetResolution {
|
||||||
|
const trimmed = params.to?.trim() ?? "";
|
||||||
|
const allowListRaw = (params.allowFrom ?? [])
|
||||||
|
.map((entry) => String(entry).trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
const hasWildcard = allowListRaw.includes("*");
|
||||||
|
const allowList = allowListRaw
|
||||||
|
.filter((entry) => entry !== "*")
|
||||||
|
.map((entry) => normalizeWhatsAppTarget(entry))
|
||||||
|
.filter((entry): entry is string => Boolean(entry));
|
||||||
|
|
||||||
|
if (trimmed) {
|
||||||
|
const normalizedTo = normalizeWhatsAppTarget(trimmed);
|
||||||
|
if (!normalizedTo) {
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
error: missingTargetError("WhatsApp", "<E.164|group JID>"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (isWhatsAppGroupJid(normalizedTo)) {
|
||||||
|
return { ok: true, to: normalizedTo };
|
||||||
|
}
|
||||||
|
if (params.mode === "implicit" || params.mode === "heartbeat") {
|
||||||
|
if (hasWildcard || allowList.length === 0) {
|
||||||
|
return { ok: true, to: normalizedTo };
|
||||||
|
}
|
||||||
|
if (allowList.includes(normalizedTo)) {
|
||||||
|
return { ok: true, to: normalizedTo };
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
error: missingTargetError("WhatsApp", "<E.164|group JID>"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return { ok: true, to: normalizedTo };
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
error: missingTargetError("WhatsApp", "<E.164|group JID>"),
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user