-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
120 lines (106 loc) · 3.22 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
// Dependencies =========================
var
twit = require('twit'),
config = require('./config');
var Twitter = new twit(config);
var stream = Twitter.stream('user');
// RETWEET BOT ==========================
// find latest tweet according the query 'q' in params
var retweet = function() {
var params = {
q: '#nodejs, #Nodejs', // REQUIRED
result_type: 'recent',
lang: 'en'
}
Twitter.get('search/tweets', params, function(err, data) {
// if there no errors
if (!err) {
// grab ID of tweet to retweet
var retweetId = data.statuses[0].id_str;
// Tell TWITTER to retweet
Twitter.post('statuses/retweet/:id', {
id: retweetId
}, function(err, response) {
if (response) {
console.log('Retweeted!!!');
}
// if there was an error while tweeting
if (err) {
console.log('Something went wrong while RETWEETING... Duplication maybe...');
}
});
}
// if unable to Search a tweet
else {
console.log('Something went wrong while SEARCHING...');
}
});
}
// grab & retweet as soon as program is running...
retweet();
// retweet in every 50 minutes
setInterval(retweet, 3000000);
// FAVORITE BOT====================
// find a random tweet and 'favorite' it
var favoriteTweet = function(){
var params = {
q: '#nodejs, #Nodejs', // REQUIRED
result_type: 'recent',
lang: 'en'
}
// find the tweet
Twitter.get('search/tweets', params, function(err,data){
// find tweets
var tweet = data.statuses;
var randomTweet = ranDom(tweet); // pick a random tweet
// if random tweet exists
if(typeof randomTweet != 'undefined'){
// Tell TWITTER to 'favorite'
Twitter.post('favorites/create', {id: randomTweet.id_str}, function(err, response){
// if there was an error while 'favorite'
if(err){
console.log('CANNOT BE FAVORITE... Error');
}
else{
console.log('FAVORITED... Success!!!');
}
});
}
});
}
// grab & 'favorite' as soon as program is running...
favoriteTweet();
// 'favorite' a tweet in every 60 minutes
setInterval(favoriteTweet, 3600000);
// function to generate a random tweet tweet
function ranDom (arr) {
var index = Math.floor(Math.random()*arr.length);
return arr[index];
};
// FOLLOW-Reply BOT ===========================
// when someone follows
stream.on('follow', followed);
// ...trigger the callback
function followed(event) {
console.log('Follow Event is running');
//get their twitter handler (screen name)
var
name = event.source.name,
screenName = event.source.screen_name;
// function that replies back to the user who followed
tweetNow('@' + screenName + ' Thank you for the follow up.');
}
// function definition to tweet back to user who followed
function tweetNow(tweetTxt) {
var tweet = {
status: tweetTxt
}
Twitter.post('statuses/update', tweet, function(err, data, response) {
if(err){
console.log("Error in Replying");
}
else{
console.log("Gratitude shown successfully");
}
});
}