forked from danielmurphy/control-deck-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (92 loc) · 2.87 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const path = require('path');
const { exec } = require('child_process');
const { RTMClient, WebClient } = require('@slack/client');
const sharp = require('sharp');
const request = require('request').defaults({ encoding: null });
class ControlDeckSlack {
constructor(streamDeck, buttonId, options) {
if (options.type === 'toggle-status') {
new ToggleStatus(streamDeck, buttonId, options);
} else if (options.type === 'user') {
new User(streamDeck, buttonId, options);
}
}
}
class User {
constructor(streamDeck, buttonId, options) {
this.streamDeck = streamDeck;
this.buttonId = buttonId;
this.userId = options.user_id;
const slackToken = process.env.SLACK_API_TOKEN;
const web = new WebClient(slackToken);
const rtm = new RTMClient(slackToken);
this.streamDeck.on('down', keyIndex => {
if (keyIndex === this.buttonId) {
exec(
`open 'slack://user?team=${options.team_id}&id=${options.user_id}'`
);
}
});
rtm.start({
batch_presence_aware: true
});
rtm.subscribePresence([this.userId]);
web.users.info({ user: this.userId }).then(data => {
const userAvatarURL = data.user.profile.image_original;
request(userAvatarURL, (error, response, body) => {
sharp(body)
.png()
.toBuffer()
.then(buffer => {
this.onlineImage = buffer;
});
sharp(body)
.grayscale()
.png()
.toBuffer()
.then(buffer => {
this.offlineImage = buffer;
});
rtm.on('presence_change', event => {
event.presence === 'active'
? streamDeck.fillImageFromFile(this.buttonId, this.onlineImage)
: streamDeck.fillImageFromFile(this.buttonId, this.offlineImage);
});
});
});
}
}
class ToggleStatus {
constructor(streamDeck, buttonId, options) {
this.streamDeck = streamDeck;
this.buttonId = buttonId;
let myPresence = 'away';
const slackToken = process.env.SLACK_API_TOKEN;
const web = new WebClient(slackToken);
const rtm = new RTMClient(slackToken);
this._updateButton(myPresence);
rtm.start({
batch_presence_aware: true
});
rtm.subscribePresence([options.user_id]);
rtm.on('presence_change', event => {
myPresence = event.presence;
this._updateButton(myPresence);
});
streamDeck.on('up', keyIndex => {
if (keyIndex === buttonId) {
let newStatus = myPresence === 'away' ? 'auto' : 'away';
web.users.setPresence({ presence: newStatus });
this._updateButton(newStatus);
}
});
}
_updateButton(status) {
let icon = status === 'away' ? 'offline.png' : 'online.png';
this.streamDeck.fillImageFromFile(
this.buttonId,
path.resolve(__dirname, `icons/${icon}`)
);
}
}
module.exports = ControlDeckSlack;