Search code examples
javatwittertwitter4j

Twitter4j reply to tweet not working


I'm trying to reply to a tweet (that i post 10 seconds before) using twitter4j but it is only posting it to the timeline and not as a reply.

post = mainTwitter.updateStatus(...); //1st twitter account
StatusUpdate reply = new StatusUpdate(...);
try {
   reply.setInReplyToStatusId(post.getId());
   contextTwitter.updateStatus(reply); //2nd twitter account
} catch (TwitterException e) {
   System.err.println("Couldn't post context");
   e.printStackTrace();
}

The id is not -1, it is the right long value. No exception thrown.


Solution

  • You can use the following code to make your snippet working

    public void init() {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
          .setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
          .setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
          .setOAuthAccessToken(TWITTER_ACCESS_TOKEN)
          .setOAuthAccessTokenSecret(TWITTER_ACCESS_SECRET);
        TwitterFactory tf = new TwitterFactory(cb.build());
        twitter = tf.getInstance();
    }
    
    
    /***
     * 
     * @param tweetId : tweet Id
     * @param messgae : Reply message
     * @return
     * @throws TwitterException
     */
    public String reply(String tweetId, String messgae ) throws TwitterException {
            Status status = twitter.showStatus(Long.parseLong(tweetId));
            Status reply = twitter.updateStatus(new StatusUpdate(" @" + status.getUser().getScreenName() + " "+ messgae).inReplyToStatusId(status.getId()));
        return Long.toString(reply.getId());
    }
    

    Usage :

    reply("930497966443454464", " Hello Eva")