mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-23 22:55:24 +00:00
fix(whatsapp): trim leading whitespace in direct outbound sends (#43539)
Trim leading whitespace from direct WhatsApp text and media caption sends. Also guard empty text-only web sends after trimming.
This commit is contained in:
@@ -48,6 +48,34 @@ describe("web outbound", () => {
|
||||
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);
|
||||
|
||||
const buf = Buffer.from("img");
|
||||
loadWebMediaMock.mockResolvedValueOnce({
|
||||
buffer: buf,
|
||||
contentType: "image/jpeg",
|
||||
kind: "image",
|
||||
});
|
||||
await sendMessageWhatsApp("+1555", "\n \tcaption", {
|
||||
verbose: false,
|
||||
mediaUrl: "/tmp/pic.jpg",
|
||||
});
|
||||
expect(sendMessage).toHaveBeenLastCalledWith("+1555", "caption", buf, "image/jpeg");
|
||||
});
|
||||
|
||||
it("skips whitespace-only text sends without media", async () => {
|
||||
const result = await sendMessageWhatsApp("+1555", "\n \t", { verbose: false });
|
||||
|
||||
expect(result).toEqual({
|
||||
messageId: "",
|
||||
toJid: "1555@s.whatsapp.net",
|
||||
});
|
||||
expect(sendComposingTo).not.toHaveBeenCalled();
|
||||
expect(sendMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("throws a helpful error when no active listener exists", async () => {
|
||||
setActiveWebListener(null);
|
||||
await expect(
|
||||
|
||||
@@ -26,7 +26,11 @@ export async function sendMessageWhatsApp(
|
||||
accountId?: string;
|
||||
},
|
||||
): Promise<{ messageId: string; toJid: string }> {
|
||||
let text = body;
|
||||
let text = body.trimStart();
|
||||
const jid = toWhatsappJid(to);
|
||||
if (!text && !options.mediaUrl) {
|
||||
return { messageId: "", toJid: jid };
|
||||
}
|
||||
const correlationId = generateSecureUuid();
|
||||
const startedAt = Date.now();
|
||||
const { listener: active, accountId: resolvedAccountId } = requireActiveWebListener(
|
||||
@@ -51,7 +55,6 @@ export async function sendMessageWhatsApp(
|
||||
to: redactedTo,
|
||||
});
|
||||
try {
|
||||
const jid = toWhatsappJid(to);
|
||||
const redactedJid = redactIdentifier(jid);
|
||||
let mediaBuffer: Buffer | undefined;
|
||||
let mediaType: string | undefined;
|
||||
|
||||
Reference in New Issue
Block a user