-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
62 lines (54 loc) · 1.9 KB
/
server.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
//node packages
const cron = require("node-cron");
const axios = require("axios");
const Sentry = require("@sentry/node");
require("dotenv").config();
//local packages
const { app } = require("./utilities/bolt.js");
const { getTodaysChores } = require("./utilities/getChores.js");
const { sendNoChores, postToSlack } = require("./utilities/notify.js");
const { markChoreDone } = require("./utilities/markChoreDone.js");
//package configuration
//local configuration
//globals
const CRON_SCHEDULE = process.env.CRON_SCHEDULE;
const WHITEBOARD_CRON_SCHEDULE = process.env.WHITEBOARD_CRON_SCHEDULE;
const WHITEBOARD_SERVER_URL = process.env.WHITEBOARD_SERVER_URL;
const ENABLE = process.env.ENABLE == "true" ? true : false;
// Configure Sentry exception logging
if (process.env.SENTRY_DSN) {
const sentryConfig = {
dsn: process.env.SENTRY_DSN,
release: `chorebot@${require("./package.json").version}`
};
if (process.env.ENVIRONMENT) sentryConfig.environment = process.env.ENVIRONMENT;
Sentry.init(sentryConfig);
}
//helper functions
const runChores = async () => {
console.log("Running ChoreBot!");
const chores = await getTodaysChores();
if (chores === -1) sendNoChores();
// TODO: in the future, notify the officers that there are no chores
else postToSlack(chores);
};
app.action(
/^\d+$/,
async ({ ack, next }) => {
ack();
next();
},
async ({ action, body }) => {
if (!action || !body || !body.user || !body.channel || !body.message) return;
markChoreDone(action.action_id, body.user.id, body.channel.id, body.message.ts,
body.message.blocks);
}
);
if (ENABLE) {
cron.schedule(CRON_SCHEDULE, runChores);
cron.schedule(WHITEBOARD_CRON_SCHEDULE, async () => {
let chores = await getTodaysChores();
chores = chores === -1 ? {chores: []} : { chores };
axios.post(`${WHITEBOARD_SERVER_URL}/chores`, chores).catch((err) => console.error(err));
});
}