-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathairtable-email.mjs
183 lines (155 loc) · 6.02 KB
/
airtable-email.mjs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/** @typedef { import("./airtable").AirtableRecordResults } AirtableRecordResults */
import got from "got";
import { SendEmailCommand, SESClient } from "@aws-sdk/client-ses";
const AIRTABLE_API_KEY = process.env.AIRTABLE_API_KEY;
const AIRTABLE_BASE_ID = process.env.AIRTABLE_BASE_ID;
const AIRTABLE_TABLE_NAME = process.env.AIRTABLE_TABLE_NAME;
const client = new SESClient({
region: process.env.EMAIL_SES_REGION,
credentials: {
accessKeyId: process.env.EMAIL_SES_ACCESS_KEY_ID,
secretAccessKey: process.env.EMAIL_SES_SECRET_ACCESS_KEY,
},
});
/**
* Find all applicants on our Airtable that are in the "Out"
* column, i.e., manually marked as rejected, and send them
* a rejection email automatically.
*/
const airtableEmail = async () => {
console.log("Starting Airtable email script");
// Ensure that API key and table is provided
if (!AIRTABLE_API_KEY) throw new Error("API key not found");
/**
* List of Airtable records
* @type {AirtableRecordResults}
*/
const result = await got(
`https://api.airtable.com/v0/${AIRTABLE_BASE_ID}/${encodeURIComponent(
AIRTABLE_TABLE_NAME
)}?maxRecords=100&filterByFormula=${encodeURIComponent(
// All applicants with the `Status` of "Out"
"{Status} = 'Out'"
)}`,
{ headers: { Authorization: `Bearer ${AIRTABLE_API_KEY}` } }
).json();
console.log(`Sending ${result.records.length} emails`);
// Loop through each and send the email
let i = 0;
for await (const item of result.records) {
i++;
if (
// Ensure records has a position
!item.fields.Position ||
// Ensure record has an email
!item.fields.Email ||
// Ensure record has a name
!item.fields.Name ||
// Ensure email is valid
// @link https://stackoverflow.com/a/9204568/1656944
!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(item.fields.Email)
) {
// Move this item to "Emailed out" and update comment
await got.patch(
`https://api.airtable.com/v0/${AIRTABLE_BASE_ID}/${encodeURIComponent(
AIRTABLE_TABLE_NAME
)}/${item.id}`,
{
headers: { Authorization: `Bearer ${AIRTABLE_API_KEY}` },
json: {
fields: {
Status: "Emailed out",
Notes: `${
item.fields.Notes || ""
}\n\nPabio Escobar: I didn't find a name, position, or valid email, so I skipped emailing this applicant.`.trim(),
},
},
}
);
console.log(`Skipped invalid entry ${i}/${result.records.length}`);
continue;
}
const firstName = `${item.fields.Name.charAt(
0
).toUpperCase()}${item.fields.Name.slice(1).toLowerCase()}`.split(" ")[0];
// Send email via AWS SES
await client.send(
new SendEmailCommand({
Destination: {
ToAddresses: [
// Transform name to title case
`"${item.fields.Name.trim().replace(
/\w\S*/g,
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
)}" <${item.fields.Email}>`,
],
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: `<p>Hi ${firstName},</p>
<p>Thank you for your application to the <strong>${
item.fields.Position
}</strong> role at <a href="https://pabio.com?utm_source=recruiting&utm_medium=email&utm_campaign=application_follow_up&utm_content=${encodeURIComponent(
item.fields.Position.toLowerCase()
.replace(/[^\w ]+/g, "")
.replace(/ +/g, "_")
)}&utm_term=${item.id}">Pabio</a>.</p>
<p>We've carefully reviewed your background and experience, and have decided not to proceed with your application at this time.</p>
<p>Thanks again for your application, and we'll keep you in mind for any future openings that may be a better match for your skills and experience.</p>
<p>Best of luck!</p>
<p>Your friends from Pabio (<a href="https://pabio.com?utm_source=recruiting&utm_medium=email&utm_campaign=application_follow_up&utm_content=${encodeURIComponent(
item.fields.Position.toLowerCase()
.replace(/[^\w ]+/g, "")
.replace(/ +/g, "_")
)}&utm_term=${item.id}">https://pabio.com</a>)</p>
<p style="font-size: 80%; color: #999">--</p>
<p style="font-size: 80%; color: #999">You're receiving this email because you applied for the position of ${
item.fields.Position
} on ${new Date(item.createdTime).toLocaleDateString()}.</p>
`,
},
Text: {
Charset: "UTF-8",
Data: `Hi ${firstName},
Thank you for your application to the ${item.fields.Position} role at Pabio.
We've carefully reviewed your background and experience, and have decided not to proceed with your application at this time.
Thanks again for your application, and we'll keep you in mind for any future openings that may be a better match for your skills and experience.
Best of luck!
Your friends from Pabio (https://pabio.com)
--
You're receiving this email because you applied for the position of ${
item.fields.Position
} on ${new Date(item.createdTime).toLocaleDateString()}.
`,
},
},
Subject: {
Charset: "UTF-8",
Data: "Following up on your recent application to Pabio",
},
},
Source: "Pabio Careers <[email protected]>",
})
);
// Move this item to "Emailed out"
await got.patch(
`https://api.airtable.com/v0/${AIRTABLE_BASE_ID}/${encodeURIComponent(
AIRTABLE_TABLE_NAME
)}/${item.id}`,
{
headers: { Authorization: `Bearer ${AIRTABLE_API_KEY}` },
json: { fields: { Status: "Emailed out" } },
}
);
console.log(`Sent email ${i}/${result.records.length}`);
}
console.log("Completed Airtable email script");
};
airtableEmail()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});