-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot.js
153 lines (119 loc) · 4.11 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*!
* Bot.js
* A Node JS Twitter bot that periodically searches for tweets and then like, reply and follow automatically.
* The bot saves tweets ids already processed to avoid interact with the same ones again.
* Version 1.0.0
* Created by Carlos E. Torres (carlos.torres@cacira.com)
* http://cacira.com
* http://cetorres.com
* Date: 02/09/2017
*/
// Create object reference to libs
var Twit = require('twit');
var fs = require('fs');
// Set Twitter API keys
var TWITTER_CONSUMER_KEY = '';
var TWITTER_CONSUMER_SECRET = '';
var TWITTER_ACCESS_TOKEN = '';
var TWITTER_ACCESS_TOKEN_SECRET = '';
// Database file
var DB_FILE = "bot_db.txt";
// Set interval time. Try to use a not so small interval to avoid Twitter to lock your account.
var INTERVAL = 3*60*60*1000; // 3 hours
// Set Twitter search phrase. You can use hash tags or simples text. Hash tags works better. Separate with OR or AND.
var TWITTER_SEARCH_PHRASE = '#AGoodHashTag OR #AnotherOne OR #MyGreatSearch';
// Set max number of tweets to get on the search results each time
var TWITTER_SEARCH_MAX_RESULTS = 20;
// Set tweets to reply
var TWEETS_TO_REPLY = [
"Write something to reply.",
"Bot will randomly pick one of the tweets to send.",
"Try to be very natural and human like on the replies.",
"Use the main Twitter handle you want to promote in all options. @MyHandle",
"Create 5 to 10 replies. The more options the more natural bot will look."
];
// Init Twit lib
var Bot = new Twit({
consumer_key: TWITTER_CONSUMER_KEY,
consumer_secret: TWITTER_CONSUMER_SECRET,
access_token: TWITTER_ACCESS_TOKEN,
access_token_secret: TWITTER_ACCESS_TOKEN_SECRET
});
function BotStart() {
var query = {
q: TWITTER_SEARCH_PHRASE,
result_type: "recent",
lang: 'en',
count: TWITTER_SEARCH_MAX_RESULTS
}
console.log("> Twitter bot is running (" + Date() + ")...")
Bot.get('search/tweets', query, BotQueryResults);
function BotQueryResults (error, data, response) {
if (error) {
console.log('Bot could not find tweets matching the query: ' + error);
}
else {
// DB file
var bot_db = [];
// If db file doesn't exist, create empty file
if (!fs.existsSync(DB_FILE)) {
fs.closeSync(fs.openSync(DB_FILE, 'w'));
}
fs.readFile(DB_FILE, 'utf8', function (err, fileData) {
if (!err) {
fileData = fileData.trim();
if (fileData != "") {
bot_db = fileData.split("\n");
}
var processed_tweets = [];
for (var i = 0; i < data.statuses.length; i++) {
// Tweet id
var id = data.statuses[i].id_str;
// User id and handle
var userId = data.statuses[i].user.id_str;
var userHandle = data.statuses[i].user.screen_name;
// If id doesn't exist on database, process it
if (bot_db.indexOf(id) == -1) {
processed_tweets.push(id);
fs.appendFile(DB_FILE, id + "\n", function (err) {
if (err) {
console.log("Error on save to '" + DB_FILE + "' file.");
}
});
// Like
Bot.post('favorites/create', {id: id}, function(err, response){
if (err) {
console.log("> Error: Tweet " + id + " could not be favorited. " + err);
}
});
// Reply
var textToReply = TWEETS_TO_REPLY[Math.floor(Math.random()*TWEETS_TO_REPLY.length)];
textToReply = "Hey @" + userHandle + ". " + textToReply;
Bot.post('statuses/update', {status: textToReply, in_reply_to_status_id: id}, function(err, response){
if (err) {
console.log("> Error: Status could not be updated. " + err);
}
});
// Follow
Bot.post('friendships/create', {user_id: userId, follow: "true"}, function(err, response){
if (err) {
console.log("> Error: Could not follow user " + userId + ". " + err);
}
});
}
}
// Log of processed tweets
if (processed_tweets.length > 0) {
console.log("> Tweets processed: " + processed_tweets);
}
else {
console.log("> No tweets processed.");
}
}
});
}
}
}
// Start bot and timer
BotStart();
setInterval(BotStart, INTERVAL);