-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (48 loc) · 1.71 KB
/
index.js
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
require('dotenv').config();
var nodemailer = require('nodemailer');
const http = require('http');
const { Client } = require("@notionhq/client");
const { Scheduler } = require("./scheduler");
const { getNotionDB, getMailContent, getMailContacts } = require('./notion');
const { processMail } = require('./mailer');
// Setup Notion client
const notion = new Client({
auth: process.env.NOTION_SECRET_KEY,
})
const poll = async (notion, transporter, scheduler) => {
// console.log(scheduler.getJobs());
const mailContent = await getMailContent(notion);
// console.log(mailContent);
const mailContacts = await getMailContacts(notion);
// console.log(mailContacts);
const filteredMail = mailContent.filter(mail =>
mail.status === 'Ready to Publish' || (mail.status === 'Scheduled' && !!mail.date));
if (filteredMail.length === 0) {
console.log("No mail to send");
} else {
console.log("Processing and sending mail...");
processMail(filteredMail, mailContacts, notion, transporter, scheduler);
}
console.log("Done polling process.");
setTimeout(() => poll(notion, transporter, scheduler), 1000 * 30);
}
const transporter = nodemailer.createTransport({
service: process.env.EMAIL_PROVIDER,
auth: {
user: process.env.EMAIL_SENDER,
pass: process.env.EMAIL_PW
}
});
const scheduler = new Scheduler();
const hostname = '0.0.0.0';
const port = process.env.PORT || 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
poll(notion, transporter, scheduler);
// getNotionDB(notion);