-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
115 lines (95 loc) · 2.94 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
'use strict';
const Twitter = require('twitter');
const auth = require('./auth.json');
const bole = require('bole');
const config = require('./config.json');
const messages = config.messages;
// Set up logging
const log = bole('cthun');
bole.output({
level: config.logging,
stream: process.stdout,
});
// Construct the filter
let users = [];
for (var i = 0; i < config.usersToFollow.length; i++) {
let user = config.usersToFollow[i];
users.push(user.id);
}
// Convert to a string so Twitter can process it as param.
let params = {
follow : users.join(','),
}
// Set up the twitter client.
const client = new Twitter(auth);
client.stream('statuses/filter', params, function (stream) {
log.info('C\'Thun started whispering');
stream.on('data', processData);
stream.on('error', log.error);
});
/**
* Extract relevant info from a tweet and determine whether to whisper a
* maddening thought.
* @param {Object} tweet Object passed by the Twitter.stream.
*/
function processData(tweet) {
// Check whether there is a user at all.
if (!tweet.user) return;
// Make sure it's a tweet from a followed user, not a retweet or something
if (users.indexOf(tweet.user.id) === -1) {
log.debug('This mortal is not worthy of my time...');
return;
}
// Speaking of retweets, we don't want them from the followed users either
if (tweet.retweeted_status) {
log.debug('How original...');
return;
}
// Check whether the user actually mentioned us.
let message = tweet.text.toLowerCase();
let shouldWhisper = false;
for (var i = 0; i < config.queries.length; i++) {
let query = config.queries[i];
if (message.indexOf(query) !== -1) {
shouldWhisper = true;
break;
}
}
if (!shouldWhisper) {
log.debug('I should bide my time...');
return;
}
// Extract relevant information.
let user = tweet.user.screen_name;
let tweetId = tweet.id_str;
// Drive them mad...
tweetWhisper(user, tweetId);
}
/**
* Whisper a random maddening thought to a user.
* @param {String} user Twitter screen name of the user to reply to.
* @param {String} tweetId ID of the tweet to reply to.
*/
function tweetWhisper(user, tweetId) {
// Direct it to the lucky individual.
let whisper = '@' + user + ': ';
// Add a maddening whisper.
whisper += messages[Math.floor(Math.random() * messages.length)];
// Make sure we don't go over the 140 character limit.
whisper.substr(0, 140);
// Set up post object.
let data = {
in_reply_to_status_id: tweetId,
status: whisper,
}
// Commenting code out is cumbersome.
if (config.testing) {
log.info(data);
return;
}
// HEY, LISTEN!
client.post('statuses/update', data, function (err, tweet, response) {
if (err) log.error(err);
else log.info('Whispered... ' + tweet.text);
});
};