Skip to content

feat: add webhooks - #30

Open
null8626 wants to merge 47 commits into
top-gg-community:masterfrom
null8626:split/webhooks
Open

feat: add webhooks#30
null8626 wants to merge 47 commits into
top-gg-community:masterfrom
null8626:split/webhooks

Conversation

@null8626

Copy link
Copy Markdown
Member

The following pull request is a toned down version of #29. This pull request focuses solely on adding a webhooks wrapper for ASP.NET Core/Blazor.

@velddev

velddev commented Sep 17, 2025

Copy link
Copy Markdown
Contributor

This is not how a ASP.NET Core webhook endpoint should look like. Prefer using the minimal API to create a route at build-time. This will let the internal ASP.NET core router handle all of the actual context.

public static class RoutingExtension {
  public static void UseVoteWebhook(IRouteEndpointBuilder router, string path, ...) {
    router.MapGet(path, (req) => {
      ...
    });  
  }
}

@null8626

Copy link
Copy Markdown
Member Author

Will work on it.

@null8626

Copy link
Copy Markdown
Member Author

Done. Made it more similar to Node.js SDK' webhooks wrapper.

Example usage:

using DiscordBotsList.Api.Webhooks;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

var webhooks = new Webhooks("my webhook secret");

app.MapPost("/webhooks", webhooks.Listener((context, vote) =>
{
    Console.WriteLine($"A user with the ID of {vote.VoterId} has voted us on Top.gg!");

    return Task.CompletedTask;
}));

app.Run();

@null8626
null8626 force-pushed the split/webhooks branch 3 times, most recently from b84b479 to 094a304 Compare February 3, 2026 15:47
@williamdevgg

Copy link
Copy Markdown

looks good to me

@velddev velddev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this wouldn't work with the actual api. Refer to the docs here for more information.

https://docs.top.gg/docs/API/v1/webhooks/
https://docs.top.gg/docs/API/v1/integrations

Comment thread DiscordBotsList.Api.Webhooks/Middleware.cs Outdated
Comment thread DiscordBotsList.Api.Webhooks/Middleware.cs Outdated
Comment on lines +50 to +52
var hmac = new HMACSHA256(authorization);

hmac.TransformFinalBlock(transformBuffer, 0, transformBuffer.Length);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done without an allocation an hmac object every request by using

HMACSHA256.HashData(authorization, data);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return;
}

var vote = JsonSerializer.Deserialize<Vote>(body, serializerOptions);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would break on additional other values. See; https://docs.top.gg/docs/API/v1/webhooks/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. What do you think of this diff?

Comment thread DiscordBotsList.Api.Webhooks/Vote.cs Outdated

namespace DiscordBotsList.Api.Webhooks
{
public class Vote

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong Type, this is the legacy payload

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@null8626

Copy link
Copy Markdown
Member Author

Most of this wouldn't work with the actual api. Refer to the docs here for more information.

https://docs.top.gg/docs/API/v1/webhooks/ https://docs.top.gg/docs/API/v1/integrations

So sorry! I was too fixated on top-gg/webhooks-v2-nodejs-example that I didn't notice the new webhook events. My bad. I've addressed them now.

Added XML documentation for the Webhooks class.

@velddev velddev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, few small nits and naming recommendations

Comment thread DiscordBotsList.Webhooks/Webhooks.cs Outdated
using Microsoft.Extensions.Primitives;

namespace DiscordBotsList.Webhooks
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you avoid adding scope for namespaces? This is no longer required

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread DiscordBotsList.Webhooks/Webhooks.cs Outdated
/// <summary>
/// A Top.gg webhook manager.
/// </summary>
public abstract class Webhooks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could we call it something like WebhookEventListener. Webhooks feels like a constant class with readonly properties (e.g. Webhooks.VoteCreate)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread DiscordBotsList.Webhooks/Webhooks.cs Outdated
return;
}

context.Request.Headers.TryGetValue("x-topgg-trace", out var trace);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if this returns false?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! Forgot about that one. Should be resolved by now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants