From e75c29d57fb4d3b9ab6608d59fe2db8155066630 Mon Sep 17 00:00:00 2001 From: Dani Lipari <64545085+danilipari@users.noreply.github.com> Date: Mon, 16 Feb 2026 08:37:32 +0100 Subject: [PATCH] [core] fix: strip escaped newlines from ChatWoot v4.10.1+ messages (#1900) ChatWoot v4.10.1 escapes newlines by prepending a trailing backslash before each line break in webhook payloads. This caused literal backslash characters to appear in WhatsApp messages. Strip the trailing backslash before newlines in MarkdownToWhatsApp() so line breaks are preserved correctly. Closes #1833 Mentions chatwoot/chatwoot#13348 Co-authored-by: Dani Lipari --- src/apps/chatwoot/messages/to/whatsapp/markdown.test.ts | 6 ++++++ src/apps/chatwoot/messages/to/whatsapp/markdown.ts | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/apps/chatwoot/messages/to/whatsapp/markdown.test.ts b/src/apps/chatwoot/messages/to/whatsapp/markdown.test.ts index e8acf5f9..bb9e632d 100644 --- a/src/apps/chatwoot/messages/to/whatsapp/markdown.test.ts +++ b/src/apps/chatwoot/messages/to/whatsapp/markdown.test.ts @@ -15,6 +15,12 @@ describe('MarkdownToWhatsApp', () => { expect(MarkdownToWhatsApp(input)).toBe(expected); }); + // ChatWoot v4.10.1+ escapes newlines with trailing backslash + it('strips trailing backslash before newlines', () => { + const input = 'Hi \\\ntext message \\\nwith line \\\nbreak'; + expect(MarkdownToWhatsApp(input)).toBe('Hi \ntext message \nwith line \nbreak'); + }); + // Tests for URL protection it('preserves URLs with multiple underscores', () => { const input = 'https://example.com/page__name__test'; diff --git a/src/apps/chatwoot/messages/to/whatsapp/markdown.ts b/src/apps/chatwoot/messages/to/whatsapp/markdown.ts index bed57fa1..40c3e860 100644 --- a/src/apps/chatwoot/messages/to/whatsapp/markdown.ts +++ b/src/apps/chatwoot/messages/to/whatsapp/markdown.ts @@ -4,6 +4,8 @@ export function MarkdownToWhatsApp(text: string): string { } return ( text + // Remove trailing backslash before newlines (ChatWoot v4.10.1+ escaping) + .replace(/\\\n/g, '\n') // Triple-backtick blocks → WhatsApp monospace blocks (same syntax) .replace(/```([\s\S]*?)```/g, '```$1```') // Italic **first**, but only single‐star or single‐underscore