2023-05-26 09:20:22 +02:00
|
|
|
import { byId } from '@typebot.io/lib'
|
|
|
|
import {
|
|
|
|
MakeComBlock,
|
|
|
|
PabblyConnectBlock,
|
2023-11-13 15:27:36 +01:00
|
|
|
ChatLog,
|
2023-05-26 09:20:22 +02:00
|
|
|
VariableWithUnknowValue,
|
2024-02-16 12:04:25 +01:00
|
|
|
HttpRequestBlock,
|
2023-05-26 09:20:22 +02:00
|
|
|
ZapierBlock,
|
|
|
|
} from '@typebot.io/schemas'
|
2023-08-24 07:48:30 +02:00
|
|
|
import { SessionState } from '@typebot.io/schemas/features/chat/sessionState'
|
2023-09-20 15:26:52 +02:00
|
|
|
import { ExecuteIntegrationResponse } from '../../../types'
|
2023-12-13 10:22:02 +01:00
|
|
|
import { parseVariables } from '@typebot.io/variables/parseVariables'
|
|
|
|
import { updateVariablesInSession } from '@typebot.io/variables/updateVariablesInSession'
|
2024-05-14 14:17:40 +02:00
|
|
|
import vm from 'vm'
|
2023-05-26 09:20:22 +02:00
|
|
|
|
2023-07-18 14:31:20 +02:00
|
|
|
type Props = {
|
|
|
|
state: SessionState
|
2024-02-16 12:04:25 +01:00
|
|
|
block: HttpRequestBlock | ZapierBlock | MakeComBlock | PabblyConnectBlock
|
2023-11-13 15:27:36 +01:00
|
|
|
logs?: ChatLog[]
|
2023-07-18 14:31:20 +02:00
|
|
|
response: {
|
2023-05-26 09:20:22 +02:00
|
|
|
statusCode: number
|
|
|
|
data?: unknown
|
2023-07-18 14:31:20 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-26 09:20:22 +02:00
|
|
|
|
2023-07-18 14:31:20 +02:00
|
|
|
export const resumeWebhookExecution = ({
|
|
|
|
state,
|
|
|
|
block,
|
|
|
|
logs = [],
|
|
|
|
response,
|
|
|
|
}: Props): ExecuteIntegrationResponse => {
|
2023-08-24 07:48:30 +02:00
|
|
|
const { typebot } = state.typebotsQueue[0]
|
2023-07-18 14:31:20 +02:00
|
|
|
const status = response.statusCode.toString()
|
|
|
|
const isError = status.startsWith('4') || status.startsWith('5')
|
2023-05-26 09:20:22 +02:00
|
|
|
|
2023-07-18 14:31:20 +02:00
|
|
|
const responseFromClient = logs.length === 0
|
|
|
|
|
|
|
|
if (responseFromClient)
|
|
|
|
logs.push(
|
|
|
|
isError
|
|
|
|
? {
|
|
|
|
status: 'error',
|
|
|
|
description: `Webhook returned error`,
|
|
|
|
details: response.data,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
status: 'success',
|
|
|
|
description: `Webhook executed successfully!`,
|
|
|
|
details: response.data,
|
|
|
|
}
|
|
|
|
)
|
2023-05-26 09:20:22 +02:00
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
const newVariables = block.options?.responseVariableMapping?.reduce<
|
2023-07-18 14:31:20 +02:00
|
|
|
VariableWithUnknowValue[]
|
|
|
|
>((newVariables, varMapping) => {
|
|
|
|
if (!varMapping?.bodyPath || !varMapping.variableId) return newVariables
|
|
|
|
const existingVariable = typebot.variables.find(byId(varMapping.variableId))
|
|
|
|
if (!existingVariable) return newVariables
|
2024-05-14 14:17:40 +02:00
|
|
|
const sandbox = vm.createContext({
|
2024-05-21 11:20:35 +02:00
|
|
|
data: response,
|
2024-05-14 14:17:40 +02:00
|
|
|
})
|
2023-07-18 14:31:20 +02:00
|
|
|
try {
|
2024-05-14 14:17:40 +02:00
|
|
|
const value: unknown = vm.runInContext(
|
2024-05-21 11:20:35 +02:00
|
|
|
`data.${parseVariables(typebot.variables)(varMapping?.bodyPath)}`,
|
2024-05-14 14:17:40 +02:00
|
|
|
sandbox
|
|
|
|
)
|
2023-07-18 14:31:20 +02:00
|
|
|
return [...newVariables, { ...existingVariable, value }]
|
|
|
|
} catch (err) {
|
|
|
|
return newVariables
|
|
|
|
}
|
|
|
|
}, [])
|
2023-11-08 15:34:16 +01:00
|
|
|
if (newVariables && newVariables.length > 0) {
|
2024-05-15 14:24:55 +02:00
|
|
|
const { updatedState, newSetVariableHistory } = updateVariablesInSession({
|
|
|
|
newVariables,
|
|
|
|
state,
|
|
|
|
currentBlockId: block.id,
|
|
|
|
})
|
2023-05-26 09:20:22 +02:00
|
|
|
return {
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
2024-05-15 14:24:55 +02:00
|
|
|
newSessionState: updatedState,
|
|
|
|
newSetVariableHistory,
|
2023-07-18 14:31:20 +02:00
|
|
|
logs,
|
2023-05-26 09:20:22 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-18 14:31:20 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
|
|
|
logs,
|
|
|
|
}
|
|
|
|
}
|