From d4728e6972882e09ead61214367a35c5c6feafef Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Tue, 29 Dec 2020 00:44:19 +0100 Subject: Added newsletter --- newsletter/send | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100755 newsletter/send (limited to 'newsletter/send') diff --git a/newsletter/send b/newsletter/send new file mode 100755 index 0000000..66bdeb9 --- /dev/null +++ b/newsletter/send @@ -0,0 +1,129 @@ +#!/usr/bin/env node + +const fs = require('fs'); +const path = require('path'); +const axios = require('axios'); +const dayjs = require('dayjs'); +const weekOfYear = require('dayjs/plugin/weekOfYear'); +const handlebars = require('handlebars'); +const yesno = require('yesno'); +const meow = require('meow'); +const { reverse } = require('dns'); + +dayjs.extend(weekOfYear); + +(async function () { + + const cli = meow(` + Usage + $ ./send + + Options + --production, -p Uses production mailing list + + Examples + $ ./send campaigns/2020-51.json --production + `, { + flags: { + production: { + type: 'boolean', + alias: 'p' + } + } + }); + + if (cli.input.length === 0) { + console.log('You must provide campagin JSON file.'); + process.exit(1); + } + + const campaignJSONFile = cli.input[0]; + const year = path.basename(cli.input[0]).split('.')[0].split('-')[0]; + const week = path.basename(cli.input[0]).split('.')[0].split('-')[1]; + const from = { name: 'Mitja Felicijan', email: 'weekly@mitjafelicijan.com' }; + const mailingList = cli.flags.production + ? { env: 'production', id: 'b03de6fc-ff95-40a0-8707-c0706b3c0b31' } + : { env: 'testing', id: '2bbcdedb-49d8-48f3-9f33-df6e04c9e5bf' }; + + const headers = { + 'Authorization': 'Bearer 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', + 'Content-Type': 'application/json', + }; + + // gets current week + let campaign = null; + try { + campaign = require(`./${campaignJSONFile}`); + } catch (err) { + console.error(err); + process.exit(1); + } + + // gets list subscribers + const personalizations = []; + const contacts = await axios.get('https://api.sendgrid.com/v3/marketing/contacts', { headers: headers }).catch(error => { console.log(error) }); + if (contacts) { + for (const contact of contacts.data.result) { + if (contact.list_ids.includes(mailingList.id)) { + personalizations.push({ to: [{ email: contact.email }] }); + } + } + } + + // gets handlebars template contents + let template = null; + try { + template = handlebars.compile(fs.readFileSync('templates/mailing.hbs', 'utf8')); + template = template({ title: `${year}: Week #${week} Links`, campaign }); + } catch (e) { + console.error(err); + process.exit(1); + } + + fs.writeFileSync(`generated/${year}-${week}.html`, template); + + // asks for user input to allow sending emails + console.log(`\nWill send to ${personalizations.length} subscribers from list "${mailingList.env}":`) + for (const subscriber of personalizations) { + console.log(' - ', subscriber.to[0].email) + } + + const consent = await yesno({ + question: '\nAre you sure you want to continue?' + }); + + if (consent) { + // send actual emails + await axios.post('https://api.sendgrid.com/v3/mail/send', { + from, + subject: `Week #${week} Links`, + personalizations, + content: [{ + type: 'text/html', + value: template + }] + }, { headers: headers }).catch(error => { console.log(error) }); + } + + console.log('\nAnd we are done.\n'); + + // generates index file + + const HTMLFiles = fs.readdirSync('./generated/'); + const indexFile = HTMLFiles.indexOf('index.html'); + if (indexFile > -1) HTMLFiles.splice(indexFile, 1); + + HTMLFiles.forEach((item, idx) => { + const parts = item.split('.')[0].split('-'); + HTMLFiles[idx] = { + year: parts[0], + week: parts[1], + file: item, + } + }); + + let indexTemplate = handlebars.compile(fs.readFileSync('templates/index.hbs', 'utf8')); + indexTemplate = indexTemplate({ files: HTMLFiles.reverse() }); + fs.writeFileSync(`generated/index.html`, indexTemplate); + +}()); -- cgit v1.2.3