简体   繁体   中英

Twitter4j Listen to tweets from a particular user

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM