-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
TwitterBot.java
154 lines (123 loc) · 4.4 KB
/
TwitterBot.java
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
154
package github.nisrulz.bot;
import java.util.ArrayList;
import java.util.List;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
public class TwitterBot {
static boolean debug = false;
// if something goes wrong, we might see a TwitterException
public static void main(String... args) {
// send a tweet
if (!debug) {
try {
// Send Tweet
// sendTweet("Hello World!");
// Get tweets from Home Timeline
// getHomeTimeLine();
// Seach for tweets
// searchForTweets("@gdg_nd");
// Reply to a tweet
String query_text = "\"your welcome\"";
String reply = "I believe you meant \"you're\" here?";
replyToTweet(query_text, reply);
// Reply with variety
List<String> searches = new ArrayList<>();
searches.add("\"your welcome\"");
searches.add("\"your the\"");
searches.add("\"your a \"");
List<String> replies = new ArrayList<>();
replies.add("I believe you meant \"you're\" here?");
replies.add(" I've detected the wrong \"you're\". Destroy!");
replies.add(" No, you are! Seriously. You are. \"You're\".");
replyToTweetWithVariety(searches, replies);
} catch (TwitterException e) {
e.printStackTrace();
}
} else {
// print a message so we know when it finishes
System.out.println("Debug Mode Enabled!");
}
}
private static Status sendTweet(String text) throws TwitterException {
// access the twitter API using your twitter4j.properties file
// The factory instance is re-useable and thread safe.
Twitter twitter = TwitterFactory.getSingleton();
Status status = null;
status = twitter.updateStatus(text);
System.out.println("Successfully updated the status to ["
+ status.getText() + "].");
return status;
}
private static void getHomeTimeLine() throws TwitterException {
Twitter twitter = TwitterFactory.getSingleton();
List<Status> statuses = null;
statuses = twitter.getHomeTimeline();
System.out.println("Showing home timeline.");
if (statuses != null) {
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":"
+ status.getText());
}
}
}
private static void searchForTweets(String query_text)
throws TwitterException {
Twitter twitter = TwitterFactory.getSingleton();
Query query = new Query(query_text);
QueryResult result;
result = twitter.search(query);
for (Status status : result.getTweets()) {
System.out.println("@" + status.getUser().getScreenName() + ":"
+ status.getText());
}
}
private static void replyToTweet(String query_text, String reply)
throws TwitterException {
// access the twitter API using your twitter4j.properties file
Twitter twitter = TwitterFactory.getSingleton();
// create a new search
Query query = new Query(query_text);
// get the results from that search
QueryResult result = twitter.search(query);
// get the first tweet from those results
Status tweetResult = result.getTweets().get(0);
// reply to that tweet
StatusUpdate statusUpdate = new StatusUpdate(".@"
+ tweetResult.getUser().getScreenName() + " " + reply);
statusUpdate.inReplyToStatusId(tweetResult.getId());
Status status = twitter.updateStatus(statusUpdate);
}
private static void replyToTweetWithVariety(List<String> searches,
List<String> replies) throws TwitterException {
// access the twitter API using your twitter4j.properties file
Twitter twitter = TwitterFactory.getSingleton();
// keep tweeting forever
while (true) {
// create a new search, chosoe from random searches
Query query = new Query(searches.get((int) (searches.size() * Math
.random())));
// get the results from that search
QueryResult result = twitter.search(query);
// get the first tweet from those results
Status tweetResult = result.getTweets().get(0);
// reply to that tweet, choose from random replies
StatusUpdate statusUpdate = new StatusUpdate(".@"
+ tweetResult.getUser().getScreenName()
+ replies.get((int) (replies.size() * Math.random())));
statusUpdate.inReplyToStatusId(tweetResult.getId());
Status status = twitter.updateStatus(statusUpdate);
System.out.println("Sleeping.");
// go to sleep for an hour
try {
Thread.sleep(60 * 60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}