1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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 <input>
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);
}());
|