chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { webhookPayloadSchema } from 'src/schemas'
|
||||
import { recordCreated, recordUpdated, recordDeleted } from '../events'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export function safeJsonParse(x: any) {
|
||||
try {
|
||||
return { data: JSON.parse(x), success: true }
|
||||
} catch {
|
||||
return { data: x, success: false }
|
||||
}
|
||||
}
|
||||
|
||||
export const handler: bp.IntegrationProps['handler'] = async ({ req, logger, client }) => {
|
||||
if (!req.body) {
|
||||
const errorMsg = 'Webhook processing failed: empty body'
|
||||
logger.forBot().error(errorMsg)
|
||||
return {
|
||||
status: 400,
|
||||
body: JSON.stringify({ error: errorMsg }),
|
||||
}
|
||||
}
|
||||
|
||||
// Parse and validate the webhook payload
|
||||
const webhookData = safeJsonParse(req.body)
|
||||
if (!webhookData.success) {
|
||||
const errorMsg = 'Webhook processing failed: invalid JSON'
|
||||
logger.forBot().error(errorMsg)
|
||||
return { status: 400, body: JSON.stringify({ error: errorMsg }) }
|
||||
}
|
||||
|
||||
const parseResult = webhookPayloadSchema.safeParse(webhookData.data)
|
||||
if (!parseResult.success) {
|
||||
const errorMsg = `Webhook processing failed: ${parseResult.error.message}`
|
||||
logger.forBot().error(errorMsg)
|
||||
return { status: 400, body: JSON.stringify({ error: errorMsg }) }
|
||||
}
|
||||
|
||||
const { webhook_id, events } = parseResult.data
|
||||
logger.forBot().info(`Processing webhook ${webhook_id} with ${events.length} events`)
|
||||
|
||||
// Process each event
|
||||
for (const attioEvent of events) {
|
||||
const event = attioEvent.event_type
|
||||
|
||||
switch (event) {
|
||||
case 'record.created':
|
||||
await recordCreated({ payload: attioEvent, client, logger })
|
||||
break
|
||||
case 'record.updated':
|
||||
await recordUpdated({ payload: attioEvent, client, logger })
|
||||
break
|
||||
case 'record.deleted':
|
||||
await recordDeleted({ payload: attioEvent, client, logger })
|
||||
break
|
||||
default:
|
||||
logger.forBot().warn(`Unsupported event type: ${event}`)
|
||||
}
|
||||
}
|
||||
|
||||
return { status: 200 }
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export { register, unregister } from './setup'
|
||||
export { handler } from './handler'
|
||||
@@ -0,0 +1,76 @@
|
||||
import * as sdk from '@botpress/sdk'
|
||||
import { AttioApiClient } from '../attio-api'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const register: bp.IntegrationProps['register'] = async ({ ctx, client, webhookUrl, logger }) => {
|
||||
try {
|
||||
const accessToken = ctx.configuration.accessToken
|
||||
const attioClient = new AttioApiClient(accessToken)
|
||||
|
||||
// Test the connection using the API client
|
||||
logger.forBot().info('Testing connection to Attio...')
|
||||
await attioClient.testConnection()
|
||||
logger.forBot().info('Connection to Attio successful')
|
||||
|
||||
// Check if webhooks already exist
|
||||
logger.forBot().info('Checking if webhooks already exist...')
|
||||
const { state } = await client.getOrSetState({
|
||||
name: 'attioIntegrationInfo',
|
||||
type: 'integration',
|
||||
id: ctx.integrationId,
|
||||
payload: {
|
||||
attioWebhookId: '',
|
||||
},
|
||||
})
|
||||
|
||||
if (!state.payload.attioWebhookId) {
|
||||
logger.forBot().info('Webhooks do not exist. Creating webhooks...')
|
||||
const webhookResp = await attioClient.createWebhook({
|
||||
data: {
|
||||
target_url: webhookUrl,
|
||||
subscriptions: [
|
||||
{ event_type: 'record.created', filter: null },
|
||||
{ event_type: 'record.updated', filter: null },
|
||||
{ event_type: 'record.deleted', filter: null },
|
||||
],
|
||||
},
|
||||
})
|
||||
const attioWebhookId = String(webhookResp.data.id.webhook_id)
|
||||
await client.setState({
|
||||
type: 'integration',
|
||||
name: 'attioIntegrationInfo',
|
||||
id: ctx.integrationId,
|
||||
payload: { attioWebhookId },
|
||||
})
|
||||
}
|
||||
|
||||
logger.forBot().info('Webhooks created')
|
||||
} catch (thrown) {
|
||||
const error = thrown instanceof Error ? thrown : new Error(String(thrown))
|
||||
throw new sdk.RuntimeError(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
export const unregister: bp.IntegrationProps['unregister'] = async ({ ctx, client, logger }) => {
|
||||
try {
|
||||
const accessToken = ctx.configuration.accessToken
|
||||
const attioClient = new AttioApiClient(accessToken)
|
||||
|
||||
const stateAttioIntegrationInfo = await client.getState({
|
||||
id: ctx.integrationId,
|
||||
name: 'attioIntegrationInfo',
|
||||
type: 'integration',
|
||||
})
|
||||
|
||||
const { state } = stateAttioIntegrationInfo
|
||||
const { attioWebhookId } = state.payload
|
||||
|
||||
if (attioWebhookId) {
|
||||
await attioClient.deleteWebhook(attioWebhookId)
|
||||
logger.forBot().info('Webhook successfully deleted')
|
||||
}
|
||||
} catch (thrown) {
|
||||
const error = thrown instanceof Error ? thrown : new Error(String(thrown))
|
||||
throw new sdk.RuntimeError(error.message)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user