-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (139 loc) · 4.55 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var botkit = require('botkit');
var EventEmitter = require('events').EventEmitter;
var thunky = require('thunky');
var TESTER_NAME = 'robbie';
module.exports = function (opts) {
var that = new EventEmitter();
var controller = botkit.slackbot();
var currentChannel;
var channels = {};
var isRTMOpen = false;
var rtmOpenCbs = [];
var replyCbs = [];
var mentionCbs = [];
var matchCbs = [];
var reactionsCbs = [];
var bot;
var rtmMessages = new EventEmitter();
var tester = controller.spawn({
token: opts.token,
}).startRTM();
tester.api.users.list({}, function (err, users) {
if (err) return that.emit('error', err);
bot = users.members.filter(function (user) {
return user.name === opts.name;
})[0];
if (!bot) that.emit('error', new Error('Couldn\'t find the bot'));
});
rtmMessages.on('message', function (message) {
if (message.user === tester.identity.id) return;
if (['message', 'reaction_added'].indexOf(message.type) === -1) return;
getChannels(function (err, channels) {
if (err) return that.emit('error', err);
var ch;
if (message.channel) {
ch = channels.filter(c => c.id === message.channel)[0];
if (ch) message.channelName = '#' + ch.name;
else message.channelName = 'dm';
}
if (message.type === 'message' && replyCbs.length) replyCbs.shift()(null, message);
if (message.type === 'reaction_added' && reactionsCbs.length) reactionsCbs.shift()(null, message);
if (!matchCbs.length) return;
var idx = matchCbs.reduce(function (idx, matchCb, i) {
return idx === -1 && message.text.match(matchCb.regex) ? i : idx;
}, -1);
if (idx === -1) return;
matchCbs[idx].cb(message);
matchCbs = matchCbs.slice(0, idx).concat(matchCbs.slice(idx + 1)); // Remove the matching cb
});
});
controller.hears('', ['mention', 'direct_mention'], function(tester, message) {
if (mentionCbs.length && message.event === 'direct_mention') mentionCbs.shift()(null, message);
});
controller.on('rtm_open', function () {
isRTMOpen = true;
tester.rtm.on('message', function (data) {
rtmMessages.emit('message', JSON.parse(data));
});
rtmOpenCbs.forEach(function (fn) {
fn();
});
});
that.join = function (channel, cb) {
cb = cb || function () {};
tester.api.channels.list({}, onlist);
function onlist (err, chs) {
if (err) return cb(err);
var ch = chs.channels.filter(function (info) {
return info.name === channel;
})[0];
channels[channel] = ch.id;
currentChannel = ch.id;
cb();
}
};
that.say = function (message, opts) {
if (!isRTMOpen) return rtmOpenCbs.push(that.say.bind(null, message, opts));
opts = opts || {};
var ch = currentChannel;
if (opts.channel) ch = channels[opts.channel];
tester.say({text: message, channel: ch});
};
that.mention = function (message, opts) {
that.say('@'+bot.name+': ' + message, opts);
};
that.dm = function (message, opts) {
if (!isRTMOpen) return rtmOpenCbs.push(that.dm.bind(null, message, opts));
tester.startPrivateConversation({user: bot.id}, function (err, convo){
if (err) return that.emit('error', err);
convo.say(message);
});
}
that.nextReply = function (cb) {
replyCbs.push(cb);
};
that.nextMention = function (cb) {
mentionCbs.push(cb);
};
that.nextMatch = function (regex, cb) {
matchCbs.push({regex: regex, cb: cb});
};
that.nextReaction = function (cb) {
reactionsCbs.push(cb);
};
that.script = function (scrpt, cb) {
var s = scrpt.slice();
loop();
function loop() {
if (!s.length) return cb(null, {ok: true});
var next = s.shift();
if (next.tester && next.bot) return cb(new Error('Cannot set both tester and bot.'));
if (next.tester) {
that.say(next.tester);
loop();
return;
}
if (next.bot) return that.nextReply(checkReply(next.bot));
}
function checkReply (expected) {
return function (err, reply) {
if (err) return cb(err);
if (reply.text !== expected) return cb(null, {ok: false});
loop();
}
}
};
that.close = function () {
tester.closeRTM();
};
that.addReaction = function (msg, name, cb) {
tester.api.reactions.add({name: name, channel: msg.channel, timestamp: msg.ts});
};
var getChannels = thunky(function (cb) {
tester.api.channels.list({}, function (err, ch) {
if (err) return cb(err);
cb(null, ch.channels);
});
});
return that;
};