2022-01-18 18:25:18 +01:00
|
|
|
import {
|
|
|
|
IntegrationStep,
|
|
|
|
IntegrationStepType,
|
|
|
|
GoogleSheetsStep,
|
|
|
|
GoogleSheetsAction,
|
|
|
|
GoogleSheetsInsertRowOptions,
|
|
|
|
Variable,
|
|
|
|
Table,
|
|
|
|
GoogleSheetsUpdateRowOptions,
|
|
|
|
Cell,
|
|
|
|
GoogleSheetsGetOptions,
|
2022-01-19 14:25:15 +01:00
|
|
|
GoogleAnalyticsStep,
|
2022-01-22 18:24:57 +01:00
|
|
|
WebhookStep,
|
2022-01-18 18:25:18 +01:00
|
|
|
} from 'models'
|
|
|
|
import { stringify } from 'qs'
|
|
|
|
import { sendRequest } from 'utils'
|
2022-01-19 14:25:15 +01:00
|
|
|
import { sendGaEvent } from '../../lib/gtag'
|
|
|
|
import { parseVariables, parseVariablesInObject } from './variable'
|
2022-01-18 18:25:18 +01:00
|
|
|
|
2022-01-22 18:24:57 +01:00
|
|
|
const safeEval = eval
|
|
|
|
|
2022-01-18 18:25:18 +01:00
|
|
|
export const executeIntegration = (
|
2022-01-22 18:24:57 +01:00
|
|
|
typebotId: string,
|
2022-01-18 18:25:18 +01:00
|
|
|
step: IntegrationStep,
|
|
|
|
variables: Table<Variable>,
|
|
|
|
updateVariableValue: (variableId: string, value: string) => void
|
|
|
|
) => {
|
|
|
|
switch (step.type) {
|
|
|
|
case IntegrationStepType.GOOGLE_SHEETS:
|
|
|
|
return executeGoogleSheetIntegration(step, variables, updateVariableValue)
|
2022-01-19 14:25:15 +01:00
|
|
|
case IntegrationStepType.GOOGLE_ANALYTICS:
|
|
|
|
return executeGoogleAnalyticsIntegration(step, variables)
|
2022-01-22 18:24:57 +01:00
|
|
|
case IntegrationStepType.WEBHOOK:
|
|
|
|
return executeWebhook(typebotId, step, variables, updateVariableValue)
|
2022-01-18 18:25:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 14:25:15 +01:00
|
|
|
export const executeGoogleAnalyticsIntegration = async (
|
|
|
|
step: GoogleAnalyticsStep,
|
|
|
|
variables: Table<Variable>
|
|
|
|
) => {
|
|
|
|
if (!step.options?.trackingId) return
|
|
|
|
const { default: initGoogleAnalytics } = await import('../../lib/gtag')
|
|
|
|
await initGoogleAnalytics(step.options.trackingId)
|
|
|
|
sendGaEvent(parseVariablesInObject(step.options, variables))
|
|
|
|
}
|
|
|
|
|
2022-01-18 18:25:18 +01:00
|
|
|
const executeGoogleSheetIntegration = async (
|
|
|
|
step: GoogleSheetsStep,
|
|
|
|
variables: Table<Variable>,
|
|
|
|
updateVariableValue: (variableId: string, value: string) => void
|
|
|
|
) => {
|
2022-01-19 18:54:49 +01:00
|
|
|
if (!step.options) return step.edgeId
|
2022-01-18 18:25:18 +01:00
|
|
|
switch (step.options?.action) {
|
|
|
|
case GoogleSheetsAction.INSERT_ROW:
|
|
|
|
await insertRowInGoogleSheets(step.options, variables)
|
|
|
|
break
|
|
|
|
case GoogleSheetsAction.UPDATE_ROW:
|
|
|
|
await updateRowInGoogleSheets(step.options, variables)
|
|
|
|
break
|
|
|
|
case GoogleSheetsAction.GET:
|
|
|
|
await getRowFromGoogleSheets(step.options, variables, updateVariableValue)
|
|
|
|
break
|
|
|
|
}
|
2022-01-19 18:54:49 +01:00
|
|
|
return step.edgeId
|
2022-01-18 18:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const insertRowInGoogleSheets = async (
|
|
|
|
options: GoogleSheetsInsertRowOptions,
|
|
|
|
variables: Table<Variable>
|
|
|
|
) => {
|
|
|
|
if (!options.cellsToInsert) return
|
|
|
|
return sendRequest({
|
|
|
|
url: `http://localhost:3001/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
credentialsId: options.credentialsId,
|
|
|
|
values: parseCellValues(options.cellsToInsert, variables),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateRowInGoogleSheets = async (
|
|
|
|
options: GoogleSheetsUpdateRowOptions,
|
|
|
|
variables: Table<Variable>
|
|
|
|
) => {
|
|
|
|
if (!options.cellsToUpsert || !options.referenceCell) return
|
|
|
|
return sendRequest({
|
|
|
|
url: `http://localhost:3001/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
|
|
|
|
method: 'PATCH',
|
|
|
|
body: {
|
|
|
|
credentialsId: options.credentialsId,
|
|
|
|
values: parseCellValues(options.cellsToUpsert, variables),
|
|
|
|
referenceCell: {
|
|
|
|
column: options.referenceCell.column,
|
2022-01-19 14:25:15 +01:00
|
|
|
value: parseVariables({
|
|
|
|
text: options.referenceCell.value ?? '',
|
|
|
|
variables,
|
|
|
|
}),
|
2022-01-18 18:25:18 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const getRowFromGoogleSheets = async (
|
|
|
|
options: GoogleSheetsGetOptions,
|
|
|
|
variables: Table<Variable>,
|
|
|
|
updateVariableValue: (variableId: string, value: string) => void
|
|
|
|
) => {
|
|
|
|
if (!options.referenceCell || !options.cellsToExtract) return
|
|
|
|
const queryParams = stringify(
|
|
|
|
{
|
|
|
|
credentialsId: options.credentialsId,
|
|
|
|
referenceCell: {
|
|
|
|
column: options.referenceCell.column,
|
2022-01-19 14:25:15 +01:00
|
|
|
value: parseVariables({
|
|
|
|
text: options.referenceCell.value ?? '',
|
|
|
|
variables,
|
|
|
|
}),
|
2022-01-18 18:25:18 +01:00
|
|
|
},
|
|
|
|
columns: options.cellsToExtract.allIds.map(
|
|
|
|
(id) => options.cellsToExtract?.byId[id].column
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{ indices: false }
|
|
|
|
)
|
|
|
|
const { data } = await sendRequest<{ [key: string]: string }>({
|
|
|
|
url: `http://localhost:3001/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}?${queryParams}`,
|
|
|
|
method: 'GET',
|
|
|
|
})
|
|
|
|
if (!data) return
|
|
|
|
options.cellsToExtract.allIds.forEach((cellId) => {
|
|
|
|
const cell = options.cellsToExtract?.byId[cellId]
|
|
|
|
if (!cell) return
|
|
|
|
updateVariableValue(cell.variableId ?? '', data[cell.column ?? ''])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const parseCellValues = (
|
|
|
|
cells: Table<Cell>,
|
|
|
|
variables: Table<Variable>
|
|
|
|
): { [key: string]: string } =>
|
|
|
|
cells.allIds.reduce((row, id) => {
|
|
|
|
const cell = cells.byId[id]
|
|
|
|
return !cell.column || !cell.value
|
|
|
|
? row
|
2022-01-19 14:25:15 +01:00
|
|
|
: {
|
|
|
|
...row,
|
|
|
|
[cell.column]: parseVariables({ text: cell.value, variables }),
|
|
|
|
}
|
2022-01-18 18:25:18 +01:00
|
|
|
}, {})
|
2022-01-22 18:24:57 +01:00
|
|
|
|
|
|
|
const executeWebhook = async (
|
|
|
|
typebotId: string,
|
|
|
|
step: WebhookStep,
|
|
|
|
variables: Table<Variable>,
|
|
|
|
updateVariableValue: (variableId: string, value: string) => void
|
|
|
|
) => {
|
|
|
|
if (!step.options?.webhookId) return step.edgeId
|
|
|
|
const { data, error } = await sendRequest({
|
|
|
|
url: `http://localhost:3000/api/typebots/${typebotId}/webhooks/${step.options?.webhookId}/execute`,
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
variables,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
console.error(error)
|
|
|
|
step.options.responseVariableMapping?.allIds.forEach((varMappingId) => {
|
|
|
|
const varMapping = step.options?.responseVariableMapping?.byId[varMappingId]
|
|
|
|
if (!varMapping?.bodyPath || !varMapping.variableId) return
|
|
|
|
const value = safeEval(`(${JSON.stringify(data)}).${varMapping?.bodyPath}`)
|
|
|
|
updateVariableValue(varMapping.variableId, value)
|
|
|
|
})
|
|
|
|
}
|