This repository has been archived by the owner on Jan 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
133 lines (114 loc) · 3.8 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* setup slack connection */
var RtmClient = require('@slack/client').RtmClient;
var WebClient = require('@slack/client').WebClient;
var MemoryDataStore = require('@slack/client').MemoryDataStore;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
var token = process.env.SLACK_API_TOKEN || '';
var rtm = new RtmClient(token, {
logLevel: 'error',
dataStore: new MemoryDataStore(),
autoReconnect: true,
autoMark: false
});
var web = new WebClient(token);
rtm.start();
/* setup elasticsearch connection */
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: process.env.ELASTICSEARCH_HOST,
});
client.ping().then(
function(){
console.error("[Elasticsearch] I'm here!");
},
function(error){
console.error('[Elasticsearch] Could not contact server, error was:');
console.error(error);
console.error('... exiting');
process.exit(1);
}
);
/* slack events */
rtm.on(RTM_EVENTS.MESSAGE, function (message) {
var channel = rtm.dataStore.getChannelGroupOrDMById(message.channel);
var user = rtm.dataStore.getUserById(message.user);
if (!user) {
user = {};
}
// console.log('[Slack] Received message');
client.create({
index: 'slack',
type: 'message',
body: {
user: user,
channel: channel,
message: message,
}
}).then(
function(body){
// nothing to do
},
function(error){
console.error('[Elasticsearch] could not record message, error was:');
console.error(error);
}
);
});
/* monitor online presence, setting user to away if this is the only client left */
var presenceMonitor;
function checkPresence(){
web.users.getPresence().then(
function(user){
if(user.presence != 'away'){
if(user.connection_count == 1){
web.users.setPresence('away').then(
function(){
console.log("[Slack] I'm all alone now. Setting presence to away");
if(presenceMonitor){
clearInterval(presenceMonitor);
presenceMonitor = null;
}
},
function(error){
console.error(error);
}
);
} else if (!presenceMonitor) {
console.log("[Slack] I'm online but not alone, start monitoring");
presenceMonitor = setInterval(function(){
checkPresence();
}, 30000);
}
}
},
function(error){
console.error(error);
}
);
}
rtm.on(RTM_EVENTS.PRESENCE_CHANGE, function(presence){
if(presence.user == rtm.activeUserId && presence.presence == 'active'){
checkPresence();
}
});
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function(){
var user = rtm.dataStore.getUserById(rtm.activeUserId);
var team = rtm.dataStore.getTeamById(rtm.activeTeamId);
console.log('[Slack] Connected to %s as %s', team.name, user.name);
console.log('[Slack] Ready when you are...');
if(presenceMonitor){
clearInterval(presenceMonitor);
presenceMonitor = null;
}
presenceMonitor = setInterval(function(){
checkPresence();
}, 5000);
});
rtm.on(CLIENT_EVENTS.RTM.DISCONNECT, function(){
console.error('[Slack] Could not reconnect to Slack, exiting');
process.exit(1);
});
rtm.on(CLIENT_EVENTS.RTM.ATTEMPTING_RECONNECT, function(){
console.error('[Slack] Connection to Slack lost, reconnecting...');
});