-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·98 lines (87 loc) · 2.71 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const axios = require('axios');
const {addMinutes, getUnixTime} = require('date-fns');
const querystring = require('querystring');
const config = require('./config');
const {log, error} = console;
if (!config.slackToken && config.slackToken.length) {
error('Missing Slack token. Set it in config.js');
process.exit(1);
}
function setSlackStatus(token, status) {
return axios.post('https://slack.com/api/users.profile.set', querystring.stringify({
token: token,
profile: JSON.stringify(status)
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function (response) {
log('Set Slack status API response: %j', response.data.username, response.data.profile.status_emoji, response.data.profile.status_text);
}).catch(function (err) {
error('Set Slack status error: %s', err);
});
}
function slackStatus(status, timeout) {
if (timeout > 0 && !isNaN(parseInt(timeout, 10))) {
// eslint-disable-next-line camelcase
status.status_expiration = getUnixTime(addMinutes(new Date(), parseInt(timeout, 10)));
}
log('Setting Slack status to: %j', status);
config.slackToken.forEach(token => {
setSlackStatus(token, status);
});
}
function manualSlackStatus(emoji = ':coffee:', text, timeout) {
const status = {
status_text: text, // eslint-disable-line camelcase
status_emoji: `:${emoji.replace(/[^a-zA-Z0-9-_]/g, '')}:` // eslint-disable-line camelcase
};
if (timeout > 0 && !isNaN(parseInt(timeout, 10))) {
// eslint-disable-next-line camelcase
status.status_expiration = getUnixTime(addMinutes(new Date(), parseInt(timeout, 10)));
}
log('Setting Slack status to: %j', status);
config.slackToken.forEach(token => {
setSlackStatus(token, status);
});
}
const cli = meow(`
Usage
$ ./index.js <input>
$ ./index.js -s <status>
$ ./index.js <input> --emoji=<emoji>
Options
--status, -s status based on presets [lunch, afk, off, meeting, etc. - see config.js]
--timeout, -t how long status lasts before clearing (in minutes)
--emoji, -e manually set the emoji for slack status (cannot be used with 'status')
Examples
$ ./index.js need more coffee -t 10
$ ./index.js -s lunch
$ ./index.js -s lunch -t 60
$ ./index.js -e shrug
`, {
flags: {
status: {
type: 'string',
alias: 's'
},
timeout: {
type: 'number',
alias: 't'
},
emoji: {
type: 'string',
alias: 'e'
}
}
});
if (cli.flags.status) {
slackStatus(config.statusBy[cli.flags.status.toUpperCase()], cli.flags.timeout);
} else if (cli.flags.emoji || cli.input.length) {
manualSlackStatus(cli.flags.emoji, cli.input.join(' '), cli.flags.timeout);
} else {
cli.showHelp();
}