From d02f2675883a7b636fa1a66922b64f85f781e5f5 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Fri, 27 May 2022 11:03:51 -0700 Subject: [PATCH] =?UTF-8?q?fix(engine):=20=F0=9F=90=9B=20Multi-line=20var?= =?UTF-8?q?=20parsing=20for=20webhook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blocks/[blockId]/executeWebhook.ts | 8 ++++++-- packages/bot-engine/src/services/variable.ts | 16 ++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/apps/viewer/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts b/apps/viewer/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts index 8bfa515ce..79c37d4da 100644 --- a/apps/viewer/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts +++ b/apps/viewer/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts @@ -142,11 +142,15 @@ export const executeWebhook = ...basicAuth, json: contentType !== 'x-www-form-urlencoded' && body - ? safeJsonParse(parseVariables(variables)(body)) + ? safeJsonParse( + parseVariables(variables, { escapeLineBreaks: true })(body) + ) : undefined, form: contentType === 'x-www-form-urlencoded' && body - ? safeJsonParse(parseVariables(variables)(body)) + ? safeJsonParse( + parseVariables(variables, { escapeLineBreaks: true })(body) + ) : undefined, } try { diff --git a/packages/bot-engine/src/services/variable.ts b/packages/bot-engine/src/services/variable.ts index 2f2c1b2ba..365226ae1 100644 --- a/packages/bot-engine/src/services/variable.ts +++ b/packages/bot-engine/src/services/variable.ts @@ -7,7 +7,10 @@ export const stringContainsVariable = (str: string): boolean => export const parseVariables = ( variables: Variable[], - options: { fieldToParse: 'value' | 'id' } = { fieldToParse: 'value' } + options: { fieldToParse?: 'value' | 'id'; escapeLineBreaks?: boolean } = { + fieldToParse: 'value', + escapeLineBreaks: false, + } ) => (text: string | undefined): string => { if (!text || text === '') return '' @@ -17,11 +20,12 @@ export const parseVariables = return matchedVarName === v.name && isDefined(v.value) }) if (!variable) return '' - return ( - (options.fieldToParse === 'value' - ? variable.value?.toString() - : variable.id) || '' - ) + if (options.fieldToParse === 'id') return variable.id + const { value } = variable + if (!value) return '' + if (options.escapeLineBreaks) + return value.toString().replace(/\n/g, '\\n') + return value.toString() }) }