TwitchLib.EventSub.Webhooks
3.0.0-preview.1.2c180de
dotnet add package TwitchLib.EventSub.Webhooks --version 3.0.0-preview.1.2c180de
NuGet\Install-Package TwitchLib.EventSub.Webhooks -Version 3.0.0-preview.1.2c180de
<PackageReference Include="TwitchLib.EventSub.Webhooks" Version="3.0.0-preview.1.2c180de" />
<PackageVersion Include="TwitchLib.EventSub.Webhooks" Version="3.0.0-preview.1.2c180de" />
<PackageReference Include="TwitchLib.EventSub.Webhooks" />
paket add TwitchLib.EventSub.Webhooks --version 3.0.0-preview.1.2c180de
#r "nuget: TwitchLib.EventSub.Webhooks, 3.0.0-preview.1.2c180de"
#:package TwitchLib.EventSub.Webhooks@3.0.0-preview.1.2c180de
#addin nuget:?package=TwitchLib.EventSub.Webhooks&version=3.0.0-preview.1.2c180de&prerelease
#tool nuget:?package=TwitchLib.EventSub.Webhooks&version=3.0.0-preview.1.2c180de&prerelease
TwitchLib.EventSub.Webhooks
Provides an easy way to setup a Twitch EventSub Webhooks Server
Setting up a Twitch EventSub server can be daunting and has some moving parts that you could get wrong. TwitchLib.EventSub.Webhooks was build with that in mind and makes it as easy as it can get. You only need a few lines of code to add and configure it.
Installation
Breaking Changes in Version 3.0
- Removed deprecated versions of .NET.
- Events are now asynchronous (return value changed from
void
toTask
) - Events dropped the
On
prefix (OnChannelChatMessage
⇒ChannelChatMessage
) - All EventSub events were moved to
TwitchLib.EventSub.Core
Nuget Package, for better management across future EventSub transport Client libraries. That means their namespace changed fromTwitchLib.EventSub.Webhooks.Core.EventArgs.*
toTwitchLib.EventSub.Core.EventArgs.*
. - Like Events, all EventSub Models were moved to the
TwitchLib.EventSub.Core
package, (namespace changed fromTwitchLib.EventSub.Webhooks.Core.Models
toTwitchLib.EventSub.Core.Models
) but to ensure that the models can be used across projects some changes had to be made:Notification
inTwitchLibEventSubEventArgs<T>
were renamed toPayload
Headers
(Dictionary<string,string>
) inTwitchLibEventSubEventArgs<T>
were replaced withMetadata
(EventSubMetadata
) and before you can access the values you have to cast it toWebhookEventSubMetadata
EventSubSubscriptionTransport
was renamed toEventSubTransport
Breaking Changes in Version 2.0
Version 2.0 contains some breaking changes.
- Subscription Types and their models were moved to their own shared Nuget Package
TwitchLib.EventSub.Core
for better management across future EventSub transport Client libraries That means their namespace changed fromTwitchLib.EventSub.Webhooks.Core.SubscriptionTypes
/TwitchLib.EventSub.Webhooks.Core.Models
toTwitchLib.EventSub.Core.SubscriptionTypes
/TwitchLib.EventSub.Core.Models
- Every use of
DateTime
internally and in models was changed to useDateTimeOffset
instead ITwitchEventSubWebhooks
/TwitchEventSubWebhooks
were renamed toIEventSubWebhooks
/EventSubWebhooks
Disclaimer
The usual requirements that Twitch has for EventSub webhooks do still apply!
- Your callback url needs to be publicly accessible (localhost wont work!)
- You need to have SSL on port 443 for the domain used as a callback.
Setup
Step 1: Create a new ASP.NET Core project (.NET 8.0 and up)
Step 2: Install the TwitchLib.EventSub.Webhooks nuget package. (See above on how to do that)
Step 3: Add necessary services and config to the DI Container
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTwitchLibEventSubWebhooks(config =>
{
config.CallbackPath = "/eventsub/";
config.Secret = "supersecuresecret";
});
builder.Services.AddHostedService<EventSubHostedService>();
!!! If you follow these steps your callback url will https://{your_domain}/eventsub/
!!!
Step 4: Put the TwitchLib.EventSub.Webhooks middleware in the request pipeline
var app = builder.Build();
app.UseTwitchLibEventSubWebhooks();
app.Run();
Step 5: Create the HostedService and listen for events
using TwitchLib.EventSub.Core.EventArgs.Channel;
using TwitchLib.EventSub.Webhooks.Core;
using TwitchLib.EventSub.Webhooks.Core.EventArgs;
using TwitchLib.EventSub.Webhooks.Core.Models;
namespace TwitchLib.EventSub.Webhooks.Example
{
public class EventSubHostedService : IHostedService
{
private readonly ILogger<EventSubHostedService> _logger;
private readonly IEventSubWebhooks _eventSubWebhooks;
public EventSubHostedService(ILogger<EventSubHostedService> logger, IEventSubWebhooks eventSubWebhooks)
{
_logger = logger;
_eventSubWebhooks = eventSubWebhooks;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_eventSubWebhooks.OnError += OnError;
_eventSubWebhooks.UnknownEventSubNotification += OnUnknownEventSubNotification;
_eventSubWebhooks.ChannelChatMessage += OnChannelChatMessage;
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_eventSubWebhooks.OnError -= OnError;
_eventSubWebhooks.UnknownEventSubNotification -= OnUnknownEventSubNotification;
_eventSubWebhooks.ChannelChatMessage -= OnChannelChatMessage;
return Task.CompletedTask;
}
private async Task OnChannelChatMessage(object? sender, ChannelChatMessageArgs e)
{
_logger.LogInformation($"@{e.Payload.Event.ChatterUserName} #{e.Payload.Event.BroadcasterUserName}: {e.Payload.Event.Message.Text}");
}
private async Task OnError(object? sender, OnErrorArgs e)
{
_logger.LogError($"Reason: {e.Reason} - Message: {e.Message}");
}
// Handling notifications that are not (yet) implemented
private async Task OnUnknownEventSubNotification(object? sender, UnknownEventSubNotificationArgs e)
{
var metadata = (WebhookEventSubMetadata)e.Metadata;
_logger.LogInformation("Received event that has not yet been implemented: type:{type}, version:{version}", metadata.SubscriptionType, metadata.SubscriptionVersion);
switch ((metadata.SubscriptionType, metadata.SubscriptionVersion))
{
case ("channel.chat.message", "1"): /*code to handle the event*/ break;
default: break;
}
}
}
}
That is all that you need to do to setup a Twitch EventSub Webhook Server with TwitchLib.EventSub.Webhooks. Easy isn't it?
Alternatively you can also just clone the https://github.com/TwitchLib/TwitchLib.EventSub.Webhooks/tree/master/TwitchLib.EventSub.Webhooks.Example
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- TwitchLib.EventSub.Core (>= 3.0.0-preview.68.b035bbb)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added .NET 8 build target, Updated TwitchLib.EventSub.Core to the latest version to include the latest subscription type changes