-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
74 lines (63 loc) · 2.45 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
const config = require('./config')
const twit = require('twit')
const T = new twit(config)
// Utility function - Gives unique elements from an array
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function retweet(searchText) {
// Params to be passed to the 'search/tweets' API endpoint
let params = {
q : searchText + '',
result_type : 'mixed',
count : 25,
}
T.get('search/tweets', params, function(err_search, data_search, response_search){
let tweets = data_search.statuses
if (!err_search)
{
let tweetIDList = []
for(let tweet of tweets) {
// To avoid duplication of retweets
if(tweet.text.startsWith("RT @")){
// If tweet text starts with "RT @" then it is a retweeted tweet,
// with a different 'id_str' than the original
console.log("\nStarts with RT@, adding retweeted status id_str")
if (tweet.retweeted_status) {
tweetIDList.push(tweet.retweeted_status.id_str);
}
else {
tweetIDList.push(tweet.id_str);
}
}
else {
tweetIDList.push(tweet.id_str);
}
}
// Get only unique entries
tweetIDList = tweetIDList.filter( onlyUnique )
console.log("TweetID LIST = \n" + tweetIDList)
// Call the 'statuses/retweet/:id' API endpoint for retweeting EACH of the tweetID
for (let tweetID of tweetIDList) {
T.post('statuses/retweet/:id', {id : tweetID}, function(err_rt, data_rt, response_rt){
if(!err_rt){
console.log("\n\nRetweeted! ID - " + tweetID)
}
else {
console.log("\nError... Duplication maybe... " + tweetID)
console.log("Error = " + err_rt)
}
// For debugging
// console.log("Data = " + data_rt.text)
// console.log(data_rt)
})
}
}
else {
console.log("Error while searching" + err_search)
process.exit(1)
}
})
}
// Run every 60 seconds
setInterval(function() { retweet('#AzureDevOps'); }, 60000)