forked from bryanbraun/twitter-listbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (86 loc) · 2.96 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
// START HEROKU SETUP
var express = require("express");
var app = express();
app.get('/', function(req, res){ res.send('The robot is happily running.'); });
app.listen(process.env.PORT || 5000);
// END HEROKU SETUP
// Listbot config
//
// Config.keys uses environment variables so sensitive info is not in the repo.
var config = {
me: 'StoriesInData', // The authorized account with a list to retweet.
myList: 'data-driven-storytellers', // The list we want to retweet.
regexFilter: '', // Accept only tweets matching this regex pattern.
regexReject: '(RT|@)', // AND reject any tweets matching this regex pattern.
keys: {
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
},
};
// Get the members of our list, and pass them into a callback function.
function getListMembers(callback) {
var memberIDs = [];
tu.listMembers({owner_screen_name: config.me,
slug: config.myList
},
function(error, data){
if (!error) {
for (var i=0; i < data.users.length; i++) {
memberIDs.push(data.users[i].id_str);
}
// This callback is designed to run listen(memberIDs).
callback(memberIDs);
} else {
console.log(error);
console.log(data);
}
});
}
// What to do after we retweet something.
function onReTweet(err) {
if(err) {
console.error("retweeting failed :(");
console.error(err);
}
}
// What to do when we get a tweet.
function onTweet(tweet) {
// Reject the tweet if:
// 1. it's flagged as a retweet
// 2. it's matches our regex rejection criteria
// 3. it doesn't match our regex acceptance filter
var regexReject = new RegExp(config.regexReject, 'i');
var regexFilter = new RegExp(config.regexFilter, 'i');
if (tweet.retweeted) {
return;
}
if (config.regexReject !== '' && regexReject.test(tweet.text)) {
return;
}
if (regexFilter.test(tweet.text)) {
console.log(tweet);
console.log("RT: " + tweet.text);
// Note we're using the id_str property since javascript is not accurate
// for 64bit ints.
tu.retweet({
id: tweet.id_str
}, onReTweet);
}
}
// Function for listening to twitter streams and retweeting on demand.
function listen(listMembers) {
tu.filter({
follow: listMembers
}, function(stream) {
console.log("listening to stream");
stream.on('tweet', onTweet);
});
}
// The application itself.
// Use the tuiter node module to get access to twitter.
var tu = require('tuiter')(config.keys);
// Run the application. The callback in getListMembers ensures we get our list
// of twitter streams before we attempt to listen to them via the twitter API.
getListMembers(listen);