-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterBot.js
94 lines (87 loc) · 2.86 KB
/
twitterBot.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
// define the dependencies
const twit = require('twit');
global.lastTweetId = 0;
const config = {
CONSUMER_KEY: process.env.CONSUMER_KEY,
CONSUMER_SECRET: process.env.CONSUMER_SECRET,
ACCESS_TOKEN: process.env.ACCESS_TOKEN,
ACCESS_TOKEN_SECRET: process.env.ACCESS_TOKEN_SECRET
}
const Twitter = new twit(config);
let getAnswer = function()
{
let answerNumber = Math.floor((Math.random() * 3) + 1);
switch(answerNumber) {
case 1:
return 'You’re saying it wrong, It’s Wing-GAR-dium Levi-O-sa, make the “gar” nice and long.';
case 2:
return 'Swish and flick, remember, swish and flick.';
case 3:
return 'Never forget Wizard Baruffio, who said ‘s’ instead of ‘f’ and found himself on the floor with a buffalo on his chest.';
default:
return 'You do it, then, if you’re so clever';
}
}
let main = function()
{
let params = {
count: 10
}
if(global.lastTweetId > 0) {
params = {
since_id: global.lastTweetId
}
}
// search through all tweets using our params and execute a function:
Twitter.get('statuses/mentions_timeline', params, function(err, data) {
// if there is no error
if (!err) {
if (data.length > 0) {
global.lastTweetId = data[0].id_str;
console.log("Last tweet registered: " + global.lastTweetId);
console.log("Tweets received in this interval :" + data.length);
} else {
console.log("No tweets receiven in this interval");
}
} else {
// catch all log if the search could not be executed
console.log('Could not search tweets.');
}
}).then(function(data) {
let tweets = data.data;
// loop through the returned tweets
for (let i = 0; i < tweets.length; i++) {
// iterate through those first four defining a rtId that is equal to the value of each of those tweets' ids
let tId = tweets[i].id_str;
if (tweets[i].favorited) {
console.log('already liked ' + tweets[i].user.name + ' tweet.');
continue;
}
//Like tweet
Twitter.post('favorites/create', {
id: tId
}, function(err, response) {
if (response) {
console.log('Successfully liked ' + tweets[i].user.name + ' tweet.');
}
if (err) {
console.log(err);
}
});
//answer tweet
Twitter.post('statuses/update', {
status: '@'+tweets[i].user.screen_name+' '+getAnswer(),
in_reply_to_status_id: tweets[i].id_str
}, function(err, response) {
if (response) {
console.log('Successfully answered ' + tweets[i].user.name + ' tweet.');
}
if (err) {
console.log(err);
}
});
}
});
}
main();
setInterval(main, (60 * 60 * 1000));