Last Updated: February 25, 2016
·
645
· fabax

Sending tweets using Processing

import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.json.*;
import twitter4j.internal.util.*;
import twitter4j.management.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import twitter4j.internal.json.*;
import java.util.*;

Twitter twitter;
String tweetMessage = "tweet";
String directMessage = "message de test";
String reveiver = "fabax1";

void setup(){
    twitterConfiguration();
}

void draw(){
}

void tweet(String _tweetMessage){
    try {
        Status status = twitter.updateStatus(_tweetMessage);
        println("Status updated to [" + status.getText() + "].");
    }catch (TwitterException te){
        System.out.println("Error: "+ te.getMessage()); 
    }
}

void directMessage(String _reveiver ,String _directMessage){
  try {
        twitter.sendDirectMessage(_reveiver,_directMessage);
        println("Direct message sent");
    }catch (TwitterException te){
        System.out.println("Error: "+ te.getMessage()); 
    }
}

void keyPressed(){
    if(key == 't' || key =='T'){
        tweet(tweetMessage);
    }

    if(key == 'd' || key =='D'){
        directMessage(reveiver,directMessage);
    }

}

void twitterConfiguration(){
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("XXXX");
    cb.setOAuthConsumerSecret("XXXX");
    cb.setOAuthAccessToken("XXXX");
    cb.setOAuthAccessTokenSecret("XXXX");
    TwitterFactory tf = new TwitterFactory(cb.build());
    twitter = tf.getInstance();
}