-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bot.java
95 lines (85 loc) · 3.47 KB
/
Bot.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
package bot;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import java.util.List;
import twitter4j.*;
public class Bot{
public static void main(String[] args)
{ //call the function which you want
NewTweet();
gettimline();
gettweet();
reply();
}
public static void NewTweet()
{ //making a tweet from your account
Twitter twitter = TwitterFactory.getSingleton();
String mytweet="Hello, this UPES DevOps Bot";
try
{
Status status = twitter.updateStatus(mytweet);
System.out.println("successful"+status.getText());
}
catch(TwitterException e){
e.printStackTrace();
}
}
public static void gettimline(){
try {
// gets Twitter instance with default credentials
Twitter twitter = new TwitterFactory().getInstance();
User user = twitter.verifyCredentials();
List<Status> statuses = twitter.getHomeTimeline();
System.out.println("Showing @" + user.getScreenName() + "'s home timeline.");//getting all the latest tweets from your timeline
for (Status status : statuses) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
}
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get timeline: " + te.getMessage());
System.exit(-1);
}
}
public static void gettweet(){
Twitter twitter = new TwitterFactory().getInstance();
try {
Query query = new Query("#DevOpsAtUPES");//searching for tweets
QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
long Id = tweet.getId();
// System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText()+" "+"tweet id:"+ Id);
// twitter.retweetStatus(Id);
twitter.retweetStatus(Id);//retweet type1
//retweetStatus(Id);
}
} while ((query = result.nextQuery()) != null);
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}
public static Status retweetStatus(long Id) throws TwitterException {//retweet type 2
Twitter twitter = new TwitterFactory().getInstance();
return twitter.retweetStatus(Id);
}
public static void reply(){
Twitter twitter = TwitterFactory.getSingleton();
try {
long inReplyToStatusId = 1252942945612775425l;
StatusUpdate statusUpdate = new StatusUpdate("Bot replying to my tweet attempt"); // that message which you want to reply
statusUpdate.inReplyToStatusId(inReplyToStatusId);
Status status = twitter.updateStatus(statusUpdate);
System.out.println("Successfully updated the status to [" + status.getText() + "].");
}
catch(TwitterException e) {
System.err.println("Send tweet: " + e + " Status code: " + e.getStatusCode());
}
}
}