2
0

♻️ (api) Auto start bot if starting with input

Closes #877, closes #884
This commit is contained in:
Baptiste Arnaud
2023-10-04 16:47:58 +02:00
parent 2bc9dfb503
commit 9e6a1f7dc0
11 changed files with 180 additions and 39 deletions

View File

@@ -28,6 +28,7 @@ import { validateRatingReply } from './blocks/inputs/rating/validateRatingReply'
import { parsePictureChoicesReply } from './blocks/inputs/pictureChoice/parsePictureChoicesReply'
import { parseVariables } from './variables/parseVariables'
import { updateVariablesInSession } from './variables/updateVariablesInSession'
import { startBotFlow } from './startBotFlow'
import { TRPCError } from '@trpc/server'
export const continueBotFlow =
@@ -36,6 +37,9 @@ export const continueBotFlow =
reply?: string
): Promise<ChatReply & { newSessionState: SessionState }> => {
let newSessionState = { ...state }
if (!newSessionState.currentBlock) return startBotFlow(state)
const group = state.typebotsQueue[0].typebot.groups.find(
(group) => group.id === state.currentBlock?.groupId
)

View File

@@ -26,14 +26,19 @@ import { startBotFlow } from './startBotFlow'
import { prefillVariables } from './variables/prefillVariables'
import { deepParseVariables } from './variables/deepParseVariables'
import { injectVariablesFromExistingResult } from './variables/injectVariablesFromExistingResult'
import { getNextGroup } from './getNextGroup'
import { upsertResult } from './queries/upsertResult'
import { continueBotFlow } from './continueBotFlow'
type Props = {
message: string | undefined
startParams: StartParams
userId: string | undefined
initialSessionState?: Pick<SessionState, 'whatsApp' | 'expiryTimeout'>
}
export const startSession = async ({
message,
startParams,
userId,
initialSessionState,
@@ -130,13 +135,39 @@ export const startSession = async ({
}
}
let chatReply = await startBotFlow(initialState, startParams.startGroupId)
// If params has message and first block is an input block, we can directly continue the bot flow
if (message) {
const firstEdgeId =
chatReply.newSessionState.typebotsQueue[0].typebot.groups[0].blocks[0]
.outgoingEdgeId
const nextGroup = await getNextGroup(chatReply.newSessionState)(firstEdgeId)
const newSessionState = nextGroup.newSessionState
const firstBlock = nextGroup.group?.blocks.at(0)
if (firstBlock && isInputBlock(firstBlock)) {
const resultId = newSessionState.typebotsQueue[0].resultId
if (resultId)
await upsertResult({
hasStarted: true,
isCompleted: false,
resultId,
typebot: newSessionState.typebotsQueue[0].typebot,
})
chatReply = await continueBotFlow({
...newSessionState,
currentBlock: { groupId: firstBlock.groupId, blockId: firstBlock.id },
})(message)
}
}
const {
messages,
input,
clientSideActions: startFlowClientActions,
newSessionState,
logs,
} = await startBotFlow(initialState, startParams.startGroupId)
} = chatReply
const clientSideActions = startFlowClientActions ?? []

View File

@@ -12,7 +12,6 @@ import { decrypt } from '@typebot.io/lib/api'
import { saveStateToDatabase } from '../saveStateToDatabase'
import prisma from '@typebot.io/lib/prisma'
import { isDefined } from '@typebot.io/lib/utils'
import { startBotFlow } from '../startBotFlow'
type Props = {
receivedMessage: WhatsAppIncomingMessage
@@ -65,9 +64,9 @@ export const resumeWhatsAppFlow = async ({
const resumeResponse =
session && !isSessionExpired
? session.state.currentBlock
? await continueBotFlow(session.state)(messageContent)
: await startBotFlow({ ...session.state, whatsApp: { contact } })
? await continueBotFlow({ ...session.state, whatsApp: { contact } })(
messageContent
)
: workspaceId
? await startWhatsAppSession({
incomingMessage: messageContent,

View File

@@ -12,11 +12,8 @@ import {
WhatsAppCredentials,
defaultSessionExpiryTimeout,
} from '@typebot.io/schemas/features/whatsapp'
import { isInputBlock, isNotDefined } from '@typebot.io/lib/utils'
import { isNotDefined } from '@typebot.io/lib/utils'
import { startSession } from '../startSession'
import { getNextGroup } from '../getNextGroup'
import { continueBotFlow } from '../continueBotFlow'
import { upsertResult } from '../queries/upsertResult'
type Props = {
incomingMessage?: string
@@ -80,7 +77,8 @@ export const startWhatsAppSession = async ({
publicTypebot.settings.whatsApp?.sessionExpiryTimeout ??
defaultSessionExpiryTimeout
let chatReply = await startSession({
return startSession({
message: incomingMessage,
startParams: {
typebot: publicTypebot.typebot.publicId as string,
},
@@ -92,31 +90,6 @@ export const startWhatsAppSession = async ({
expiryTimeout: sessionExpiryTimeoutHours * 60 * 60 * 1000,
},
})
let sessionState: SessionState = chatReply.newSessionState
// If first block is an input block, we can directly continue the bot flow
const firstEdgeId =
sessionState.typebotsQueue[0].typebot.groups[0].blocks[0].outgoingEdgeId
const nextGroup = await getNextGroup(sessionState)(firstEdgeId)
sessionState = nextGroup.newSessionState
const firstBlock = nextGroup.group?.blocks.at(0)
if (firstBlock && isInputBlock(firstBlock)) {
const resultId = sessionState.typebotsQueue[0].resultId
if (resultId)
await upsertResult({
hasStarted: true,
isCompleted: false,
resultId,
typebot: sessionState.typebotsQueue[0].typebot,
})
chatReply = await continueBotFlow({
...sessionState,
currentBlock: { groupId: firstBlock.groupId, blockId: firstBlock.id },
})(incomingMessage)
}
return chatReply
}
export const messageMatchStartCondition = (