forked from McCambley/slack-updates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (61 loc) · 2.13 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
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
#!/usr/bin/env node
// @ts-check
const { WebClient } = require("@slack/web-api");
const path = require("path");
const dotenv = require("dotenv");
dotenv.config({ path: path.resolve(__dirname, ".env") });
// Capture command-line arguments:
// process.argv[0] => 'node'
// process.argv[1] => 'updateSlackStatus.js'
// process.argv[2] => statusText
// process.argv[3] => statusEmoji
// process.argv[4] => statusExpiration (in hours) (0 will not expire)
// process.argv[5] => doNotDisturb (0 | 1)
const [
,
,
statusText = "Away",
statusEmoji = ":away:",
statusExpiration = "0", // Hours
doNotDisturb = "0",
] = process.argv;
// Slack token should be stored securely, here we read from an environment variable
const token = process.env.SLACK_TOKEN;
if (!token) {
console.error("Error: SLACK_TOKEN environment variable is missing.");
process.exit(1);
}
// Create a new WebClient instance with your token
const web = new WebClient(token);
(async () => {
try {
const expirationTime = parseFloat(statusExpiration) || 0;
const statusExpirationAdjusted = expirationTime
? Math.floor(Date.now() / 1000 + expirationTime * 60 * 60)
: 0;
const result = await web.users.profile.set({
profile: {
status_text: statusText,
status_emoji: statusEmoji,
status_expiration: statusExpirationAdjusted,
},
});
const snooze = await web.dnd.setSnooze({
num_minutes: parseInt(doNotDisturb) ? expirationTime * 60 : 0,
token,
});
console.log("Status updated successfully:");
console.log(` Text: ${result.profile?.status_text}`);
console.log(` Emoji: ${result.profile?.status_emoji}`);
console.log(` Expiration time: ${result.profile?.status_expiration}`);
console.log(` Snooze duration: ${expirationTime * 60}`);
console.log(` Snooze enabled: ${snooze?.snooze_enabled}`);
console.log(` Snooze endtime: ${snooze?.snooze_endtime}`);
} catch (error) {
console.error("Error updating Slack status:", error.message);
// Log inputs
console.log(` Text: ${statusText}`);
console.log(` Emoji: ${statusEmoji}`);
process.exit(1);
}
})();