forked from ggauravr/slack-terminalize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (56 loc) · 1.7 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
'use strict';
var logger = require('winston'),
RtmClient = require('@slack/client').RtmClient,
WebClient = require('@slack/client').WebClient,
MemoryDataStore = require('@slack/client').MemoryDataStore,
RTM_EVENTS = require('@slack/client').RTM_EVENTS,
CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS,
util = {
config: require('./util/config'),
command: require('./util/command')
},
dispatcher = require('./dispatcher'),
rtmClient, webClient;
var _listen = function () {
dispatcher.init(rtmClient);
rtmClient.on(RTM_EVENTS.MESSAGE, function (message) {
dispatcher.handle(message);
});
};
var _login = function () {
if (!rtmClient) {
throw new Error('Slack RTM client not initialized');
}
logger.info('Initiating RTM client authentication');
rtmClient.start();
rtmClient.on(CLIENT_EVENTS.RTM.AUTHENTICATED, function (rtmStartData) {
if (!dispatcher.isInitialized()) {
logger.info('RTM client authenticated.');
_listen();
}
});
};
var getRTMClient = function () {
return rtmClient;
};
var getWebClient = function () {
return webClient;
};
var init = function (token, opts, config) {
if (!token) {
throw new Error('slack token not passed with opts in init');
}
// set defaults if not provided
opts.autoReconnect = opts.autoReconnect || true;
opts.logLevel = opts.logLevel || 'error';
opts.dataStore = new MemoryDataStore({});
rtmClient = new RtmClient(token, opts);
webClient = new WebClient(token, opts);
util.config.init(config);
_login();
};
exports.init = init;
exports.getCommands = util.command.getCommandObjects;
exports.getResponses = util.command.getResponseObjects;
exports.getRTMClient = getRTMClient;
exports.getWebClient = getWebClient;