-
Notifications
You must be signed in to change notification settings - Fork 2
/
scheduler.js
62 lines (51 loc) · 2.08 KB
/
scheduler.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
60
61
62
var schedule = require('node-schedule');
var nodemailer = require('nodemailer');
var jsonfile = require('jsonfile');
const { log } = require('./logger');
const config = jsonfile.readFileSync('./config.json')
class scheduler {
//Schedule email reminders
static email(address, date, time, message) {
//Submited Dates must adhere to TimeEx format
var date_array = date.split("-").map(val => Number(val));
var trigger_year = date_array[0];
var trigger_month = date_array[1]-1;
var trigger_day = date_array[2];
//Submited times must adhere to 24-hour format
var time_array = time.split(":").map(val => Number(val));
var trigger_hour = time_array[0];
var trigger_minute = time_array[1];
var trigger = new Date(trigger_year,trigger_month,trigger_day,trigger_hour, trigger_minute);
schedule.scheduleJob(trigger, function(){
//Email logic
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: config.email.address,
pass: config.email.password
},
tls: {
rejectUnauthorized: false
}
});
//We need a dialog that builds this
let HelperOptions = {
from: `BotCaptain <${config.email.address}>`,
to: `${address}`,
subject: 'BotCaptain',
text: `${message}`
};
transporter.sendMail(HelperOptions, (error, info) => {
if (error) {
log.error(`[ERROR] Error occurred while sending mail: ${error}.`)
}
log.info(`[INFO] Email sent to ${address}.`)
log.info(info)
});
}
);
}
}
exports.scheduler = scheduler;