Replying to users who tweets your account using Tw

2019-03-04 17:13发布

I'm using Twitter4J to send tweets automatically. Basically what I want to do is:

If a user tweets my account, I'll reply with "Hello hello!" for example. I know how to tweet using the Twitter4J but just don't know how to reply to users that tweet me.

This is the way I'd just do a normal tweet:

// The factory instance is re-useable and thread safe.
Twitter twitter1 = TwitterFactory.getSingleton();
Status status = twitter1.updateStatus("Hello Hello");
System.out.println("Successfully updated the status to [" + status.getText() + "].");

I think I need to do: 1. Request last tweet 2. If newTweet.id != oldTweet.id then it means you have a new message else jump to step 4 3. Reply 4. Loop 1

1条回答
Lonely孤独者°
2楼-- · 2019-03-04 17:26

Don't use rest api here. Use streaming api (Follow stream). Follow your own handle.

public void onStatus(Status status) {
  System.out.println("onStatus @" + status.getUser().getScreenName() + " - "+ status.getText());
  System.out.println(status.getInReplyToUserId());
  Twitter tf = new TwitterFactory().getInstance();
  StatusUpdate st = new StatusUpdate("hello");
  st.inReplyToStatusId(status.getId());
  try {
    tf.updateStatus(st);
  } catch (TwitterException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
}
查看更多
登录 后发表回答