-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
71 lines (58 loc) · 1.76 KB
/
bot.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
63
64
65
66
67
68
69
70
71
const app = require("./app");
const cron = require("node-cron");
const {
startOfToday,
setYear,
addDays,
} = require("date-fns");
const User = require("./models/user");
// Messages Handlers
const start = require("./messages/start");
const wish = require("./messages/wish");
const wishlist = require("./messages/wishlist");
const help = require("./messages/help");
// Actions Handlers
const setBirthday = require("./actions/set_birthday");
const deleteWishFromList = require("./actions/delete_wish_from_list");
// Commands Handlers
const wishlistCommand = require("./commands/wishlist");
// Middlewares
const globalMiddleware = require("./middlewares/global");
app.use(globalMiddleware);
/**
* Add Empty Middleware to the list
* Otherwise previous one doesn't work correctly
* There is some bug
*/
app.use(() => {});
app.message("start", start);
app.message(/^wish\s(.*)/, wish);
app.message("wishlist", wishlist);
app.message("help", help);
app.action("set_birthday", setBirthday);
app.action("delete_wish_from_list", deleteWishFromList);
app.command("/wishlist", wishlistCommand);
/**
* Cron job that tries to find users who
* has a birthday in two weeks and send them notification
* once a day
*/
cron.schedule("0 14 * * 1-5", async () => {
const inTwoWeeks = addDays(setYear(startOfToday(), 1990), 14);
const users = await User.find({
birthDate: inTwoWeeks
});
users.forEach(async user => {
await app.client.chat.postMessage({
token: process.env.SLACK_BOT_USER_TOKEN,
channel: user.slackImChannel,
text:
"Hey, your birthday is in 2 weeks. Please add some items to your wishlist!"
});
});
});
(async () => {
// Start your app
await app.start(process.env.PORT || 3000);
console.log("⚡️ Bolt app is running!");
})();