17 KiB
Introduction
A Java library based on TikTokLive and TikTokLiveSharp. Use it to receive live stream events such as comments and gifts in realtime from TikTok LIVE by connecting to TikTok's internal WebCast push service.
The library includes a wrapper that connects to the WebCast service using just the username (uniqueId). This allows you to connect to your own live chat as well as the live chat of other streamers.
No credentials are required. Events such as Members Joining, Gifts, Subscriptions, Viewers, Follows, Shares, Questions, Likes and Battles can be tracked.
Join the support discord and visit the #java-support channel for questions, contributions and ideas. Feel free to make pull requests with missing/new features, fixes, etc
Do you prefer other programming languages?
- Node orginal: TikTok-Live-Connector by @zerodytrash
- Python rewrite: TikTokLive by @isaackogan
- Go rewrite: GoTikTokLive by @Davincible
- C# rewrite: TikTokLiveSharp by @frankvHoof93
NOTE: This is not an official API. It's a reverse engineering project.
Overview
Getting started
- Install the package via Maven
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.jwdeveloper.TikTok-Live-Java</groupId>
<artifactId>Client</artifactId>
<version>NOT_FOUND</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
- Create your first chat connection
package io.github.jwdeveloper.tiktok;
import io.github.jwdeveloper.tiktok.utils.ConsoleColors;
import java.io.IOException;
import java.time.Duration;
import java.util.logging.Level;
public class SimpleExample
{
public static String TIKTOK_HOSTNAME = "szwagierkaqueen";
public static void main(String[] args) throws IOException {
showLogo();
// set tiktok username
TikTokLive.newClient(SimpleExample.TIKTOK_HOSTNAME)
.configure(clientSettings ->
{
clientSettings.setHostName(SimpleExample.TIKTOK_HOSTNAME); // This method is useful in case you want change hostname later
clientSettings.setClientLanguage("en"); // Language
clientSettings.setTimeout(Duration.ofSeconds(2)); // Connection timeout
clientSettings.setLogLevel(Level.ALL); // Log level
clientSettings.setPrintToConsole(true); // Printing all logs to console even if log level is Level.OFF
clientSettings.setHandleExistingEvents(true); // Invokes all TikTok events that had occurred before connection
clientSettings.setRetryOnConnectionFailure(true); // Reconnecting if TikTok user is offline
clientSettings.setRetryConnectionTimeout(Duration.ofSeconds(1)); // Timeout before next reconnection
//Optional: Sometimes not every message from chat are send to TikTokLiveJava to fix this issue you can set sessionId
// documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
// clientSettings.setSessionId("86c3c8bf4b17ebb2d74bb7fa66fd0000");
//Optional:
//RoomId can be used as an override if you're having issues with HostId.
//You can find it in the HTML for the livestream-page
//clientSettings.setRoomId("XXXXXXXXXXXXXXXXX");
})
.onGift((liveClient, event) ->
{
switch (event.getGift()) {
case ROSE -> print(ConsoleColors.RED, "Rose!");
case GG -> print(ConsoleColors.YELLOW, " GOOD GAME!");
case TIKTOK -> print(ConsoleColors.CYAN,"Thanks for TikTok");
default -> print(ConsoleColors.GREEN, "[Thanks for gift] ", ConsoleColors.YELLOW, event.getGift().getName(), "x", event.getCombo());
}
})
.onGiftCombo((liveClient, event) ->
{
print(ConsoleColors.RED,"GIFT COMBO",event.getGift().getName(),event.getCombo());
})
.onConnected((client, event) ->
{
print(ConsoleColors.GREEN, "[Connected]");
})
.onDisconnected((liveClient, event) ->
{
print(ConsoleColors.RED,"[Disconnected]");
})
.onFollow((liveClient, event) ->
{
print(ConsoleColors.BLUE, "Follow:", ConsoleColors.WHITE_BRIGHT, event.getUser().getName());
})
.onJoin((client, event) ->
{
print(ConsoleColors.WHITE, "Join:", ConsoleColors.WHITE_BRIGHT, event.getUser().getName());
})
.onComment((client, event) ->
{
print(ConsoleColors.GREEN, event.getUser().getName(), ":", ConsoleColors.WHITE_BRIGHT, event.getText());
})
.onEvent((client, event) ->
{
//System.out.println("Event: " +event.getClass().getSimpleName());
})
.onError((client, event) ->
{
event.getException().printStackTrace();
})
.buildAndConnectAsync();
System.in.read();
}
private static void print(Object... messages) {
var sb = new StringBuilder();
for (var message : messages) {
sb.append(message).append(" ");
}
System.out.println(sb);
}
private static void showLogo()
{
System.out.println(ConsoleColors.GREEN+"""
_____ _ _ _____ _ _ _ s
|_ _(_) | _|_ _|__ | | _| | (_)_ _____s
| | | | |/ / | |/ _ \| |/ / | | \ \ / / _ \
| | | | < | | (_) | <| |___| |\ V / __/
|_| |_|_|\_\ |_|\___/|_|\_\_____|_| \_/ \___|
""");
}
}
Events
Control:
Message:
- onEmote
- onUnhandledSocial
- onJoin
- onLiveEnded
- onShare
- onLivePaused
- onRoomUserInfo
- onGiftCombo
- onQuestion
- onSubscribe
- onComment
- onFollow
- onLike
- onRoom
- onGift
- onEvent
Debug:
Examples
onReconnecting TikTokReconnectingEvent
TikTokLive.newClient("host-name")
.onReconnecting((liveClient, event) ->
{
})
.buildAndConnect();
onConnected TikTokConnectedEvent
Triggered when the connection is successfully established.
TikTokLive.newClient("host-name")
.onConnected((liveClient, event) ->
{
})
.buildAndConnect();
onDisconnected TikTokDisconnectedEvent
Triggered when the connection gets disconnected. In that case you can call connect() again to have a reconnect logic. Note that you should wait a little bit before attempting a reconnect to to avoid being rate-limited.
TikTokLive.newClient("host-name")
.onDisconnected((liveClient, event) ->
{
})
.buildAndConnect();
onError TikTokErrorEvent
General error event. You should handle this.
TikTokLive.newClient("host-name")
.onError((liveClient, event) ->
{
})
.buildAndConnect();
onEmote TikTokEmoteEvent
Triggered every time a subscriber sends an emote (sticker).
TikTokLive.newClient("host-name")
.onEmote((liveClient, event) ->
{
})
.buildAndConnect();
onUnhandledSocial TikTokUnhandledSocialEvent
TikTokLive.newClient("host-name")
.onUnhandledSocial((liveClient, event) ->
{
})
.buildAndConnect();
onJoin TikTokJoinEvent
TikTokLive.newClient("host-name")
.onJoin((liveClient, event) ->
{
})
.buildAndConnect();
onLiveEnded TikTokLiveEndedEvent
Triggered when the live stream gets terminated by the host. Will also trigger the TikTokDisconnectedEvent event.
TikTokLive.newClient("host-name")
.onLiveEnded((liveClient, event) ->
{
})
.buildAndConnect();
onShare TikTokShareEvent
Triggers when a user shares the stream. Based on social event.
TikTokLive.newClient("host-name")
.onShare((liveClient, event) ->
{
})
.buildAndConnect();
onLivePaused TikTokLivePausedEvent
TikTokLive.newClient("host-name")
.onLivePaused((liveClient, event) ->
{
})
.buildAndConnect();
onRoomUserInfo TikTokRoomUserInfoEvent
Only top 5 users in ranking has detailed data
rest has only ID
TikTokLive.newClient("host-name")
.onRoomUserInfo((liveClient, event) ->
{
})
.buildAndConnect();
onGiftCombo TikTokGiftComboEvent
TikTokLive.newClient("host-name")
.onGiftCombo((liveClient, event) ->
{
})
.buildAndConnect();
onQuestion TikTokQuestionEvent
Triggered every time someone asks a new question via the question feature.
TikTokLive.newClient("host-name")
.onQuestion((liveClient, event) ->
{
})
.buildAndConnect();
onSubscribe TikTokSubscribeEvent
Triggers when a user creates a subscription.
TikTokLive.newClient("host-name")
.onSubscribe((liveClient, event) ->
{
})
.buildAndConnect();
onComment TikTokCommentEvent
Triggered every time a new chat comment arrives.
TikTokLive.newClient("host-name")
.onComment((liveClient, event) ->
{
})
.buildAndConnect();
onFollow TikTokFollowEvent
Triggers when a user follows the streamer. Based on social event.
TikTokLive.newClient("host-name")
.onFollow((liveClient, event) ->
{
})
.buildAndConnect();
onLike TikTokLikeEvent
Triggered when a viewer sends likes to the streamer. For streams with many viewers, this event is not always triggered by TikTok.
TikTokLive.newClient("host-name")
.onLike((liveClient, event) ->
{
})
.buildAndConnect();
onRoom TikTokRoomEvent
TikTokLive.newClient("host-name")
.onRoom((liveClient, event) ->
{
})
.buildAndConnect();
onGift TikTokGiftEvent
Triggered every time a gift arrives.
TikTokLive.newClient("host-name")
.onGift((liveClient, event) ->
{
})
.buildAndConnect();
onEvent TikTokEvent
Base class for all events
TikTokLive.newClient("host-name")
.onEvent((liveClient, event) ->
{
})
.buildAndConnect();
onWebsocketUnhandledMessage TikTokWebsocketUnhandledMessageEvent
Triggered every time a protobuf encoded webcast message arrives. You can deserialize the binary object depending on the use case.
TikTokLive.newClient("host-name")
.onWebsocketUnhandledMessage((liveClient, event) ->
{
})
.buildAndConnect();
onWebsocketResponse TikTokWebsocketResponseEvent
TikTokLive.newClient("host-name")
.onWebsocketResponse((liveClient, event) ->
{
})
.buildAndConnect();
onWebsocketMessage TikTokWebsocketMessageEvent
Triggered every time a protobuf encoded webcast message arrives. You can deserialize the binary object depending on the use case.
TikTokLive.newClient("host-name")
.onWebsocketMessage((liveClient, event) ->
{
})
.buildAndConnect();
Listener Example
{{listener-content}}
Contributing
Your improvements are welcome! Feel free to open an issue or pull request.