forked from blocktrail/slack-tipbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
128 lines (105 loc) · 3.77 KB
/
bot.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
var _ = require('lodash');
var debug = require('debug');
var Slack = require('slack-client');
var TipBot = require('./lib/tipbot');
var assert = require('assert');
var parseArgs = require('minimist');
var argv = parseArgs(process.argv.slice(2));
var SLACK_TOKEN = argv['slack-token'] || process.env.TIPBOT_SLACK_TOKEN,
BLOCKTRAIL_APIKEY = argv['blocktrail-apikey'] || process.env.TIPBOT_BLOCKTRAIL_APIKEY,
BLOCKTRAIL_APISECRET = argv['blocktrail-apisecret'] || process.env.TIPBOT_BLOCKTRAIL_APISECRET,
SECRET = argv['secret'] || process.env.TIPBOT_SECRET,
TESTNET = argv['testnet'] || process.env.TIPBOT_TESTNET,
AUTO_RECONNECT = true,
OPTIONS = {ALL_BALANCES: true, DEMAND: true};
assert(SLACK_TOKEN, "--slack-token or TIPBOT_SLACK_TOKEN is required");
assert(BLOCKTRAIL_APIKEY, "--blocktrail-apikey or TIPBOT_BLOCKTRAIL_APIKEY is required");
assert(BLOCKTRAIL_APISECRET, "--blocktrail-apisecret or TIPBOT_BLOCKTRAIL_APISECRET is required");
assert(SECRET, "--secret or TIPBOT_SECRET is required");
/**
* find a DM channel object by userID
*
* @param userId
* @returns {*}
*/
Slack.prototype.getDMByUserId = function(userId) {
return _.find(this.dms, {user: userId});
};
Slack.prototype.reconnect = function() {
var timeout;
if (this._pongTimeout) {
clearInterval(this._pongTimeout);
this._pongTimeout = null;
}
this.authenticated = false;
this.ws.close();
this._connAttempts++;
timeout = this._connAttempts * 1000;
this.logger.info("Reconnecting in %dms", timeout);
// reset
this.channels = {};
this.dms = {};
this.groups = {};
this.users = {};
this.bots = {};
return setTimeout((function(_this) {
return function() {
_this.logger.info('Attempting reconnect');
return _this.login();
};
})(this), timeout);
};
var slack = new Slack(SLACK_TOKEN, AUTO_RECONNECT, /* AUTO_MARK */ true);
var tipbot = new TipBot(slack, BLOCKTRAIL_APIKEY, BLOCKTRAIL_APISECRET, SECRET, TESTNET, OPTIONS);
slack.on('open', function() {
var channels = [],
groups = [];
_.each(slack.channels, function(channel, key) {
if (channel.is_member) {
channels.push('#' + channel.name);
}
});
_.each(slack.groups, function(group, key) {
if (group.is_open && !group.is_archived) {
groups.push(group.name);
}
});
debug('tipbot:bot')('Connected to Slack, SocketURL: %s', slack.socketUrl);
debug('tipbot:bot')('You are <@%s:%s> of %s', slack.self.id, slack.self.name, slack.team.name);
debug('tipbot:bot')('You are in (channels): %s', channels.join(', '));
debug('tipbot:bot')('As well as (groups): %s', groups.join(', '));
// init the tipbot
setTimeout(function() {
tipbot.init();
}, 0);
});
slack.on('message', function(message) {
// debug messages to seperate channel so we only log them when explicitly enabled
debug('tipbot:messages')('MESSAGE', message.type, message.channel, message.user, message.text);
var type = message.type,
channel = slack.getChannelGroupOrDMByID(message.channel),
member = slack.getUserByID(message.user);
// Respond to messages with the reverse of the text received.
if (type === 'message') {
// random stuff we can safely ignore
if (!message.text || !member) {
return;
}
// let tipbot handle the message
tipbot.onMessage(channel, member, message.text);
}
});
slack.on('userChange', function(u) {
tipbot.onUserChange(u);
});
slack.on('close', function(e) {
debug('tipbot:bot')('Close!!');
console.log(e);
process.exit(1);
});
slack.on('error', function(error) {
debug('tipbot:bot')('Error!!');
console.log(error);
process.exit(1);
});
slack.login();