forked from patricksimpson/status-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (50 loc) · 1.81 KB
/
app.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
require('dotenv').config()
const RtmClient = require('@slack/client').RtmClient;
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
const WebClient = require('@slack/client').WebClient;
const bot_token = process.env.SLACK_BOT_TOKEN || '';
const web_token = process.env.SLACK_WEB_TOKEN || '';
const rtm = new RtmClient(bot_token);
const web = new WebClient(web_token);
console.log('Starting bot, please stand by.');
let channel;
let users = [];
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
for (const c of rtmStartData.channels) {
if (c.is_member && c.name ==='status') { channel = c.id }
}
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
});
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function () {
console.log('connected');
});
rtm.on(RTM_EVENTS.USER_CHANGE, function(e){
const user = e.user;
const id = user.id;
const name = user.name;
const message = `${name} updated`;
web._makeAPICall('users.profile.get', null, {user: id}, function(err, info) {
if (!err) {
const profile = info.profile;
const userStatus = `${profile.status_emoji} ${profile.status_text}`;
let index = users.findIndex( u => u.id === id);
if (index < 0) {
index = users.length;
users.push( {id, name, status: '' } );
}
let savedUser = users[index];
if (savedUser.status !== userStatus) {
users[index].status = userStatus;
if (userStatus.trim() !== '') {
rtm.sendMessage(`${name}: ${userStatus}`, channel);
} else {
rtm.sendMessage(`${name} removed status.`, channel);
}
}
} else {
console.log('There was an error: ', err);
}
});
});
rtm.start();