Display the most recent Tweet


Here we'll show you how to make a request to the Twitter API and then display the most tweet on any topic you input.

A screenshot of the finished sketch displaying a Tweet.

The finished sketch can display the most recent Tweet about whatever you're hungry for.

Set up with Temboo and Twitter

1Sign up or login in to a Twitter account.

2Create a Twitter application. You only need to fill in Name, Description, and Website. You can leave Callback URL blank.

3Go to the API Keys tab and click on the Create my access token button. This will generate the API keys that you need to run this example.

4Now, let's store these values in a Temboo Profile. Log in to Temboo and go to the Twitter > Search > Tweets Choreo in our Library. Copy the four API key values into the corresponding inputs on the Choreo page (note: API Key = Consumer Key, API Secret = Consumer Secret). Click on the Save Profile button, name your Profile, and save.

Now you can reuse these Twitter authentication details on any Twitter Choreo in our Library. It will also make your code a lot simpler.

Test the Choreo

5Let's test the Twitter > Search > Tweets Choreo directly from the Temboo Library. If they aren't already there, insert your saved Profile values. Click Run and you'll be able to see the response that you get back in the Output section.

6Next, copy the generated Code directly into your Processing sketch and run it there.

Get the Tweet from the JSON Response

7You can see that you got a lot of JSON back from the Choreo (showing up in your Processing console). Thankfully, Processing has some built in JSON parsing methods. You can drill down to the latest Tweet with the code below.

Copy this code into Processing and remember to substitute in your Temboo account details and the name of the Temboo Twitter Profile that you created earlier.

import com.temboo.core.*;
import com.temboo.Library.Twitter.Search.*;

// Create a session using your Temboo account application details
TembooSession session = new TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY");

// The name of your Temboo Twitter Profile 
String twitterProfile = "yourTwitterProfile";

// Set up text strings
String searchText, tweetText;

// Create a JSON object to store the search results
JSONObject searchResults;

void setup() {
  // Set a search term and instructions
  searchText = "sandwich";

  // Display initial tweet
  runTweetsChoreo(); // Run the Tweets Choreo function
  getTweetFromJSON(); // Parse the JSON response
  displayText(); // Display the response
}

void runTweetsChoreo() {
  // Create the Choreo object using your Temboo session
  Tweets tweetsChoreo = new Tweets(session);

  // Set Profile
  tweetsChoreo.setCredential(twitterProfile);

  // Set inputs
  tweetsChoreo.setQuery(searchText);

  // Run the Choreo and store the results
  TweetsResultSet tweetsResults = tweetsChoreo.run();
  
  // Store results in a JSON object
  searchResults = parseJSONObject(tweetsResults.getResponse());
}

void getTweetFromJSON() {
  JSONArray statuses = searchResults.getJSONArray("statuses"); // Create a JSON array of the Twitter statuses in the object
  JSONObject tweet = statuses.getJSONObject(0); // Grab the first tweet and put it in a JSON object
  tweetText = tweet.getString("text"); // Pull the tweet text from tweet JSON object
}

void displayText() {
  println(tweetText); // Print tweet to console
}

Display tweet text and update when a key is pressed

8Now that we've isolated the most recent Tweet, let's make the Sketch a little fancier. Some code in the draw() will update the Tweet whenever a key is pressed.

We are also adding the latest Tweet and instructional text to the display window (here's documentation on using fonts in Processing).

To see the finished version in action, copy the code below into Processing and remember to substitute in your Temboo account details and the name of the Temboo Twitter Profile that you created earlier.

import com.temboo.core.*;
import com.temboo.Library.Twitter.Search.*;

// Create a session using your Temboo account application details
TembooSession session = new TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY");

// The name of your Temboo Twitter Profile 
String twitterProfile = "yourTwitterProfile";

// Declare font and text strings
PFont fontTweet, fontInstructions;
String searchText, tweetText, instructionText;

// Create a JSON object to store the search results
JSONObject searchResults;

void setup() {
  size(700, 350);

  // Set up font
  fontTweet = createFont("Courier", 30);
  fontInstructions = createFont("Courier", 20);

  // Set a search term and instructions
  searchText = "sandwich";
  instructionText = "Press any key to load a new tweet about '"+searchText+"'";

  // Display initial tweet
  runTweetsChoreo(); // Run the Tweets Choreo function
  getTweetFromJSON(); // Parse the JSON response
  displayText(); // Display the response
}

void draw() {
  if (keyPressed) {
    runTweetsChoreo(); // Run the Tweets Choreo function
    getTweetFromJSON(); // Parse the JSON response
    displayText(); // Display the response
  }
}

void runTweetsChoreo() {
  // Create the Choreo object using your Temboo session
  Tweets tweetsChoreo = new Tweets(session);

  // Set Profile
  tweetsChoreo.setCredential(twitterProfile);

  // Set inputs
  tweetsChoreo.setQuery(searchText);

  // Run the Choreo and store the results
  TweetsResultSet tweetsResults = tweetsChoreo.run();
  
  // Store results in a JSON object
  searchResults = parseJSONObject(tweetsResults.getResponse());
}

void getTweetFromJSON() {
  JSONArray statuses = searchResults.getJSONArray("statuses"); // Create a JSON array of the Twitter statuses in the object
  JSONObject tweet = statuses.getJSONObject(0); // Grab the first tweet and put it in a JSON object
  tweetText = tweet.getString("text"); // Pull the tweet text from tweet JSON object
}

void displayText() {
  println(tweetText); // Print tweet to console

  int margin = 30; // Set margins for text box
  background(255); // Clear background

  // Display tweet
  fill(0); // Set font color
  textFont(fontTweet); // Set tweet font
  text(tweetText, margin, margin, width-margin*3, height-margin*3); // Display tweet text box

  // Display instructions
  fill(0); // Set instruction box color
  rect(0, height-margin*2, width, margin*2); // Draw instruction box rectangle
  fill(255); // Set font color
  textFont(fontInstructions); // Set instructions font
  text(instructionText, margin, height-margin); // Display instructions text
}

What next?

We're all finished! We have access to more Tweets and associated data (timestamp, user, etc.) in the JSON response that we could visualize. We could also bring in some animation – or other Choreos. Check out our Library for some more ideas.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Get Live Updates About Your Application

Integrate Temboo with Slack to get up-to-the-second notifications and alerts about your deployed Temboo code.

Commit Code to GitHub

Commit Temboo-generated code directly to GitHub and share with the world. Learn more.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back