2022-02-22 06:55:15 +01:00
|
|
|
import { ResultValues } from 'contexts/AnswersContext'
|
2022-01-18 18:25:18 +01:00
|
|
|
import {
|
|
|
|
IntegrationStep,
|
|
|
|
IntegrationStepType,
|
|
|
|
GoogleSheetsStep,
|
|
|
|
GoogleSheetsAction,
|
|
|
|
GoogleSheetsInsertRowOptions,
|
|
|
|
Variable,
|
|
|
|
GoogleSheetsUpdateRowOptions,
|
|
|
|
Cell,
|
|
|
|
GoogleSheetsGetOptions,
|
2022-01-19 14:25:15 +01:00
|
|
|
GoogleAnalyticsStep,
|
2022-01-22 18:24:57 +01:00
|
|
|
WebhookStep,
|
2022-02-07 18:06:37 +01:00
|
|
|
SendEmailStep,
|
2022-02-22 06:55:15 +01:00
|
|
|
PublicBlock,
|
2022-01-18 18:25:18 +01:00
|
|
|
} from 'models'
|
|
|
|
import { stringify } from 'qs'
|
2022-02-22 06:55:15 +01:00
|
|
|
import { parseAnswers, sendRequest } from 'utils'
|
2022-01-19 14:25:15 +01:00
|
|
|
import { sendGaEvent } from '../../lib/gtag'
|
2022-02-22 06:55:15 +01:00
|
|
|
import { sendErrorMessage, sendInfoMessage } from './postMessage'
|
2022-01-19 14:25:15 +01:00
|
|
|
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-02-10 14:31:44 +01:00
|
|
|
type IntegrationContext = {
|
|
|
|
apiHost: string
|
|
|
|
typebotId: string
|
2022-02-15 12:36:08 +01:00
|
|
|
blockId: string
|
|
|
|
stepId: string
|
2022-02-13 07:49:56 +01:00
|
|
|
isPreview: boolean
|
2022-02-10 14:31:44 +01:00
|
|
|
variables: Variable[]
|
2022-02-22 06:55:15 +01:00
|
|
|
resultValues: ResultValues
|
|
|
|
blocks: PublicBlock[]
|
2022-01-18 18:25:18 +01:00
|
|
|
updateVariableValue: (variableId: string, value: string) => void
|
2022-02-10 14:31:44 +01:00
|
|
|
}
|
2022-02-22 06:55:15 +01:00
|
|
|
|
2022-02-10 14:31:44 +01:00
|
|
|
export const executeIntegration = ({
|
|
|
|
step,
|
|
|
|
context,
|
|
|
|
}: {
|
|
|
|
step: IntegrationStep
|
|
|
|
context: IntegrationContext
|
|
|
|
}): Promise<string | undefined> => {
|
2022-01-18 18:25:18 +01:00
|
|
|
switch (step.type) {
|
|
|
|
case IntegrationStepType.GOOGLE_SHEETS:
|
2022-02-10 14:31:44 +01:00
|
|
|
return executeGoogleSheetIntegration(step, context)
|
2022-01-19 14:25:15 +01:00
|
|
|
case IntegrationStepType.GOOGLE_ANALYTICS:
|
2022-02-10 14:31:44 +01:00
|
|
|
return executeGoogleAnalyticsIntegration(step, context)
|
2022-01-22 18:24:57 +01:00
|
|
|
case IntegrationStepType.WEBHOOK:
|
2022-02-10 14:31:44 +01:00
|
|
|
return executeWebhook(step, context)
|
2022-02-07 18:06:37 +01:00
|
|
|
case IntegrationStepType.EMAIL:
|
2022-02-10 14:31:44 +01:00
|
|
|
return sendEmail(step, context)
|
2022-01-18 18:25:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 14:25:15 +01:00
|
|
|
export const executeGoogleAnalyticsIntegration = async (
|
|
|
|
step: GoogleAnalyticsStep,
|
2022-02-10 14:31:44 +01:00
|
|
|
{ variables }: IntegrationContext
|
2022-01-19 14:25:15 +01:00
|
|
|
) => {
|
2022-02-07 18:06:37 +01:00
|
|
|
if (!step.options?.trackingId) return step.outgoingEdgeId
|
2022-01-19 14:25:15 +01:00
|
|
|
const { default: initGoogleAnalytics } = await import('../../lib/gtag')
|
|
|
|
await initGoogleAnalytics(step.options.trackingId)
|
|
|
|
sendGaEvent(parseVariablesInObject(step.options, variables))
|
2022-02-07 18:06:37 +01:00
|
|
|
return step.outgoingEdgeId
|
2022-01-19 14:25:15 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 18:25:18 +01:00
|
|
|
const executeGoogleSheetIntegration = async (
|
|
|
|
step: GoogleSheetsStep,
|
2022-02-10 14:31:44 +01:00
|
|
|
context: IntegrationContext
|
2022-01-18 18:25:18 +01:00
|
|
|
) => {
|
2022-02-04 19:00:08 +01:00
|
|
|
if (!('action' in step.options)) return step.outgoingEdgeId
|
2022-02-02 08:05:02 +01:00
|
|
|
switch (step.options.action) {
|
2022-01-18 18:25:18 +01:00
|
|
|
case GoogleSheetsAction.INSERT_ROW:
|
2022-02-10 14:31:44 +01:00
|
|
|
await insertRowInGoogleSheets(step.options, context)
|
2022-01-18 18:25:18 +01:00
|
|
|
break
|
|
|
|
case GoogleSheetsAction.UPDATE_ROW:
|
2022-02-10 14:31:44 +01:00
|
|
|
await updateRowInGoogleSheets(step.options, context)
|
2022-01-18 18:25:18 +01:00
|
|
|
break
|
|
|
|
case GoogleSheetsAction.GET:
|
2022-02-10 14:31:44 +01:00
|
|
|
await getRowFromGoogleSheets(step.options, context)
|
2022-01-18 18:25:18 +01:00
|
|
|
break
|
|
|
|
}
|
2022-02-04 19:00:08 +01:00
|
|
|
return step.outgoingEdgeId
|
2022-01-18 18:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const insertRowInGoogleSheets = async (
|
|
|
|
options: GoogleSheetsInsertRowOptions,
|
2022-02-10 14:31:44 +01:00
|
|
|
{ variables, apiHost }: IntegrationContext
|
2022-01-18 18:25:18 +01:00
|
|
|
) => {
|
|
|
|
if (!options.cellsToInsert) return
|
|
|
|
return sendRequest({
|
2022-02-10 14:31:44 +01:00
|
|
|
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
|
2022-01-18 18:25:18 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
credentialsId: options.credentialsId,
|
|
|
|
values: parseCellValues(options.cellsToInsert, variables),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateRowInGoogleSheets = async (
|
|
|
|
options: GoogleSheetsUpdateRowOptions,
|
2022-02-10 14:31:44 +01:00
|
|
|
{ variables, apiHost }: IntegrationContext
|
2022-01-18 18:25:18 +01:00
|
|
|
) => {
|
|
|
|
if (!options.cellsToUpsert || !options.referenceCell) return
|
|
|
|
return sendRequest({
|
2022-02-10 14:31:44 +01:00
|
|
|
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
|
2022-01-18 18:25:18 +01:00
|
|
|
method: 'PATCH',
|
|
|
|
body: {
|
|
|
|
credentialsId: options.credentialsId,
|
|
|
|
values: parseCellValues(options.cellsToUpsert, variables),
|
|
|
|
referenceCell: {
|
|
|
|
column: options.referenceCell.column,
|
2022-02-07 18:06:37 +01:00
|
|
|
value: parseVariables(variables)(options.referenceCell.value ?? ''),
|
2022-01-18 18:25:18 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const getRowFromGoogleSheets = async (
|
|
|
|
options: GoogleSheetsGetOptions,
|
2022-02-10 14:31:44 +01:00
|
|
|
{ variables, updateVariableValue, apiHost }: IntegrationContext
|
2022-01-18 18:25:18 +01:00
|
|
|
) => {
|
|
|
|
if (!options.referenceCell || !options.cellsToExtract) return
|
|
|
|
const queryParams = stringify(
|
|
|
|
{
|
|
|
|
credentialsId: options.credentialsId,
|
|
|
|
referenceCell: {
|
|
|
|
column: options.referenceCell.column,
|
2022-02-07 18:06:37 +01:00
|
|
|
value: parseVariables(variables)(options.referenceCell.value ?? ''),
|
2022-01-18 18:25:18 +01:00
|
|
|
},
|
2022-02-04 19:00:08 +01:00
|
|
|
columns: options.cellsToExtract.map((cell) => cell.column),
|
2022-01-18 18:25:18 +01:00
|
|
|
},
|
|
|
|
{ indices: false }
|
|
|
|
)
|
|
|
|
const { data } = await sendRequest<{ [key: string]: string }>({
|
2022-02-10 14:31:44 +01:00
|
|
|
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}?${queryParams}`,
|
2022-01-18 18:25:18 +01:00
|
|
|
method: 'GET',
|
|
|
|
})
|
|
|
|
if (!data) return
|
2022-02-04 19:00:08 +01:00
|
|
|
options.cellsToExtract.forEach((cell) =>
|
2022-01-18 18:25:18 +01:00
|
|
|
updateVariableValue(cell.variableId ?? '', data[cell.column ?? ''])
|
2022-02-04 19:00:08 +01:00
|
|
|
)
|
2022-01-18 18:25:18 +01:00
|
|
|
}
|
|
|
|
const parseCellValues = (
|
2022-02-04 19:00:08 +01:00
|
|
|
cells: Cell[],
|
|
|
|
variables: Variable[]
|
2022-01-18 18:25:18 +01:00
|
|
|
): { [key: string]: string } =>
|
2022-02-04 19:00:08 +01:00
|
|
|
cells.reduce((row, cell) => {
|
2022-01-18 18:25:18 +01:00
|
|
|
return !cell.column || !cell.value
|
|
|
|
? row
|
2022-01-19 14:25:15 +01:00
|
|
|
: {
|
|
|
|
...row,
|
2022-02-07 18:06:37 +01:00
|
|
|
[cell.column]: parseVariables(variables)(cell.value),
|
2022-01-19 14:25:15 +01:00
|
|
|
}
|
2022-01-18 18:25:18 +01:00
|
|
|
}, {})
|
2022-01-22 18:24:57 +01:00
|
|
|
|
|
|
|
const executeWebhook = async (
|
|
|
|
step: WebhookStep,
|
2022-02-10 14:31:44 +01:00
|
|
|
{
|
2022-02-15 12:36:08 +01:00
|
|
|
blockId,
|
|
|
|
stepId,
|
2022-02-10 14:31:44 +01:00
|
|
|
variables,
|
|
|
|
updateVariableValue,
|
|
|
|
typebotId,
|
|
|
|
apiHost,
|
|
|
|
}: IntegrationContext
|
2022-01-22 18:24:57 +01:00
|
|
|
) => {
|
2022-02-04 19:00:08 +01:00
|
|
|
if (!step.webhook) return step.outgoingEdgeId
|
2022-01-22 18:24:57 +01:00
|
|
|
const { data, error } = await sendRequest({
|
2022-02-15 12:36:08 +01:00
|
|
|
url: `${apiHost}/api/typebots/${typebotId}/blocks/${blockId}/steps/${stepId}/executeWebhook`,
|
2022-01-22 18:24:57 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
variables,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
console.error(error)
|
2022-02-04 19:00:08 +01:00
|
|
|
step.options.responseVariableMapping.forEach((varMapping) => {
|
2022-01-22 18:24:57 +01:00
|
|
|
if (!varMapping?.bodyPath || !varMapping.variableId) return
|
|
|
|
const value = safeEval(`(${JSON.stringify(data)}).${varMapping?.bodyPath}`)
|
|
|
|
updateVariableValue(varMapping.variableId, value)
|
|
|
|
})
|
|
|
|
}
|
2022-02-07 18:06:37 +01:00
|
|
|
|
2022-02-10 14:31:44 +01:00
|
|
|
const sendEmail = async (
|
|
|
|
step: SendEmailStep,
|
2022-02-22 06:55:15 +01:00
|
|
|
{ variables, apiHost, isPreview, resultValues, blocks }: IntegrationContext
|
2022-02-10 14:31:44 +01:00
|
|
|
) => {
|
2022-02-13 07:49:56 +01:00
|
|
|
if (isPreview) sendInfoMessage('Emails are not sent in preview mode')
|
|
|
|
if (isPreview) return step.outgoingEdgeId
|
2022-02-07 18:06:37 +01:00
|
|
|
const { options } = step
|
|
|
|
const { error } = await sendRequest({
|
2022-02-10 14:31:44 +01:00
|
|
|
url: `${apiHost}/api/integrations/email`,
|
2022-02-07 18:06:37 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
credentialsId: options.credentialsId,
|
|
|
|
recipients: options.recipients.map(parseVariables(variables)),
|
|
|
|
subject: parseVariables(variables)(options.subject ?? ''),
|
2022-02-22 06:55:15 +01:00
|
|
|
body:
|
|
|
|
options.body === '{{state}}'
|
|
|
|
? parseAnswers({ variables, blocks })(resultValues)
|
|
|
|
: parseVariables(variables)(options.body ?? ''),
|
2022-02-19 10:58:56 +01:00
|
|
|
cc: (options.cc ?? []).map(parseVariables(variables)),
|
|
|
|
bcc: (options.bcc ?? []).map(parseVariables(variables)),
|
2022-02-07 18:06:37 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
console.error(error)
|
2022-02-22 06:55:15 +01:00
|
|
|
if (isPreview && error) sendErrorMessage(`Webhook failed: ${error.message}`)
|
2022-02-07 18:06:37 +01:00
|
|
|
return step.outgoingEdgeId
|
|
|
|
}
|