-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
119 lines (90 loc) · 2.99 KB
/
server.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
var WebSocketServer = require('websocket').server;
var server = require('node-http-server');
var uid = require('uid');
var STATIC_MESSAGES = require('./constants').STATIC_MESSAGES;
var SPECIAL_ORIGINS = require('./constants').SPECIAL_ORIGINS;
var RtmClient = require('@slack/client').RtmClient;
var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var bot_token = process.env.SLACK_BOT_TOKEN || '';
var rtm = new RtmClient(bot_token);
var channel;
var connections = {};
var httpServer = new server.Server({
port: 8000,
root:'./public'
});
httpServer.deploy({}, serverReady);
var wsServer = new server.Server({
port: 1337,
root: '.'
});
wsServer.deploy({}, serverReady);
// create the server
wsServer = new WebSocketServer({
httpServer: wsServer.server
});
// WebSocket server
wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
connection.uid = uid(24);
connections[connection.uid] = connection;
connection.on('close', function(){
delete connections[connection.uid];
})
});
function sendActivity(text, channel, user) {
var count = 0;
for (var id in connections){
connections[id].sendUTF(JSON.stringify({text: text, channel: channel, user: user}));
count++;
}
console.log('Broadcasted for ' + count + ' connections.');
}
function serverReady(server){
console.log( 'Server on port ' + server.config.port + ' is now up');
}
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, function (rtmStartData) {
console.log('Logged in as ' + rtmStartData.self.name + ' of team ' + rtmStartData.team.name);
});
rtm.on(RTM_EVENTS.MESSAGE, function (messageData) {
var origin = false;
var channel = rtm.dataStore.getChannelById(messageData.channel);
var dm = rtm.dataStore.getDMById(messageData.channel);
var who = getUserOrBot(messageData.user);
if (channel) {
origin = channel.name;
} else if (dm) {
origin = SPECIAL_ORIGINS.DIRECT;
}
if (origin && who) {
sendActivity(messageData.text, origin, who.name);
}
});
rtm.on(RTM_EVENTS.PRESENCE_CHANGE, function(presenceData) {
var userIds = presenceData.users;
if(presenceData.user) {
userIds = [presenceData.user];
}
var message = (presenceData.presence == 'active' ? STATIC_MESSAGES.PRESENCE_ACTIVE : STATIC_MESSAGES.PRESENCE_AWAY);
for(var userId of userIds) {
var who = getUserOrBot(userId);
if (who) {
sendActivity(message, SPECIAL_ORIGINS.PRESENCE_CHANGE, who.name);
}
}
});
rtm.on(RTM_EVENTS.REACTION_ADDED, function(reactionData) {
var who = getUserOrBot(reactionData.user);
if (who && reactionData.item.channel) {
var channel = rtm.dataStore.getChannelById(reactionData.item.channel);
// emojis will be specially processed in client side
sendActivity(':' + reactionData.reaction + ':', channel.name, who.name);
}
});
rtm.start();
function getUserOrBot(id) {
var user = rtm.dataStore.getUserById(id);
var bot = rtm.dataStore.getBotById(id);
return user || bot;
}