Created
February 4, 2021 22:08
-
-
Save iProdigy/53f61e439b81d1d926748d5c15fdf23b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.github.philippheuer.events4j.core.EventManager; | |
import com.github.philippheuer.events4j.simple.SimpleEventHandler; | |
import com.github.twitch4j.common.util.EventManagerUtils; | |
import com.github.twitch4j.common.util.TypeConvert; | |
import com.github.twitch4j.eventsub.EventSubNotification; | |
import com.github.twitch4j.eventsub.EventSubSubscriptionStatus; | |
import com.github.twitch4j.eventsub.EventSubTransport; | |
import com.github.twitch4j.eventsub.EventSubTransportMethod; | |
import com.github.twitch4j.eventsub.events.ChannelFollowEvent; | |
import com.github.twitch4j.eventsub.subscriptions.SubscriptionTypes; | |
import com.github.twitch4j.helix.TwitchHelix; | |
import com.github.twitch4j.helix.TwitchHelixBuilder; | |
import io.javalin.Javalin; | |
import org.eclipse.jetty.server.Connector; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.ServerConnector; | |
import org.eclipse.jetty.util.ssl.SslContextFactory; | |
import static com.github.twitch4j.eventsub.util.EventSubVerifier.verifyMessageId; | |
import static com.github.twitch4j.eventsub.util.EventSubVerifier.verifySignature; | |
import static com.github.twitch4j.eventsub.util.EventSubVerifier.verifyTimestamp; | |
public class SampleEventSubWebhookApp { | |
private static final String SECRET = "this-is-my-eventsub-secret"; | |
public static void main(String[] args) { | |
TwitchHelix helix = TwitchHelixBuilder.builder() | |
.withClientId("this-is-my-client-id") | |
.withClientSecret("this-is-my-client-secret") | |
.build(); | |
EventManager eventManager = EventManagerUtils.initializeEventManager(SimpleEventHandler.class); | |
Javalin | |
.create(config -> config.server(() -> { | |
Server server = new Server(); | |
ServerConnector sslConnector = new ServerConnector(server, getSslContextFactory()); | |
sslConnector.setPort(443); | |
ServerConnector connector = new ServerConnector(server); | |
connector.setPort(80); | |
server.setConnectors(new Connector[] { sslConnector, connector }); | |
return server; | |
})) | |
.post("/webhooks/callback", ctx -> { | |
final String msgId, time; | |
if (verifyMessageId(msgId = ctx.header("Twitch-Eventsub-Message-Id")) | |
&& verifyTimestamp(time = ctx.header("Twitch-Eventsub-Message-Timestamp")) | |
&& verifySignature(SECRET, msgId, time, ctx.bodyAsBytes(), ctx.header("Twitch-Eventsub-Message-Signature"))) { | |
EventSubNotification notification = TypeConvert.jsonToObject(ctx.body(), EventSubNotification.class); | |
if (notification != null) { | |
if (notification.getChallenge() != null) | |
ctx.result(notification.getChallenge()); | |
eventManager.publish(notification); | |
ctx.status(200); | |
} else { | |
ctx.status(500); | |
} | |
} else { | |
ctx.status(403); | |
} | |
}) | |
.start(443); | |
helix.createEventSubSubscription(null, SubscriptionTypes.CHANNEL_FOLLOW.prepareSubscription( | |
builder -> builder | |
.broadcasterUserId("207813352") | |
.build(), | |
EventSubTransport.builder() | |
.callback("https://aa6158ff218e.ngrok.io/webhooks/callback") | |
.method(EventSubTransportMethod.WEBHOOK) | |
.secret(SECRET) | |
.build() | |
)).execute(); | |
eventManager.onEvent(EventSubNotification.class, notif -> { | |
if (notif.getSubscription().getStatus() != EventSubSubscriptionStatus.ENABLED) return; | |
if (SubscriptionTypes.CHANNEL_FOLLOW.equals(notif.getSubscription().getType())) { | |
ChannelFollowEvent event = (ChannelFollowEvent) notif.getEvent(); | |
// TODO: handle event | |
} | |
}); | |
} | |
private static SslContextFactory getSslContextFactory() { | |
SslContextFactory sslContextFactory = new SslContextFactory.Server(); | |
sslContextFactory.setKeyStorePath("this-is-my-keystore-path"); | |
sslContextFactory.setKeyStorePassword("this-is-my-keystore-password"); | |
return sslContextFactory; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment