Skip to content

Commit

Permalink
🐛 (webhook) Fix variable list parsing in custom body
Browse files Browse the repository at this point in the history
Closes #687
  • Loading branch information
baptisteArno committed Aug 18, 2023
1 parent a4f7f8f commit ed77f5d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ export const WebhookAdvancedConfigForm = ({
</AccordionButton>
<AccordionPanel pt="4">
<TableList<VariableForTest>
initialItems={
options?.variablesForTest ?? { byId: {}, allIds: [] }
}
initialItems={options?.variablesForTest ?? []}
onItemsChange={updateVariablesForTest}
Item={VariableForTestInputs}
addLabel="Add an entry"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { safeStringify } from '@typebot.io/lib/safeStringify'
import { isDefined } from '@typebot.io/lib/utils'
import { Variable, VariableForTest } from '@typebot.io/schemas'

export const convertVariablesForTestToVariables = (
Expand All @@ -13,7 +15,19 @@ export const convertVariablesForTestToVariables = (
const variable = variables.find(
(v) => v.id === variableForTest.variableId
) as Variable
return { ...variable, value: variableForTest.value }
return { ...variable, value: parseVariableValue(variableForTest.value) }
}, {}),
]
}

const parseVariableValue = (value: string | undefined): string | string[] => {
if (!value) return ''
try {
const parsedValue = JSON.parse(value)
if (Array.isArray(parsedValue))
return parsedValue.map(safeStringify).filter(isDefined)
return safeStringify(parsedValue) ?? value
} catch (error) {
return value
}
}
12 changes: 10 additions & 2 deletions apps/viewer/src/features/chat/helpers/continueBotFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export const continueBotFlow =
const existingVariable = state.typebot.variables.find(
byId(block.options.variableId)
)
if (existingVariable) {
if (existingVariable && reply) {
const newVariable = {
...existingVariable,
value: reply,
value: safeJsonParse(reply),
}
newSessionState = updateVariables(state)([newVariable])
}
Expand Down Expand Up @@ -318,3 +318,11 @@ export const isReplyValid = (inputValue: string, block: Block): boolean => {

export const canSkip = (inputType: InputBlockType) =>
inputType === InputBlockType.FILE

export const safeJsonParse = (value: string): unknown => {
try {
return JSON.parse(value)
} catch {
return value
}
}
6 changes: 4 additions & 2 deletions apps/viewer/src/features/variables/parseVariables.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isDefined } from '@typebot.io/lib'
import { Variable, VariableWithValue } from '@typebot.io/schemas'
import { safeStringify } from './safeStringify'
import { safeStringify } from '@typebot.io/lib/safeStringify'

export type ParseVariablesOptions = {
fieldToParse?: 'value' | 'id'
Expand Down Expand Up @@ -42,7 +42,9 @@ export const parseVariables =
if (options.escapeForJson)
return (
dollarSign +
jsonParse(typeof value !== 'string' ? JSON.stringify(value) : value)
(typeof value === 'string'
? jsonParse(value)
: JSON.stringify(value))
)
const parsedValue =
dollarSign +
Expand Down
2 changes: 1 addition & 1 deletion apps/viewer/src/features/variables/prefillVariables.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StartParams, Variable } from '@typebot.io/schemas'
import { safeStringify } from './safeStringify'
import { safeStringify } from '@typebot.io/lib/safeStringify'

export const prefillVariables = (
variables: Variable[],
Expand Down
2 changes: 1 addition & 1 deletion apps/viewer/src/features/variables/updateVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
VariableWithValue,
Variable,
} from '@typebot.io/schemas'
import { safeStringify } from './safeStringify'
import { safeStringify } from '@typebot.io/lib/safeStringify'

export const updateVariables =
(state: SessionState) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNotDefined } from '@typebot.io/lib'
import type { ScriptToExecute } from '@typebot.io/schemas'
import { safeStringify } from '@typebot.io/lib/safeStringify'

// eslint-disable-next-line @typescript-eslint/no-empty-function
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
Expand All @@ -15,23 +15,12 @@ export const executeSetVariable = async ({
)
const replyToSend = await func(...args.map((arg) => arg.value))
return {
replyToSend: safeStringify(replyToSend),
replyToSend: safeStringify(replyToSend) ?? undefined,
}
} catch (err) {
console.error(err)
return {
replyToSend: safeStringify(content),
replyToSend: safeStringify(content) ?? undefined,
}
}
}

export const safeStringify = (val: unknown): string | undefined => {
if (isNotDefined(val)) return
if (typeof val === 'string') return val
try {
return JSON.stringify(val)
} catch {
console.warn('Failed to safely stringify variable value', val)
return
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNotDefined } from '@typebot.io/lib'
import { isNotDefined } from './utils'

export const safeStringify = (val: unknown): string | null => {
if (isNotDefined(val)) return null
Expand Down

4 comments on commit ed77f5d

@vercel
Copy link

@vercel vercel bot commented on ed77f5d Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

sellmycarglasgow.com
stephanesampa.online
superglicemia.com.br
talkbot.agfunnel.com
tenorioadvogados.com
uppity.wpwakanda.com
83701274.21000000.lol
87186327.21000000.one
90945247.21000000.one
97320578.21000000.one
98650901.21000000.one
abutton.wpwakanda.com
acelera.maxbot.com.br
aidigitalmarketing.kr
atendimento.vrauu.com
bbutton.wpwakanda.com
bot.anovaerarb.online
bot.coachayongzul.com
bot.digitalpointer.id
bot.eikju.photography
bot.incusservices.com
bot.mejoralasalud.fun
bot.meuesocial.com.br
bot.mycompany.reviews
bot.outstandbrand.com
bot.ramonmatos.com.br
bot.robertohairlab.it
bot.sharemyreview.net
bot.synapsegameia.com
bot.truongnguyen.live
bots.baptistearno.com
botz.cloudsiteapp.com
cdd.searchcube.com.sg
chat.missarkansas.org
chatbot.ownacademy.co
chats.maisefetivo.com
claudio-barros.online
criar.somaperuzzo.com
gotasafrodisiacas.com
homerun.wpwakanda.com
mdb.assessoria.ademir
portaldasanalises.com
prenota.aldoemaria.it
revistasaudeemdia.com
rossano.thegymgame.it
sbutton.wpwakanda.com
segredosdothreads.com
talk.convobuilder.com
terrosdoscassinos.com
test.leadbooster.help
whats.laracardoso.com
www.acesso-app.online
www.hemertonsilva.com
zillabot.saaszilla.co
815639944.21000000.one
83720273.bouclidom.com
aplicacao.bmind.com.br
apply.ansuraniphone.my
bbutton.wpwwakanda.com
viewer-v2-typebot-io.vercel.app
register.thailandmicespecialist.com
mdb.assessoria.desideri.progenbr.com
mdb.assessoria.fernanda.progenbr.com
mdb.assessoria.jbatista.progenbr.com
mdb.assessoria.mauricio.progenbr.com
mdb.evento.autocadastro.progenbr.com
form.shopmercedesbenzsouthorlando.com
mdb.evento.equipeinterna.progenbr.com
bot.studiotecnicoimmobiliaremerelli.it
mdb.assessoria.boaventura.progenbr.com
mdb.assessoria.jtrebesqui.progenbr.com
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
gabinete.baleia.formulario.progenbr.com
mdb.assessoria.carreirinha.progenbr.com
chrome-os-inquiry-system.itschromeos.com
mdb.assessoria.paulomarques.progenbr.com
viewer-v2-git-main-typebot-io.vercel.app
main-menu-for-itschromeos.itschromeos.com
mdb.assessoria.qrcode.ademir.progenbr.com
mdb.assessoria.qrcode.arthur.progenbr.com
mdb.assessoria.qrcode.danilo.progenbr.com
mdb.assessoria.qrcode.marcao.progenbr.com
mdb.assessoria.qrcode.marcio.progenbr.com
mdb.assessoria.qrcode.aloisio.progenbr.com
mdb.assessoria.qrcode.girotto.progenbr.com
mdb.assessoria.qrcode.marinho.progenbr.com
mdb.assessoria.qrcode.rodrigo.progenbr.com
mdb.assessoria.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.desideri.progenbr.com
mdb.assessoria.qrcode.fernanda.progenbr.com
mdb.assessoria.qrcode.jbatista.progenbr.com
mdb.assessoria.qrcode.mauricio.progenbr.com
mdb.assessoria.fernanda.regional.progenbr.com
mdb.assessoria.qrcode.boaventura.progenbr.com
mdb.assessoria.qrcode.jtrebesqui.progenbr.com
mdb.assessoria.qrcode.carreirinha.progenbr.com
mdb.assessoria.qrcode.paulomarques.progenbr.com
mdb.assessoria.qrcode.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.fernanda.regional.progenbr.com

@vercel
Copy link

@vercel vercel bot commented on ed77f5d Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on ed77f5d Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs.typebot.io
docs-git-main-typebot-io.vercel.app
docs-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ed77f5d Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-typebot-io.vercel.app
app.typebot.io
builder-v2-git-main-typebot-io.vercel.app

Please sign in to comment.