Reply to a user tweet using Twitter4j

24 / Sep / 2012 by Vishal Sahu 1 comments

Hi,

In one of my grails project, i worked on integrating twitter API with the grails application. The requirement is such that we need to display all of the tweets to any user in our application and provide the basic functionality such as reply, retweet, favorite, send direct message etc using the API calls. I used Twitter4j (a java wrapper for twitter API calls), to make API calls.


In the previous blog, we saw how to send ‘Direct Message’ to any twitter user using Twitter4j. In this blog we will see how to replay to any particular tweet using the same library


For making any twitter API calls we need to have twitter account access_token and access_secret, which we obtains after authorizing/connecting twitter account with the application as mentioned in this blog.


Pre-Requirements:-

       String consumerKey = CONSUMER_KEY // key obtained after registering app.
       String consumerSecret =CONSUMER_SECRET // secret key obtained from the registered app.
       String twitterToken = USER_TWITTER_TOKEN
       // access_token received by authentication user's twitter account
       String twitterSecret= USER_TWITTER_SECRET
        // access_secret obtained by authentication user's twitter account
       String replyMessage='Hi, this is just a test message.'
       Long inReplyToStatusId = // messageId of the tweet to which the user is replying


Code to Reply to Twitter User:-


        TwitterFactory factory = new TwitterFactory()
        Twitter twitter = factory.getInstance()
        twitter.setOAuthConsumer(consumerKey, consumerSecret)
        AccessToken accessToken = new AccessToken(twitterToken, twitterSecret)
        twitter.setOAuthAccessToken(accessToken)
        StatusUpdate statusUpdate = new StatusUpdate(replyMessage)
        statusUpdate.inReplyToStatusId = inReplyToStatusId
        Status status = twitter.updateStatus(statusUpdate)
    }

So, the above code will send a Reply tweet to the twitter .
This worked for me.
Hope it helps.


Other Blogs:-

Send Direct Message using Twitter4j
Mark Tweet as Favorite using Twitter4j
Retweet a tweet using Twitter4j
Post status update using Twitter4j


Cheers!!!
Vishal Sahu
vishal@intelligrape.com

FOUND THIS USEFUL? SHARE IT

comments (1 “Reply to a user tweet using Twitter4j”)

  1. Amaan Memon

    inReplyToStatusId has private access in StatusUpdate

    statusUpdate.inReplyToStatusId = inReplyToStatusId;
    ^

    Reply

Leave a comment -