Search code examples
javatwittertwitter4j

Posting twitter thread using Twitter4j


I'm trying to figure out how to use Twitter4J to post a tweet thread using Twitter4J. I'm guessing it would be somehow using the StatusUpdate class, but the documentation is a bit sparse. Any pointers?


Solution

  • You should set inReplyToStatusId as latest posted status id for every tweet coming after first tweet. Default value for inReplyToStatusId is -1.

    Example:

    long inReplyToStatusId = -1
    int counter = 0
    int threadLimit = 5
    
    while (counter < threadLimit){
        StatusUpdate statusUpdate = new StatusUpdate(Integer.toString(counter));
        statusUpdate.setInReplyToStatusId(inReplyToStatusId);
    
        Status updatedStatus = twitter.updateStatus(statusUpdate);
        inReplyToStatusId = updatedStatus.getId();
        counter++;
    }