-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
43 lines (37 loc) · 1.29 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
require('dotenv').config()
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
const cron = require('node-cron');
const moment = require('moment');
console.log(`Medication Reminder started at ${moment().format('DD/MM/YYYY HH:mm')}`);
// Trigger every day at 20h05 pm
cron.schedule('05 20 * * *', async () => {
// Inital data of dates (start and end date)
let startDate = '08/03/2022'
// Reminds you during 20 days
let endDate = moment(startDate, 'DD/MM/YYYY').add(20, 'days');
console.log(startDate, endDate);
// Set new dates for the new cycle
if (moment().isAfter(endDate)) {
for (let i = 0; i < 100000; i += 1) {
if (moment().isAfter(endDate)) {
// if 20 days have passed, set a pause for 7 days
startDate = moment(startDate, 'DD/MM/YYYY').add(27, 'days').format('DD/MM/YYYY')
endDate = moment(startDate, 'DD/MM/YYYY').add(20, 'days')
} else {
break;
}
}
}
// Send the reminder
if (moment(startDate, 'DD/MM/YYYY').isBefore()) {
client.messages
.create({
body: 'Take your medication',
from: 'MEDREM',
to: process.env.PHONE_NUMBER,
})
.then(message => console.log(message.sid));
}
});