Skip to content

Commit

Permalink
🐛 Make sure variables are properly overwritten
Browse files Browse the repository at this point in the history
And also correctly mark result as completed with new engine
  • Loading branch information
baptisteArno committed Feb 21, 2023
1 parent 83ae57c commit 148315f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import {
Typebot,
Variable,
} from 'models'
import { continueBotFlow, getSession, startBotFlow } from '../utils'
import {
continueBotFlow,
getSession,
setResultAsCompleted,
startBotFlow,
} from '../utils'
import { omit } from 'utils'

export const sendMessageProcedure = publicProcedure
Expand Down Expand Up @@ -77,6 +82,9 @@ export const sendMessageProcedure = publicProcedure
},
})

if (!input && session.state.result?.hasStarted)
await setResultAsCompleted(session.state.result.id)

return {
messages,
input,
Expand Down
62 changes: 29 additions & 33 deletions apps/viewer/src/features/chat/api/utils/continueBotFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
validatePhoneNumber,
} from '@/features/blocks/inputs/phone/api'
import { validateUrl } from '@/features/blocks/inputs/url/api'
import { parseVariables } from '@/features/variables'
import { parseVariables, updateVariables } from '@/features/variables'
import prisma from '@/lib/prisma'
import { TRPCError } from '@trpc/server'
import got from 'got'
Expand All @@ -17,7 +17,6 @@ import {
InputBlock,
InputBlockType,
SessionState,
Variable,
} from 'models'
import { isInputBlock, isNotDefined } from 'utils'
import { executeGroup } from './executeGroup'
Expand Down Expand Up @@ -58,19 +57,11 @@ export const continueBotFlow =
if (formattedReply && !isReplyValid(formattedReply, block))
return parseRetryMessage(block)

const newVariables = await processAndSaveAnswer(
const newSessionState = await processAndSaveAnswer(
state,
block
)(formattedReply)

const newSessionState = {
...state,
typebot: {
...state.typebot,
variables: newVariables,
},
}

const groupHasMoreBlocks = blockIndex < group.blocks.length - 1

const nextEdgeId = getOutgoingEdgeId(newSessionState)(block, formattedReply)
Expand All @@ -93,38 +84,36 @@ export const continueBotFlow =
}

const processAndSaveAnswer =
(
state: Pick<SessionState, 'result' | 'typebot' | 'isPreview'>,
block: InputBlock
) =>
async (reply: string | null): Promise<Variable[]> => {
if (!reply) return state.typebot.variables
(state: SessionState, block: InputBlock) =>
async (reply: string | null): Promise<SessionState> => {
if (!reply) return state
if (!state.isPreview && state.result) {
await saveAnswer(state.result.id, block)(reply)
if (!state.result.hasStarted) await setResultAsStarted(state.result.id)
}
const newVariables = saveVariableValueIfAny(state, block)(reply)
return newVariables
const newState = await saveVariableValueIfAny(state, block)(reply)
return {
...newState,
result: newState.result
? { ...newState.result, hasStarted: true }
: undefined,
}
}

const saveVariableValueIfAny =
(state: Pick<SessionState, 'result' | 'typebot'>, block: InputBlock) =>
(reply: string): Variable[] => {
if (!block.options.variableId) return state.typebot.variables
const variable = state.typebot.variables.find(
(state: SessionState, block: InputBlock) =>
async (reply: string): Promise<SessionState> => {
if (!block.options.variableId) return state
const foundVariable = state.typebot.variables.find(
(variable) => variable.id === block.options.variableId
)
if (!variable) return state.typebot.variables
if (!foundVariable) return state

return [
...state.typebot.variables.filter(
(variable) => variable.id !== block.options.variableId
),
{
...variable,
value: reply,
},
]
const newSessionState = await updateVariables(state)([
{ ...foundVariable, value: reply },
])

return newSessionState
}

const setResultAsStarted = async (resultId: string) => {
Expand All @@ -134,6 +123,13 @@ const setResultAsStarted = async (resultId: string) => {
})
}

export const setResultAsCompleted = async (resultId: string) => {
await prisma.result.update({
where: { id: resultId },
data: { isCompleted: true },
})
}

const parseRetryMessage = (
block: InputBlock
): Pick<ChatReply, 'messages' | 'input'> => {
Expand Down

3 comments on commit 148315f

@vercel
Copy link

@vercel vercel bot commented on 148315f Feb 21, 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 148315f Feb 21, 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

myrentalhost.com
stan.vselise.com
start.taxtree.io
typebot.aloe.bot
voicehelp.cr8.ai
zap.fundviser.in
app.chatforms.net
bot.hostnation.de
bot.maitempah.com
bot.phuonghub.com
bot.reviewzer.com
bot.rihabilita.it
cares.urlabout.me
chat.gaswadern.de
fmm.wpwakanda.com
gentleman-shop.fr
k1.kandabrand.com
lb.ticketfute.com
ov1.wpwakanda.com
ov2.wpwakanda.com
ov3.wpwakanda.com
support.triplo.ai
viewer.typebot.io
1988.bouclidom.com
andreimayer.com.br
bot.danyservice.it
bot.iconicbrows.it
bot.megafox.com.br
bot.neferlopez.com
bots.robomotion.io
cadu.uninta.edu.br
dicanatural.online
digitalhelp.com.au
goalsettingbot.com
pant.maxbot.com.br
positivobra.com.br
survey.digienge.io
this-is-a-test.com
zap.techadviser.in
bot.boston-voip.com
bot.cabinpromos.com
bot.digitalbled.com
bot.dsignagency.com
bot.eventhub.com.au
bot.jepierre.com.br
bot.ltmidias.com.br
bbutton.wpwakanda.com
bot.coachayongzul.com
bot.digitalpointer.id
bot.eikju.photography
bot.incusservices.com
bot.meuesocial.com.br
bot.mycompany.reviews
bot.outstandbrand.com
bot.hotelplayarimini.it
bot.upgradesolutions.eu
bots.baptiste-arnaud.fr
help.comebackreward.com
link.venturasuceder.com
mainmenu.diddancing.com
manualhandlingcourse.ie
register.kandabrand.com
signup.hypemarketing.in
subfooter.wpwakanda.com
survey.hypemarketing.in
testbot.afterorigin.com
typebot.influencer.love
91181264.your-access.one
ai.chromebookstoreph.com
form.sergiolimajr.com.br
hunterbot.saleshunter.ai
invite.bridesquadapp.com
link.cascadigital.com.br
onboarding.growthside.io
reward.onlinebotdemo.xyz
stap.venturemarketing.in
type.opaulovieira.com.br
aibot.angrybranding.co.uk
bot.aidigitalmarketing.kr
bot.amicidisanfaustino.it
bot.arraesecenteno.com.br
bot.blackboxsports.com.br
bot.cabinrentalagency.com
bot.fusionstarreviews.com
boyfriend-breakup.riku.ai
brigadeirosemdrama.com.br
chat.ertcrebateportal.com
chat.thehomebuyersusa.com
chat.thisiscrushhouse.com
forms.hiabhaykulkarni.com
healthandsafetycourses.uk
sellmyharleylouisiana.com
testbot.sharemyreview.net
typebot-viewer.vercel.app
verfica.botmachine.com.br
bot.adventureconsulting.hu
bot2.fusionstarreviews.com
casestudyemb.wpwakanda.com
chat.atlasoutfittersk9.com
configurator.bouclidom.com

@vercel
Copy link

@vercel vercel bot commented on 148315f Feb 21, 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.vercel.app
docs-git-main-typebot-io.vercel.app
docs.typebot.io

Please sign in to comment.