mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 16:59:39 -05:00
Changes:
- TikTokHttpResponseEvent - Fixed User attributes in CommentEvent - Redesign .onMapper method
This commit is contained in:
@@ -22,55 +22,64 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.data.events.TikTokErrorEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokMessageMappingException;
|
||||
import io.github.jwdeveloper.tiktok.mappers.events.MappingResult;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastChatMessage;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage;
|
||||
import io.github.jwdeveloper.tiktok.utils.ProtocolUtils;
|
||||
|
||||
public class CustomMappingExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TikTokLive.newClient("vadimpyrography")
|
||||
.onCustomEvent(CustomChatEvent.class, (liveClient, event) ->
|
||||
TikTokLive.newClient("saszareznikow")
|
||||
.onMapping(mapper ->
|
||||
{
|
||||
System.out.println("hello world!");
|
||||
mapper.forMessage(WebcastChatMessage.class)
|
||||
.onBeforeMapping((inputBytes, messageName, mapperHelper) ->
|
||||
{
|
||||
System.out.println("===============================");
|
||||
System.out.println("OnBefore mapping: " + messageName);
|
||||
return inputBytes;
|
||||
})
|
||||
.onMapping((inputBytes, messageName, mapperHelper) ->
|
||||
{
|
||||
System.out.println("onMapping mapping: " + messageName);
|
||||
var message = mapperHelper.bytesToWebcastObject(inputBytes, WebcastChatMessage.class);
|
||||
var language = message.getContentLanguage();
|
||||
var userName = message.getUser().getNickname();
|
||||
var content = message.getContent();
|
||||
var event = new CustomChatEvent(language, userName, content);
|
||||
return MappingResult.of(message, event);
|
||||
})
|
||||
.onAfterMapping(mappingResult ->
|
||||
{
|
||||
var source = mappingResult.getSource();
|
||||
var events = mappingResult.getEvents();
|
||||
System.out.println("onAfter mapping, " + source.getClass().getSimpleName() + " was mapped to " + events.size() + " events");
|
||||
return events;
|
||||
});
|
||||
|
||||
/*
|
||||
There might be cast that we don't have Webcast class for incoming message from TikTok
|
||||
`mapperHelper.bytesToProtoBufferStructure` but you can still investigate message structure
|
||||
by using helper methods
|
||||
*/
|
||||
mapper.forMessage("WebcastMemberMessage")
|
||||
.onBeforeMapping((inputBytes, messageName, mapperHelper) ->
|
||||
{
|
||||
if (mapperHelper.isMessageHasProtoClass(messageName)) {
|
||||
var messageObject = mapperHelper.bytesToWebcastObject(inputBytes, messageName);
|
||||
// System.out.println(mapperHelper.toJson(messageObject));
|
||||
} else {
|
||||
var structure = mapperHelper.bytesToProtoBufferStructure(inputBytes);
|
||||
// System.out.println(structure.toJson());
|
||||
}
|
||||
return inputBytes;
|
||||
});
|
||||
})
|
||||
.onError((liveClient, event) ->
|
||||
{
|
||||
event.getException().printStackTrace();
|
||||
})
|
||||
.onMapping(mapper ->
|
||||
{
|
||||
mapper.webcastObjectToEvent(WebcastChatMessage.class, chatMessage ->
|
||||
{
|
||||
var language = chatMessage.getContentLanguage();
|
||||
var userName = chatMessage.getUser().getNickname();
|
||||
var message = chatMessage.getContent();
|
||||
return new CustomChatEvent(language, userName, message);
|
||||
});
|
||||
mapper.bytesToEvent("WebcastGiftMessage", bytes ->
|
||||
{
|
||||
try
|
||||
{
|
||||
var event = WebcastGiftMessage.parseFrom(bytes);
|
||||
return new TikTokGiftEvent(Gift.ROSA, event);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokMessageMappingException("unable to map gift message!", e);
|
||||
}
|
||||
});
|
||||
|
||||
mapper.bytesToEvent("WebcastMemberMessage",bytes ->
|
||||
{
|
||||
//displaying unknown messages from tiktok
|
||||
var structure = ProtocolUtils.getProtocolBufferStructure(bytes);
|
||||
System.out.println(structure.toJson());
|
||||
return new TikTokErrorEvent(new RuntimeException("Message not implemented"));
|
||||
});
|
||||
}).buildAndConnect();
|
||||
.buildAndConnect();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
public class Examplee
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
TikTokLive.newClient("skullchefasmr")
|
||||
.onGift((liveClient, event) ->
|
||||
{
|
||||
System.out.println("Dzięki za gifta "+event.getGift().getName());
|
||||
}).buildAndConnect();
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.annotations.TikTokEventHandler;
|
||||
import io.github.jwdeveloper.tiktok.annotations.TikTokEventObserver;
|
||||
import io.github.jwdeveloper.tiktok.data.events.TikTokCommentEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.TikTokErrorEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
@@ -65,24 +65,24 @@ public class ListenerExample
|
||||
|
||||
public static class CustomListener implements TikTokEventListener {
|
||||
|
||||
@TikTokEventHandler
|
||||
@TikTokEventObserver
|
||||
public void onLike(LiveClient liveClient, TikTokLikeEvent event) {
|
||||
System.out.println(event.toString());
|
||||
}
|
||||
|
||||
@TikTokEventHandler
|
||||
@TikTokEventObserver
|
||||
public void onError(LiveClient liveClient, TikTokErrorEvent event) {
|
||||
// event.getException().printStackTrace();
|
||||
}
|
||||
|
||||
@TikTokEventHandler
|
||||
@TikTokEventObserver
|
||||
public void onComment(LiveClient liveClient, TikTokCommentEvent event) {
|
||||
var userName = event.getUser().getName();
|
||||
var text = event.getText();
|
||||
liveClient.getLogger().info(userName + ": " + text);
|
||||
}
|
||||
|
||||
@TikTokEventHandler
|
||||
@TikTokEventObserver
|
||||
public void onGift(LiveClient liveClient, TikTokGiftEvent event) {
|
||||
var message = switch (event.getGift()) {
|
||||
case ROSE -> "Thanks :)";
|
||||
@@ -95,7 +95,7 @@ public class ListenerExample
|
||||
liveClient.getLogger().info(message);
|
||||
}
|
||||
|
||||
@TikTokEventHandler
|
||||
@TikTokEventObserver
|
||||
public void onAnyEvent(LiveClient liveClient, TikTokEvent event) {
|
||||
liveClient.getLogger().info(event.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user