From c124671682930fca9bbaace5a007ea0aaba32f15 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 27 Jul 2023 10:09:33 +0200 Subject: [PATCH] :wrench: Add import contact to brevo script --- packages/scripts/importContactToBrevo.ts | 47 ++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 packages/scripts/importContactToBrevo.ts diff --git a/packages/scripts/importContactToBrevo.ts b/packages/scripts/importContactToBrevo.ts new file mode 100644 index 000000000..eb9d30ae1 --- /dev/null +++ b/packages/scripts/importContactToBrevo.ts @@ -0,0 +1,47 @@ +import { PrismaClient } from '@typebot.io/prisma' +import { promptAndSetEnvironment } from './utils' +import got, { HTTPError } from 'got' +import { readFileSync } from 'fs' + +const importContactToBrevo = async () => { + await promptAndSetEnvironment() + const prisma = new PrismaClient({ + log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'], + }) + + prisma.$on('query', (e) => { + console.log(e.query) + console.log(e.params) + console.log(e.duration, 'ms') + }) + const users = JSON.parse(readFileSync('users.json').toString()) as { + email: string + name: string + }[] + console.log('Inserting users', users.length) + try { + await got.post('https://api.brevo.com/v3/contacts/import', { + headers: { + 'api-key': process.env.BREVO_API_KEY, + }, + json: { + listIds: [16], + updateExistingContacts: true, + jsonBody: users.map((user) => ({ + email: user.email, + attributes: { + FIRSTNAME: user.name ? user.name.split(' ')[0] : undefined, + }, + })), + }, + }) + } catch (err) { + if (err instanceof HTTPError) { + console.log(err.response.body) + return + } + console.log(err) + } +} + +importContactToBrevo()