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-03-14 14:18:05 +01:00
|
|
|
import { isEmpty, isNotEmpty } from '../utils'
|
|
|
|
|
|
|
|
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-03-14 14:18:05 +01:00
|
|
|
if (isEmpty(process.env.TELEMETRY_WEBHOOK_URL))
|
|
|
|
return { message: 'Telemetry not enabled' }
|
|
|
|
|
|
|
|
try {
|
|
|
|
await got.post(process.env.TELEMETRY_WEBHOOK_URL, {
|
|
|
|
json: { events },
|
|
|
|
headers: {
|
|
|
|
authorization: isNotEmpty(process.env.TELEMETRY_WEBHOOK_BEARER_TOKEN)
|
|
|
|
? `Bearer ${process.env.TELEMETRY_WEBHOOK_BEARER_TOKEN}`
|
|
|
|
: 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',
|
|
|
|
}
|
|
|
|
}
|