fix: honor whatsapp default outbound account

This commit is contained in:
Tak Hoffman
2026-04-03 14:21:38 -05:00
parent 63443acc2b
commit 489a62e788
2 changed files with 65 additions and 6 deletions

View File

@@ -84,6 +84,36 @@ describe("web outbound", () => {
expect(sendMessage).toHaveBeenCalledWith("+1555", "hi", undefined, undefined);
});
it("uses configured defaultAccount when outbound accountId is omitted", async () => {
setActiveWebListener(null);
setActiveWebListener("work", {
sendComposingTo,
sendMessage,
sendPoll,
sendReaction,
});
const result = await sendMessageWhatsApp("+1555", "hi", {
verbose: false,
cfg: {
channels: {
whatsapp: {
defaultAccount: "work",
accounts: {
work: {},
},
},
},
} as OpenClawConfig,
});
expect(result).toEqual({
messageId: "msg123",
toJid: "1555@s.whatsapp.net",
});
expect(sendMessage).toHaveBeenCalledWith("+1555", "hi", undefined, undefined);
});
it("trims leading whitespace before sending text and captions", async () => {
await sendMessageWhatsApp("+1555", "\n \thello", { verbose: false });
expect(sendMessage).toHaveBeenLastCalledWith("+1555", "hello", undefined, undefined);

View File

@@ -8,12 +8,27 @@ import { redactIdentifier } from "openclaw/plugin-sdk/text-runtime";
import { convertMarkdownTables } from "openclaw/plugin-sdk/text-runtime";
import { markdownToWhatsApp } from "openclaw/plugin-sdk/text-runtime";
import { toWhatsappJid } from "openclaw/plugin-sdk/text-runtime";
import { resolveWhatsAppAccount, resolveWhatsAppMediaMaxBytes } from "./accounts.js";
import {
resolveDefaultWhatsAppAccountId,
resolveWhatsAppAccount,
resolveWhatsAppMediaMaxBytes,
} from "./accounts.js";
import { type ActiveWebSendOptions, requireActiveWebListener } from "./active-listener.js";
import { loadOutboundMediaFromUrl } from "./runtime-api.js";
const outboundLog = createSubsystemLogger("gateway/channels/whatsapp").child("outbound");
function resolveOutboundWhatsAppAccountId(params: {
cfg: OpenClawConfig;
accountId?: string;
}): string | undefined {
const explicitAccountId = params.accountId?.trim();
if (explicitAccountId) {
return explicitAccountId;
}
return resolveDefaultWhatsAppAccountId(params.cfg);
}
export async function sendMessageWhatsApp(
to: string,
body: string,
@@ -38,10 +53,14 @@ export async function sendMessageWhatsApp(
}
const correlationId = generateSecureUuid();
const startedAt = Date.now();
const { listener: active, accountId: resolvedAccountId } = requireActiveWebListener(
options.accountId,
);
const cfg = options.cfg ?? loadConfig();
const effectiveAccountId = resolveOutboundWhatsAppAccountId({
cfg,
accountId: options.accountId,
});
const { listener: active, accountId: resolvedAccountId } = requireActiveWebListener(
effectiveAccountId,
);
const account = resolveWhatsAppAccount({
cfg,
accountId: resolvedAccountId ?? options.accountId,
@@ -133,7 +152,12 @@ export async function sendReactionWhatsApp(
},
): Promise<void> {
const correlationId = generateSecureUuid();
const { listener: active } = requireActiveWebListener(options.accountId);
const cfg = loadConfig();
const effectiveAccountId = resolveOutboundWhatsAppAccountId({
cfg,
accountId: options.accountId,
});
const { listener: active } = requireActiveWebListener(effectiveAccountId);
const redactedChatJid = redactIdentifier(chatJid);
const logger = getChildLogger({
module: "web-outbound",
@@ -171,7 +195,12 @@ export async function sendPollWhatsApp(
): Promise<{ messageId: string; toJid: string }> {
const correlationId = generateSecureUuid();
const startedAt = Date.now();
const { listener: active } = requireActiveWebListener(options.accountId);
const cfg = options.cfg ?? loadConfig();
const effectiveAccountId = resolveOutboundWhatsAppAccountId({
cfg,
accountId: options.accountId,
});
const { listener: active } = requireActiveWebListener(effectiveAccountId);
const redactedTo = redactIdentifier(to);
const logger = getChildLogger({
module: "web-outbound",