Hello, I’m writing a twitter bot in Java language.

From what I’ve understood, we need to create a set of rules and then define a stream.

There is no example on the Java implementation of how to achieve this: GitHub - twitterdev/twitter-api-java-sdk: A Java SDK for the Twitter API and I’m still not understanding how to achieve this.

As future reference for other people having the same doubt, can some kind soul provide a source code example for:

  1. reacting when the bot is mentioned @something at a conversation
  2. receives a private message

Many thanks in advance!

Have you tried this for creating rules? twitter-api-java-sdk/TweetsApi.md at main · twitterdev/twitter-api-java-sdk · GitHub

2 Likes

Thanks for the reply, that helps to get a sample syntax for rules.

Answering your question: No, did not try that because it was not trivial for me to find its existence.

Would expect a code example to be found at the code examples folder: twitter-api-java-sdk/examples at main · twitterdev/twitter-api-java-sdk · GitHub

“rules” is something very specific to twitter, also couldn’t find a code example how to use it. To help others, there is an explanation here which includes java code:

A deeper explanation of the rules that are available:

@IgorBrigadir, what happens after we set the rules? Should we pool the twitter API every two seconds or there is way to only trigger an action when the rule applies on the stream? Can you also point to a code example of this?

Many thanks.

No, the rules endpoint is separate to everything, you can freely add remove rules as you need. In a separate process, you open a streaming connection and consume the data, dandling it somehow tweet by tweet. This is like downloading an infinitely large file. There’s a commented out block of code here as an example:

1 Like

Thanks but that is over-complicating what should be simple. The topic here is a basic demonstrator of the Rule and Streaming functionality.

Anyways thank you for the links. I eventually found an example implementation that is functional and good as starting point in case it helps others in the future: final-assignment/RecentTweetSearcher.java at 4b32d5ff87504bfb7e1654bf1f3d021fd70bc9be · PSPD-2022-1/final-assignment · GitHub

It would help to have this kind of example on the source code folder. Thank you.

1 Like