Communicating with Twitter via Twitter4J using Scala


Twitter4J is a Java library for the Twitter API. With Twitter4J, you can easily integrate your Scala application with the Twitter service.

Twitter4J is featuring:

- Easy to use with Scala (as it works on any Java Platform version 5 or later)
- Support via a single jar file.
- Built-in OAuth support.

Here is the tutorial to use Twitter4J in Scala.

1. Download the Twitter4J jar file here.

2. Setup the TwitterFactory Object with your Twitter application credentials.

val twitter = new TwitterFactory().getInstance()
  // Authorising with your Twitter Application credentials
  twitter.setOAuthConsumer("consumer_key",
    "consumer_secret")
  twitter.setOAuthAccessToken(new AccessToken(
    "access_key",
    "access_token"))

3. Thats it ! .Now we are ready to interact with Twitter.

Posting a Tweet :

twitter.updateStatus("Hello , This is a sample tweet")

Capturing the Tweets from your timeline :

val statuses = twitter.getHomeTimeline


Sending direct messages to recipients :

val message = twitter.sendDirectMessage("twitter_user_id/name", "Hello")
println("Sent: " + message.getText + " to @" + message.getRecipientScreenName)

Querying the Twitter :

  val query = new Query("Day in Day out Knoldus Software :)");
  val result = twitter.search(query)
  println("Tweetes Found :" + result.getTweets.size)

Getting Direct Messages :

 val directMessages = twitter.getDirectMessages
 println(" Direct Messages Found : "+directMessages)
 val favorites = twitter.getFavorites
 println("Favorites : "+ favorites)

& much more…..

There are several more methods present for a complete interaction with Twitter. Find more

Here’s the Scala code file :

 import twitter4j.Twitter
import twitter4j.TwitterFactory
import twitter4j.auth.AccessToken
import twitter4j.ResponseList
import twitter4j.QueryResult
import twitter4j.Query
import twitter4j.Paging

object Twitter4JWithScala extends App {

  val twitter = new TwitterFactory().getInstance()

  // Authorising with your Twitter Application credentials
  twitter.setOAuthConsumer("consumer_key",
    "consumer_secret")
  twitter.setOAuthAccessToken(new AccessToken(
    "access_key",
    "access_token"))

  // Posting a Tweet
  twitter.updateStatus("Hello , This is a sample tweet")

  // Capturing the Tweets from your timeline
  val statuses = twitter.getHomeTimeline

  // Sending direct messages to recipients
  val message = twitter.sendDirectMessage("twitter_user_id/name", "Hello")
  println("Sent: " + message.getText + " to @" + message.getRecipientScreenName)

  //Querying the Twitter
  val query = new Query("Day in Day out Knoldus Software :)");
  val result = twitter.search(query)
  println("Tweetes Found :" + result.getTweets.size)

  // Getting Direct Messages & Favorites
  val directMessages = twitter.getDirectMessages
  println(" Direct Messages Found : " + directMessages)
  val favorites = twitter.getFavorites
  println("Favorites : " + favorites)
}

About Neelkanth Sachdeva

Software Consultant @ Knoldus Software LLP Co-Founder @ My Cell Was Stolen
This entry was posted in Java, Scala, Web and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s