Skip to content

TheNitram21/JTA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Release

JTA (Java Twitch API)

A Java Wrapper for the Twitch API.

Summary

  1. Download
  2. Building your first Bot
  3. Sending Messages
  4. Events
  5. Versioning
  6. Contributing
  7. Official Guide

Download

Currently, to get the API, you need to go to JitPack and get the dependency there. In the future, I may switch to another maven repository.

Building your first Bot

First of all, go to the Twitch Developer Console and create an application. 2FA (Two Factor Authentication) has to be enabled for that. Then you can start coding.

We tried to make the library as easy as possible, so to create a bot, you only need to do the following things:

  1. Initialize the library using JTA.initialize().
  2. Create a bot instance using the builder. This looks like this:
public static void main(String[] args) {
  JTA.initialize();
  
  JTABot bot = JTABotBuilder.create("CLIENTID", "CLIENTSECRET").build();
}

This creates a bot instance, however, you see the two Strings. To make it work, you need to replace them. Replace CLIENTID with your client id and CLIENTSECRET with your client secret.

Sending Messages

To send a message, you first need to get the chat of a user's channel. Then connect to the chat. Now you can send the message.

public static void main(String[] args) {
  // Initialize the library.
  JTA.initialize();
  
  // Create a bot.
  JTABot bot = JTABotBuilder.create("CLIENTID", "CLIENTSECRET").build();
  
  // Set the chat OAuth token.
  bot.setChatOAuthToken("OAUTH");
  
  // Get the user. In this case, I chose myself.
  User user = bot.getUserByName("TheNitram21");
  
  // Get the chat of the user's channel.
  Chat chat = user.getChannel().getChat();
  
  try {
    // Connect to the chat.
    chat.connect(false);
    
    // Send the message.
    chat.sendMessage("Hi KonCha");
    
    // Disconnect from chat.
    chat.disconnect();
  } catch(IrcException | IOExcecption e) {
    e.printStackTrace();
  }
}

Here you can again see a String you need to fill in, OAUTH. You need to put the OAuth-Chat-Token of your user in there. Log into twitch with the user which should send the messages, then go to Twitchapps and get your token there.

Events

To receive messages, you need an event listener. To create one, simply create a class implementing Listener. Then add it to the bot.

public static void main(String[] args) {
  // Initialize the library.
  JTA.initialize();
  
  // Create a bot.
  JTABot bot = JTABotBuilder.create("CLIENTID", "CLIENTSECRET").build();
  
  // Set the chat OAuth token.
  bot.setChatOAuthToken("OAUTH");
  
  // Add the listener.
  bot.addEventListeners(new MessageListener());
  
  // Get the user. In this case, I chose myself.
  User user = bot.getUserByName("TheNitram21");
  
  // Get the chat of the user.
  Chat chat = user.getChannel().getChat();
  
  try {
    // Connect to the chat.
    chat.connect(false);
    
    // Send the message.
    chat.sendMessage("Hi KonCha");
  } catch(IrcException | IOExcecption e) {
    e.printStackTrace();
  }
}

public static class MessageListener implements Listener {
  @Override
  public void onMessageReceived(MessageReceivedEvent event) {
    // Print the event.
    System.out.println(event.toString());
  }
}

In this code example, you may ask: "Why don't we disconnect from the chat?" That's because you can only receive messages from chats you're connected to. Imagine getting all messages from a 100k viewers streamer! (Your computer would make pew)

Clips

To get a clip, you only need to call JTABot#getClipBySlug(String) and you have your clip.

public static void main(String[] args) {
    // Create the bot
    JTABot bot = JTABotBuilder.create(clientId, clientSecret).build();
    
    // Get the clip
    Clip clip = bot.getClipBySlug("EnticingCorrectDelicataDansGame-ZjHJQXu6ob2R-j19");
    
    // Print the clip information
    System.out.println("Clip '" + clip.getSlug() + "': " + clip.getTitle());
}

Versioning

Here is how the versioning works: Big changes to the API: Major release. Add or redo features: Minor release. Add or methods: Patch release. Redo or fix methods: ID release.

Contributing

You can find a detailed manual for how to contribute here.

Official Guide

You can find the official guide on my GitHub Pages page, or click here