2023-03-14 14:18:05 +01:00
|
|
|
import got from 'got'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
|
2023-08-28 09:13:53 +02:00
|
|
|
import { env } from '@typebot.io/env'
|
2023-03-14 14:18:05 +01:00
|
|
|
|
|
|
|
|
export const sendTelemetryEvents = async (events: TelemetryEvent[]) => {
|
2023-06-06 13:25:13 +02:00
|
|
|
if (events.length === 0) return { message: 'No events to send' }
|
2023-08-28 09:13:53 +02:00
|
|
|
if (!env.TELEMETRY_WEBHOOK_URL) return { message: 'Telemetry not enabled' }
|
2023-03-14 14:18:05 +01:00
|
|
|
|
|
|
|
|
try {
|
2023-08-28 09:13:53 +02:00
|
|
|
await got.post(env.TELEMETRY_WEBHOOK_URL, {
|
2023-03-14 14:18:05 +01:00
|
|
|
json: { events },
|
|
|
|
|
headers: {
|
2023-08-28 09:13:53 +02:00
|
|
|
authorization: env.TELEMETRY_WEBHOOK_BEARER_TOKEN
|
|
|
|
|
? `Bearer ${env.TELEMETRY_WEBHOOK_BEARER_TOKEN}`
|
2023-03-14 14:18:05 +01:00
|
|
|
: undefined,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to send event', err)
|
|
|
|
|
return {
|
|
|
|
|
message: 'Failed to send event',
|
|
|
|
|
error: err instanceof Error ? err.message : 'Unknown error',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
message: 'Event sent',
|
|
|
|
|
}
|
|
|
|
|
}
|