-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcronjobs.js
More file actions
43 lines (38 loc) · 1.43 KB
/
cronjobs.js
File metadata and controls
43 lines (38 loc) · 1.43 KB
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
import { RtmClient, WebClient, CLIENT_EVENTS, RTM_EVENTS } from '@slack/client';
import axios from 'axios';
import express from 'express';
import { messageConfirmation, getQueryParams } from './constants';
import { User, Reminder } from './models';
import moment from 'moment';
const router = express.Router();
const botToken = process.env.SLACK_BOT_TOKEN || '';
const rtm = new RtmClient(botToken);
const channel = 'T6AVBE3GX';
const currentDate = new Date();
const yesterday = new Date(currentDate.getTime());
yesterday.setDate(yesterday.getDate() - 1);
rtm.start();
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, () => {
Reminder.find()
.then((reminders) => {
for (let i = 0; i < reminders.length; i++) {
const reminder = reminders[i];
console.log(reminder.date, currentDate);
if(reminder.date === currentDate.toISOString().slice(0, 10)) {
console.log("Today");
rtm.sendMessage(reminder.subject, reminder.userId);
Reminder.remove({subject: reminder.subject}, (err) => {
if(err) {
console.log(err);
}
});
} else if(reminder.date === yesterday.toISOString().slice(0, 10)) {
console.log("Yesterday");
rtm.sendMessage(reminder.subject, reminder.userId);
}
}
rtm.sendMessage("oi you cheeky bastard", "D69DB5HHP");
require('mongoose').connection.close();
rtm.disconnect();
});
});