Question

I want to perform certain actions when a particular user tweets something. So is it possible in twitter4j to listen to tweets from a particular user account and handle the event?

I know what the answer would be: read streaming api. But I think its too vast for my purpose and I only want to listen to tweets from a particular account.Hence I am asking it here.

Was it helpful?

Solution

You should try the sample code:

    TwitterStream twitterStream = new TwitterStreamFactory(new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance();

    twitterStream.setOAuthConsumer(_consumerKey, _consumerSecret);
    AccessToken token = new AccessToken(_accessToken, _accessTokenSecret);
    twitterStream.setOAuthAccessToken(token);

    StatusListener listener = new StatusListener() {
        public void onStatus(Status status) {
            logger.info(DataObjectFactory.getRawJSON(status));

            //do your action here
        }

        ...
    };

    twitterStream.addListener(listener);

    FilterQuery query = new FilterQuery();
    query.follow(new long[] { _twitterUserId });
    twitterStream.filter(query);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top