Below is code for the start of a Twitter bot I am going to build in Java. It’s the most basic way of getting Oauth working (with any account, not just your developer account) and it shows your timeline and can update your status – that’s it for now. The neat thing is it uses Java’s awesome serialisation, so you only should have to authorise your twitter account once!
What you will need:
- Twitter4j Libraries
- Oauth Consumer and Secret Key off twitter
- That’s it!
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;
import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
public class DFTwitterBot implements Serializable {
private Twitter twitter;
private AccessToken aT;
private long aTID;
public static void main(String args[]) throws Exception{
DFTwitterBot dftb = new DFTwitterBot();
File file=new File("twitterdetails.dftb");
boolean exists = file.exists();
if (exists) {
dftb = readObject(dftb);
}else if(!exists){
getTwitterAccess(dftb);
}
menu(dftb);
}
private static void menu(DFTwitterBot dftb) throws TwitterException, IOException {
System.out.println("DF Bot Menu");
System.out.println("1. Update Status");
System.out.println("2. View Timeline");
System.out.println("3. Exit");
System.out.println("Please enter your choice:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String option = br.readLine();
int a =Integer.parseInt( option );
switch(a){
case 1: updateStatus(dftb);
case 2: viewTimeline(dftb);
case 3: System.out.println("Bye!"); System.exit(0);
default: menu(dftb);
}
}
private static void viewTimeline(DFTwitterBot dftb) throws TwitterException {
List statuses = dftb.twitter.getFriendsTimeline();
System.out.println("Showing friends timeline.");
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":" +
status.getText());
}
}
private static DFTwitterBot readObject(DFTwitterBot dftb) {
try
{
FileInputStream fileIn =
new FileInputStream("twitterdetails.dftb");
ObjectInputStream in = new ObjectInputStream(fileIn);
dftb = (DFTwitterBot) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return dftb;
}catch(ClassNotFoundException c)
{
System.out.println(" DFTB class not found");
c.printStackTrace();
return dftb;
}
System.out.println("Deserialized Access...");
System.out.println("Access Token ID: " + dftb.aTID);
System.out.println("Access Token: " + dftb.aT.toString());
System.out.println("Tweitter: " + dftb.twitter.toString());
return dftb;
}
private static void updateStatus(DFTwitterBot dftb) throws TwitterException, IOException {
String statusUpdate = null;
System.out.println("Please enter a status:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
statusUpdate = br.readLine();
if(statusUpdate.length() >0){
StatusUpdate su = new StatusUpdate(statusUpdate);
Status status = dftb.twitter.updateStatus(su);
System.out.println("Successfully updated the status to [" + status.getText() + "].");
} else{
menu(dftb);
}
}
private static void getTwitterAccess(DFTwitterBot dftb) throws IOException, TwitterException {
// The factory instance is re-useable and thread safe.
dftb.twitter = new TwitterFactory().getInstance();
dftb.twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");
RequestToken requestToken = dftb.twitter.getOAuthRequestToken();
dftb.aT = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (null == dftb.aT) {
System.out.println("Open the following URL and grant access to your account:");
System.out.println(requestToken.getAuthorizationURL());
System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
String pin = br.readLine();
try{
if(pin.length() > 0){
dftb.aT = dftb.twitter.getOAuthAccessToken(requestToken, pin);
}else{
dftb.aT = dftb.twitter.getOAuthAccessToken();
}
} catch (TwitterException te) {
if(401 == te.getStatusCode()){
System.out.println("Unable to get the access token.");
}else{
te.printStackTrace();
}
}
}
dftb.aTID = dftb.twitter.verifyCredentials().getId();
writeObject(dftb);
//System.exit(0);
}
private static void writeObject(DFTwitterBot dftb) throws IOException {
FileOutputStream fileOut =
new FileOutputStream("twitterdetails.dftb");
ObjectOutputStream out =
new ObjectOutputStream(fileOut);
System.out.println("Serialising:...");
System.out.println("Access Token ID: " + dftb.aTID);
System.out.println("Access Token: " + dftb.aT.toString());
System.out.println("Tweitter: " + dftb.twitter.toString());
out.writeObject(dftb);
out.close();
fileOut.close();
}
}
What the code does:
- Once only:
- Gives the user an url to visit
- User enters a PIN provided by Twitter back in to the program
- The program gets the proper keys for the account from Twittter and saves them as a file
- Everytime the program is restarted it reloads in account details (if the user gave permissions in steps above – it is not redone)
- User is provided with a Menu and either can update status or show timeline
I started off with this code example, so much props to Twitter4J team for a great library!
53.424935
-7.944940