-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_bot_class.js
73 lines (60 loc) · 2.02 KB
/
twitter_bot_class.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
//import library
var twit = require('twit');
class TwitterBot {
constructor(consumerKey, consumerSecret, accessToken, accessTokenSecret, userID) {
this.consumerKey= consumerKey;
this.consumerSecret=consumerSecret;
this.accessToken = accessToken;
this.accessTokenSecret=accessTokenSecret;
this.userID=userID;
}
tweet(){
//instantiate new twit object
var credentials= {
consumer_key: this.consumerKey,
consumer_secret: this.consumerSecret,
access_token: this.accessToken,
access_token_secret: this.accessTokenSecret,
};
var Twitter = new twit(credentials);
var userID= this.userID; // twitter user id of who to follow activity for
var stream = Twitter.stream('statuses/filter', { follow: ( userID ) });
console.log("Twitter bot is starting");
stream.on('tweet', function (tweet) {
// compare the user ID inside the Tweet object we passed in
// to check it matches
if (tweet.user.id == userID) {
var tweet_id = tweet.id_str;
var name = tweet.user.screen_name;
var reply = "wow such a great tweet";
// example of how to see if a tweet contains a word
if(tweet.text.includes('apple')){
console.log('yo dude')
}
//comment on tweet
Twitter.post('statuses/update', {in_reply_to_status_id: tweet_id, status: reply + ' '+ ' @' + name}, function(err, data, response) {
if (err){
console.log(`received error on comment: ${err}`)
}
console.log("commenting")
})
// retweet
Twitter.post('statuses/retweet/:id', { id: tweet_id }, function (err, data, response) {
if (err){
console.log(`received error on retweet: ${err}`)
}
console.log("retweeting")
})
// Like the post
Twitter.post('favorites/create', {id: tweet_id}, function (err, data, response) {
if (err){
console.log(`received error on favoriting post: ${err}`)
}
console.log("favoriting post")
})
}
});
}
}
//allow other files to be able to access the TwitterBot object
module.exports = TwitterBot;