-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
72 lines (59 loc) · 1.98 KB
/
index.ts
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
72
import { App, LogLevel } from '@slack/bolt';
import { isWeekend, getRandomEmoji, fetchMorningGif } from './utils';
import botDMs from './utils/botDMs';
import { isDayOff } from './utils/dayOff';
import { config } from 'dotenv';
config();
const { SLACK_BOT_TOKEN, SLACK_USER_TOKEN, SLACK_SIGNING_SECRET, SLACK_APP_TOKEN, NODE_ENV } = process.env;
const app = new App({
token: SLACK_BOT_TOKEN,
signingSecret: SLACK_SIGNING_SECRET,
socketMode: true,
appToken: SLACK_APP_TOKEN,
logLevel: NODE_ENV === 'production' ? LogLevel.INFO : LogLevel.DEBUG
});
(async () => {
await app.start();
await botDMs(app);
console.info(`🔥 Slack Bolt app is running! 🔥`);
})();
/*********************
Scheduled Good Morning
**********************/
const sendMessage = async ({ channel = 'general', as_user = true } = {}) => {
const gif = await fetchMorningGif();
await app.client.chat.postMessage({
token: as_user ? SLACK_USER_TOKEN : SLACK_BOT_TOKEN,
channel,
text: `Good morning! ${getRandomEmoji()}\n ${gif}`,
as_user
});
};
let isDayOffToday = false;
let isSentToday = false;
let prevDay = new Date().getUTCDay();
(function loop() {
setTimeout(() => {
const currentDay = new Date().getUTCDay();
const hour = new Date().getUTCHours();
const isMorning = hour === 5; //* 5am UTC => 8am local
if (prevDay !== currentDay) {
//* Reset all flags on new day
isSentToday = false;
prevDay = currentDay;
isDayOffToday = false;
}
//* Optionally: add !isDayOffToday check in the if
//* statement to not even enter the if, if it's a day off
if (!isSentToday && !isWeekend() && isMorning) {
isDayOffToday = isDayOffToday || isDayOff(); //* A cool way to check only once a day
if (!isDayOffToday) {
sendMessage();
isSentToday = true;
console.info('!!! Sent a good morning message !!!');
}
}
console.info({ isSentToday, prevDay, currentDay });
loop();
}, 1000 * 60 * 30 * Math.random());
})();