-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (100 loc) · 3.58 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
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
var fs = require('fs');
var Slack = require('slack-client');
var express = require('express');
var util = require('util');
var async = require('async');
var MongoClient = require('mongodb').MongoClient;
var config = require('config');
var token = config.get('nobi.slack.token');
var autoReconnect = true;
var autoMark = true;
var slack = new Slack(token, autoReconnect, autoMark);
var slackChannels = [];
var slackUsers = [];
async.series([
function(callback) {
// Load channels and users into memory
var channelsStoragePath = './storage/channels.txt';
var usersStoragePath = './storage/users.txt';
if(fs.existsSync(channelsStoragePath) && fs.existsSync(usersStoragePath)) {
// Load channels and users from storage
console.log('Loading channels and users from storage');
slackChannels = JSON.parse(fs.readFileSync(channelsStoragePath));
slackUsers = JSON.parse(fs.readFileSync(usersStoragePath));
} else {
// Fetch channels and users from Slack and store in storage folder
console.log('Fetching channels from Slack');
slack._apiCall('channels.list', {'token':token}, function(data) {
if(data.ok) {
for(var i=0; i<data.channels.length; i++) {
var slackChannel = data.channels[i];
if(slackChannel.is_member) {
slackChannels.push({'id': slackChannel.id, 'name': slackChannel.name});
}
}
// Store channels in storage
fs.writeFile(channelsStoragePath, JSON.stringify(slackChannels),
function(err) {
if (err) throw err;
console.log('Saved channels to storage');
});
}
});
console.log('Fetching users from Slack');
slack._apiCall('users.list', {'token':token}, function(data) {
if(data.ok) {
for(var i=0; i<data.members.length; i++) {
var slackUser = data.members[i];
slackUsers.push({'id': slackUser.id, 'name': slackUser.name});
}
// Store users in storage
fs.writeFile(usersStoragePath, JSON.stringify(slackUsers),
function(err) {
if (err) throw err;
console.log('Saved users to storage');
});
}
});
}
callback();
},
function(callback) {
// Start express
var app = express();
var rest = require('./rest')(slack, slackUsers, slackChannels);
app.use('/nobi/slack', rest);
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});
callback();
}
]);
slack.on('open', function() {
console.log('Slack on open');
});
slack.on('message', function(message) {
var user = slack.getUserByID(message.user);
var channel = slack.getChannelGroupOrDMByID(message.channel);
if(user && message && channel) {
console.log("[" + channel.name + "] " + user.name + " : " + message.text);
// Save message to mongo
var mongoUrl = 'mongodb://' + config.get('nobi.db.url') + ':27017/nobi';
MongoClient.connect(mongoUrl, function(err, db) {
db.collection('slack').insertOne({
channel: channel.name,
user: user.name,
message: message.text,
createdtime: Date.now()
}, function(err, r) {
db.close();
});
});
}
});
slack.on('error', function(error) {
console.error(error);
});
slack.login();
console.log('Logged into Slack');