Java Twitch Chat Bot Question

So I’m currently trying to code a java twitch chat bot, and I want to set a command to set the current game to whatever I want. The way I’m currently doing this is something like this:

String currentGame = "Runescape";
public void onMessage(String channel, String sender, String login, String hostname, String message) {
    if (message.equalsIgnoreCase("!currentgame")) {
    	sendMessage(channel, sender + ": The current game is " + currentGame);
    }
}

So there’s a few things I want to addon to this for increased functionality but I’m not really sure where to begin. Firstly, I’d like to add a command “!setgame “game”” that allows me to set the currentGame variable to something else. The other thing I’d like to do is actually change the current game on my stream (Through twitch API) but I’m not sure how I’d do this. Any help would be highly appreciated, thanks!

To update the game on Twitch, use the channels/{channel ID} Kraken endpoint. You would add a JSON body to your request with the information you want to update. So, an example in C# using RestSharp would look something like this:

string game = "Dark Souls III";

RestRequest request = Request("channels/{channel_id}", Method.PUT);
request.AddUrlSegment("channel_id", "<channel ID here>");
request.RequestFormat = DataFormat.Json;
request.AddBody(new { channel = new { game } });

So the serialized object being added to the web request behind the scenes if you weren’t using a library would be:

"{\"channel\":{\"game\":\"Dark Souls III\"}}"

And then to get the game to cache with your bot, you would use the channels/{channel ID} Kraken endpoint. It’s the same endpoint as the one to update the game, but takes different input parameters. Another example using C# and RestSharp to form the request:

RestRequest request = Request("channels/{channel_id}", Method.GET);
request.AddUrlSegment("channel_id", "<channel ID here>");

How can I make this connection in Java?

After a quick search looks like you would want to use these native libraries:

java.net.HttpURLConnection           // No SSL
java.net.HttpsURLConnection          // SSL

Another option would be to use a ready made library that can simplify the process of making the requests for you:
https://jersey.github.io/documentation/latest/index.html

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.