-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
361 lines (295 loc) · 9.98 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
var Botkit = require('botkit');
var Twit = require('twit');
var sentiment = require('sentiment');
var Quiche = require('quiche');
var findHashtags = require('find-hashtags');
var config = require('./config');
var config2 = require('./config2');
var T = new Twit(config);
var negativeAlerts = false;
var stream;
var timer;
var controller = Botkit.slackbot({
debug: false,
});
var bot = controller.spawn(config2).startRTM();
var sentimentObj = {};
var sentimentIntervalObj = {
interval: 0,
positive: 0,
negative: 0,
neutral: 0
};
var topHashtags = {};
var topUsersObj = {};
var sentimentIntervalArr = [];
var xAxisLabelArr = [];
var chartColors = {
pos: '04B8F2',
neg: 'EDED1D',
neu: 'C1D1CC'
};
function resetVars() {
sentimentObj = {};
sentimentIntervalObj = {
interval: 0,
positive: 0,
negative: 0,
neutral: 0
};
topHashtags = {};
topUsersObj = {};
sentimentIntervalArr = [];
}
//constructs Sentiment Distribution by Time Interval bar chart
function makeBar(bot, message) {
var Quiche = require('quiche');
//var lastInterval = sentimentIntervalArr.length;
//var bottom = lastInterval < 10 ? 0 : lastInterval;
var negArr = sentimentIntervalArr.map(function(obj) {
return obj.negative;
});
var posArr = sentimentIntervalArr.map(function(obj) {
return obj.positive;
});
var neuArr = sentimentIntervalArr.map(function(obj) {
return obj.neutral;
});
var bar = new Quiche('bar');
bar.setWidth(400);
bar.setHeight(265);
bar.setTitle('Sentiment Distribution by Time Interval');
bar.setBarStacked(); // Stacked chart
bar.setBarWidth(0);
bar.setBarSpacing(6); // 6 pixles between bars/groups
bar.setLegendBottom('Intervals'); // Put legend at bottom
bar.setTransparentBackground(); // Make background transparent
bar.addData(neuArr, 'Neutral', chartColors.neu);
bar.addData(negArr, 'Negative', chartColors.neg);
bar.addData(posArr, 'Positive', chartColors.pos);
bar.setAutoScaling(); // Auto scale y axis
bar.addAxisLabels('x', xAxisLabelArr);
var imageUrl = bar.getUrl(true); // First param controls http vs. https
bot.reply(message, {
attachments: [{
fallback: 'Sentiment Distribution by Time Interval',
image_url: imageUrl,
color: "#4099FF"
}]
});
}
function makePie(bot, message) {
var pie = new Quiche('pie');
pie.set3D();
pie.setTitle('Sentiment Distribution');
pie.setTransparentBackground(); // Make background transparent
pie.addData(sentimentObj.positive || 0, 'Positive '.concat(sentimentObj.positive || 0), chartColors.pos);
pie.addData(sentimentObj.negative || 0, 'Negative '.concat(sentimentObj.negative || 0), chartColors.neg);
pie.addData(sentimentObj.neutral || 0, 'Neutral '.concat(sentimentObj.neutral || 0), chartColors.neu);
var imageUrl = pie.getUrl(true); // First param controls http vs. https
bot.reply(message, {
attachments: [{
fallback: 'Sentiment Distribution',
image_url: imageUrl,
color: "#4099FF"
}]
});
}
//bot outputs top 5 hashtags
function getTop5Hashtags(bot, message) {
var arr = Object.keys(topHashtags).map(function(hashtag) {
return {
hashtag: hashtag,
count: topHashtags[hashtag]
};
}).sort(function(a, b) {
return b.count - a.count;
});
var len = arr.length >= 5 ? 5 : arr.length;
var top5HashText = '';
for (var i = 0; i < len; i++) {
top5HashText = top5HashText + arr[i].count + ' for #' + arr[i].hashtag + '\n';
}
bot.reply(message, {
attachments: [{
fallback: 'Top 5 Hashtags!',
title: 'Top 5 Hashtags',
text: top5HashText,
color: "#4099FF",
}]
});
}
//bot outputs top 5 posters
function getTop5Posters(bot, message) {
var arr = Object.keys(topUsersObj).map(function(username) {
return {
user: username,
tweets: topUsersObj[username]
};
}).sort(function(a, b) {
return b.tweets - a.tweets;
});
var top5posterText = "";
var len = arr.length >= 5 ? 5 : arr.length;
for (var i = 0; i < len; i++) {
top5posterText = top5posterText + arr[i].tweets + ' from @' + arr[i].user + ' https://www.twitter.com/' + arr[i].user.toLowerCase() + '\n';
}
bot.reply(message, {
attachments: [{
fallback: 'Top 5 Posters!',
text: top5posterText,
title: 'Top 5 Posters',
color: "#4099FF",
}]
});
}
function startStream(bot, message) {
stream.on('tweet', function(tweet) {
if (!tweet.retweeted && !tweet.text.startsWith('RT')) {
var sent;
var score = sentiment(tweet.text).score;
if (score < 0) {
sent = 'negative';
if (negativeAlerts) {
bot.reply(message, {
attachments: [{
fallback: 'Negative Post Alert!',
text: tweet.created_at + ', posted by ' + tweet.user.screen_name + ': ' + tweet.text,
title: 'NEGATIVE POST ALERT!',
color: "#FF0000"
}]
});
}
}
else if (score > 0) {
sent = 'positive';
}
else if (score === 0) {
sent = 'neutral';
}
//inserts into an object and/or increments positive, negative or neutral
if (!sentimentObj[sent]) {
sentimentObj[sent] = 1;
}
else {
sentimentObj[sent] = sentimentObj[sent] + 1;
}
sentimentIntervalObj[sent]++;
//top users
if (!topUsersObj[tweet.user.screen_name]) {
topUsersObj[tweet.user.screen_name] = 1;
}
else {
topUsersObj[tweet.user.screen_name] = topUsersObj[tweet.user.screen_name] + 1;
}
//top hastags
var hashtags = findHashtags(tweet.text);
if (hashtags) {
hashtags.forEach(function(element) {
if (!topHashtags[element]) {
topHashtags[element] = 1;
}
else {
topHashtags[element] = topHashtags[element] + 1;
}
});
}
}
});
timer = setInterval(function() {
sentimentIntervalObj.interval++;
xAxisLabelArr.push(sentimentIntervalObj.interval);
bot.reply(message, {
attachments: [{
fallback: 'Interval heading!',
text: 'Interval Report ' + sentimentIntervalObj.interval + ': ' + sentimentIntervalObj.interval + 'm',
color: "#FFFF00",
}]
});
makePie(bot, message);
getTop5Posters(bot, message);
getTop5Hashtags(bot, message);
sentimentIntervalArr.push(sentimentIntervalObj);
if (sentimentIntervalObj.interval > 1) {
makeBar(bot, message);
}
if (sentimentIntervalArr.length === 10) {
sentimentIntervalArr.reverse();
sentimentIntervalArr.pop();
sentimentIntervalArr.reverse();
xAxisLabelArr.reverse();
xAxisLabelArr.pop();
xAxisLabelArr.reverse();
}
sentimentIntervalObj = {
interval: sentimentIntervalObj.interval,
positive: 0,
negative: 0,
neutral: 0
};
}, 60000);
}
controller.hears(['follow tweets (.*)', 'follow on (.*)'], 'direct_message', function(bot, message) {
if (!stream) {
//bot.reply(message, 'LAHOWENJO839U9: ' + message.match[1] + ', length: ' + message.match[1].length);
stream = T.stream('statuses/filter', {
track: [message.match[1]]
});
resetVars();
startStream(bot, message);
bot.reply(message, 'Okay. I will be tracking *' + message.match[1] + '* on Twitter, and provide you a summary report at one minute intervals.');
}
else {
bot.reply(message, 'Already tracking twitter!');
}
});
controller.hears('stop', 'direct_message', function(bot, message) {
if (stream) {
stream.stop();
stream = null;
bot.reply(message, 'stream ended.');
clearInterval(timer);
}
else {
bot.reply(message, 'there is no stream');
}
});
controller.hears('negative alerts on', 'direct_message', function(bot, message) {
if (negativeAlerts) {
bot.reply(message, 'Alerts for negative twitter posts already on.');
}
else {
negativeAlerts = true;
bot.reply(message, 'Negative twitter post alert has been turned ON at your request.');
}
});
controller.hears('negative alerts off', 'direct_message', function(bot, message) {
if (!negativeAlerts) {
bot.reply(message, 'Alerts for negative twitter posts is not on.');
}
else {
negativeAlerts = false;
bot.reply(message, 'Negative twitter post alert has been turned OFF at your request.');
}
});
controller.hears('test', 'direct_message', function(bot, message) {
bot.reply(message, {
attachments: [{
fallback: 'Negative Post Alert!',
text: 'Negative Post Alert!',
color: "#FF0000"
}]
});
});
controller.hears('bar', 'direct_message', function(bot, message) {
makeBar(bot, message);
});
controller.hears('pie', 'direct_message', function(bot, message) {
makePie(bot, message);
});
controller.hears('top', 'direct_message', function(bot, message) {
getTop5Posters(bot, message);
});
controller.hears('.*', 'direct_message', function(bot, message) {
bot.reply(message, `Sorry sir, I don't understand. I specialize only in things Twitter.`);
})