0 Full Rundown - Hopeful-Developers/topggwebhooks4j GitHub Wiki

Here's the first steps on using topggwebhooks4j

First of all, make sure that your gradle/maven dependencies are added correctly.

If you're using gradle, you should see something like this after following the dependency instructions gradle file

Note: This can look different for you

Now, lets get started:

We first need to initialize the WebhookBuilder in order to build our Webhook. This can be done the following way: example

Now we have the first essential step done. We just need to continue working with the WebhookBuilder.

Lets just get started with adding a Bot Webhook: A Webhook, that should get executed when someone votes for our bot. Adding a Bot Webhook Listener is done through the addBotListener method. This method has the required parameters of addBotListener(String context, BotWebhookListener listener, String httpAuthorization, Options... options)

parameter type example purpose
context String BotWebhook This is the path of your webhook url. This is the important thing on your webhook to identify your listener. Using the example, your webhook would be something like http://example.com:6969/BotWebhook.
listener BotWebhookListener new BotWebhookHandler() This is your class, that is responsible for handling your webhook. This is where you do things with the webhook such as rewarding users with currency or a simple notification message.
httpAuthorization String MySecretWebhookKey This is what authorizes your webhook. This is used to identify the requests coming to your webhook. We don't want people giving themselves unlimited currency of course ;)
options Options... Options.IGNORE_AUTHORIZATION This is optional. This is just for user who wish to do advanced things with their webhooks. They can for example only let test-votes through or allow invalid requests/unauthorized requests etc. More or less for higher-devs!

Lets just start with a simple example: I have a very cool bot, it's name is "CoolBot". I want my webhook to have the context "BotVoter". It should only allow requests when the Key is "CoolBot69". The magic should happen inside the already-prepared VoteWebhook class.

This would look something like this with the explaination above:

finished example

Now lets take a look into how the actual VoteWebhook class has to work.

VoteWebhook.java

You will notice two things: the implementation of BotWebhookListener and the automatically-generated onWebhookRequest method with a BotWebhookEvent object? Confusing at first, but just listen along:

This is the main part of your Webhook-Listener. This is where your webhook runs the code from when a valid, authorized request comes through your webhook. All the data from that Webhook request is on that BotWebhookEvent object, event.

These are the methods that a BotWebhookEvent object has:

method return-type purpose
getVote() BotVoteData This object contains everything about the vote that happened, such as the user, is it weekend etc.
isValid() Boolean This method returns if the request data is valid or not.
isAuthorized() Boolean This method tells you if the request your webhook received is authorized or not.
getRequestString() String returns the voting-data
getListenerAuthorization() String ----------
getListener() BotWebhookListener ----------
getRequestAuthorization() String ----------