Skip to content

Commit

Permalink
🚸 (sendEmail) Improve variable parsing in sendEmail body
Browse files Browse the repository at this point in the history
Support for multi line variables as well

Closes baptisteArno#749
  • Loading branch information
baptisteArno authored and jmgoncalves97 committed Jan 17, 2025
1 parent 441471e commit 4cb9cbe
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ export const executeSendEmailBlock = async (
],
}

const body =
findUniqueVariableValue(typebot.variables)(options.body)?.toString() ??
parseVariables(typebot.variables, { escapeHtml: true })(options.body ?? '')
const bodyUniqueVariable = findUniqueVariableValue(typebot.variables)(
options.body
)
const body = bodyUniqueVariable
? stringifyUniqueVariableValueAsHtml(bodyUniqueVariable)
: parseVariables(typebot.variables, { isInsideHtml: true })(
options.body ?? ''
)

try {
const sendEmailLogs = await sendEmail({
Expand Down Expand Up @@ -258,3 +263,11 @@ const getFileUrls =
if (typeof fileUrls === 'string') return fileUrls
return fileUrls.filter(isDefined)
}

const stringifyUniqueVariableValueAsHtml = (
value: Variable['value']
): string => {
if (!value) return ''
if (typeof value === 'string') return value.replace(/\n/g, '<br />')
return value.map(stringifyUniqueVariableValueAsHtml).join('<br />')
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const parseWebhookAttributes =
bodyContent && webhook.method !== HttpMethod.GET
? safeJsonParse(
parseVariables(typebot.variables, {
escapeForJson: !checkIfBodyIsAVariable(bodyContent),
isInsideJson: !checkIfBodyIsAVariable(bodyContent),
})(bodyContent)
)
: { data: undefined, isJson: false }
Expand Down
38 changes: 20 additions & 18 deletions apps/viewer/src/features/variables/parseVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { safeStringify } from '@typebot.io/lib/safeStringify'

export type ParseVariablesOptions = {
fieldToParse?: 'value' | 'id'
escapeForJson?: boolean
isInsideJson?: boolean
takeLatestIfList?: boolean
escapeHtml?: boolean
isInsideHtml?: boolean
}

export const defaultParseVariablesOptions: ParseVariablesOptions = {
fieldToParse: 'value',
escapeForJson: false,
isInsideJson: false,
takeLatestIfList: false,
escapeHtml: false,
isInsideHtml: false,
}

export const parseVariables =
Expand All @@ -39,13 +39,8 @@ export const parseVariables =
if (!variable) return dollarSign + ''
if (options.fieldToParse === 'id') return dollarSign + variable.id
const { value } = variable
if (options.escapeForJson)
return (
dollarSign +
(typeof value === 'string'
? jsonParse(value)
: JSON.stringify(value))
)
if (options.isInsideJson)
return dollarSign + parseVariableValueInJson(value)
const parsedValue =
dollarSign +
safeStringify(
Expand All @@ -54,15 +49,22 @@ export const parseVariables =
: value
)
if (!parsedValue) return dollarSign + ''
if (options.escapeHtml)
return parsedValue.replace(/</g, '&lt;').replace(/>/g, '&gt;')
if (options.isInsideHtml) return parseVariableValueInHtml(parsedValue)
return parsedValue
}
)
}

const jsonParse = (str: string) =>
str
.replace(/\n/g, `\\n`)
.replace(/"/g, `\\"`)
.replace(/\\[^n"]/g, `\\\\ `)
const parseVariableValueInJson = (value: VariableWithValue['value']) => {
const stringifiedValue = JSON.stringify(value)
if (typeof value === 'string') return stringifiedValue.slice(1, -1)
return stringifiedValue
}

const parseVariableValueInHtml = (
value: VariableWithValue['value']
): string => {
if (typeof value === 'string')
return value.replace(/</g, '&lt;').replace(/>/g, '&gt;')
return JSON.stringify(value).replace(/</g, '&lt;').replace(/>/g, '&gt;')
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const executeWebhook =
bodyContent && webhook.method !== HttpMethod.GET
? safeJsonParse(
parseVariables(variables, {
escapeForJson: !checkIfBodyIsAVariable(bodyContent),
isInsideJson: !checkIfBodyIsAVariable(bodyContent),
})(bodyContent)
)
: { data: undefined, isJson: false }
Expand Down
7 changes: 7 additions & 0 deletions packages/emails/src/emails/DefaultBotNotificationEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export const DefaultBotNotificationEmail = ({
<b>{key}</b>:{' '}
{isEmail ? (
<a href={`mailto:${answers[key]}`}>{answers[key]}</a>
) : answers[key].includes('\n') ? (
answers[key].split('\n').map((line) => (
<>
{line}
<br />
</>
))
) : (
answers[key]
)}
Expand Down

0 comments on commit 4cb9cbe

Please sign in to comment.