Sending Tweets from Processing

In this section we are going to learn how to send tweets from Processing.

As a starting point, use the source code that resulted from the end of the Authenticating With Twitter part of the tutorial (you will not be able to complete this part of the tutorial without having completed previous sections).

WARNING: This tutorial will cause you to send tweets (real ones) from your Twitter account which will be visible to real people so think carefully before tweeting.

1. Checking that our application is set to read AND write information

We can’t tweet unless our application has the right to send tweets.

  1. Log in to dev.twitter.com
  2. In the top right of the page, hover over your avatar and click on ‘My applications’.
  3. Click on your application
  4. In the list of menu tabs, go to ‘Settings’
  5. Underneath ‘Application Type’, there is an option called ‘Access’. Check that this is set to ‘Read and Write’.
  6. Go to the ‘Details’ tab and check that your application is now set to be a ‘Read and Write’ application. NB. You may have to re-create your access token, which can be done at the bottom of the ‘Details’ page.

2. Writing a Function To Send Tweets

We are going to create a new function, called tweet().

void tweet()
{

}

Now we will add some code that will ‘try’ to send a tweet:

void tweet()
{
    try
    {
        Status status = twitter.updateStatus("This is a tweet sent from Processing!");
        System.out.println("Status updated to [" + status.getText() + "].");
    }
}

This will now attempt to update our status on Twitter. And it will print a message to the console if it worked.

However, we also have to deal with the case where it might not work, or the internet might be down or something else. To do that, we need to ‘catch’ any problems and handle them gracefully:

void tweet()
{
    try
    {
        Status status = twitter.updateStatus("This is a tweet sent from Processing!");
        System.out.println("Status updated to [" + status.getText() + "].");
    }
    catch (TwitterException te)
    {
        System.out.println("Error: "+ te.getMessage());
    }
}

Our function is now complete, but it won’t do anything until we call it. On to the next section…

3. Sending Tweets Using Keyboard Interaction

We will now add a simple function to Processing to send a tweet whenever we press a key:

void keyPressed()
{
    tweet();
}

And that is it – hopefully you should now be all set for creating your own Processing sketches that make use of Twitter.

4. Full Source

import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;

Twitter twitter;

void setup()
{
    size(800,600);

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("*****YOUR-KEY-HERE******");
    cb.setOAuthConsumerSecret("*************YOUR-KEY-HERE*************");
    cb.setOAuthAccessToken("*************YOUR-KEY-HERE***************");
    cb.setOAuthAccessTokenSecret("**********YOUR-KEY-HERE***************");

    TwitterFactory tf = new TwitterFactory(cb.build());

    twitter = tf.getInstance();
}

void draw()
{

}

void tweet()
{
    try
    {
        Status status = twitter.updateStatus("This is a tweet sent from Processing!");
        System.out.println("Status updated to [" + status.getText() + "].");
    }
    catch (TwitterException te)
    {
        System.out.println("Error: "+ te.getMessage());
    }
}

void keyPressed()
{
    tweet();
}