TwitchLib.EventSub.Webhooks 2.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TwitchLib.EventSub.Webhooks --version 2.1.0                
NuGet\Install-Package TwitchLib.EventSub.Webhooks -Version 2.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TwitchLib.EventSub.Webhooks" Version="2.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TwitchLib.EventSub.Webhooks --version 2.1.0                
#r "nuget: TwitchLib.EventSub.Webhooks, 2.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TwitchLib.EventSub.Webhooks as a Cake Addin
#addin nuget:?package=TwitchLib.EventSub.Webhooks&version=2.1.0

// Install TwitchLib.EventSub.Webhooks as a Cake Tool
#tool nuget:?package=TwitchLib.EventSub.Webhooks&version=2.1.0                

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

NuGet TwitchLib.EventSub.Webhooks
Package Manager PM> Install-Package TwitchLib.EventSub.Webhooks -Version 2.1.0
.NET CLI > dotnet add package TwitchLib.EventSub.Webhooks --version 2.1.0
PackageReference <PackageReference Include="TwitchLib.EventSub.Webhooks" Version="2.1.0" />
Paket CLI > paket add TwitchLib.EventSub.Webhooks --version 2.1.0

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 from TwitchLib.EventSub.Webhooks.Core.SubscriptionTypes / TwitchLib.EventSub.Webhooks.Core.Models to TwitchLib.EventSub.Core.SubscriptionTypes / TwitchLib.EventSub.Core.Models
  • Every use of DateTime internally and in models was changed to use DateTimeOffset instead
  • ITwitchEventSubWebhooks / TwitchEventSubWebhooks were renamed to IEventSubWebhooks/ 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 5.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

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddTwitchLibEventSubWebhooks(config =>
    {
        config.CallbackPath = "/webhooks";
        config.Secret = "supersecuresecret";
        config.EnableLogging = true;
    });

    services.AddHostedService<EventSubHostedService>();
}

!!! If you follow these steps your callback url will https://{your_domain}/webhooks !!!

Step 4: Put the TwitchLib.EventSub.Webhooks middleware in the request pipeline

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseRouting();

    app.UseAuthorization();

    app.UseTwitchLibEventSubWebhooks();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

Step 5: Create the HostedService and listen for events

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
using TwitchLib.EventSub.Webhooks.Core;
using TwitchLib.EventSub.Webhooks.Core.EventArgs;
using TwitchLib.EventSub.Webhooks.Core.EventArgs.Channel;

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.OnChannelFollow += OnChannelFollow;
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            _eventSubWebhooks.OnError -= OnError;
            _eventSubWebhooks.OnChannelFollow -= OnChannelFollow;
            return Task.CompletedTask;
        }

        private void OnChannelFollow(object sender, ChannelFollowArgs e)
        {
            _logger.LogInformation($"{e.Notification.Event.UserName} followed {e.Notification.Event.BroadcasterUserName} at {e.Notification.Event.FollowedAt.ToUniversalTime()}");
        }

        private void OnError(object sender, OnErrorArgs e)
        {
            _logger.LogError($"Reason: {e.Reason} - Message: {e.Message}");
        }
    }
}

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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.0 3,155 2/12/2023
2.2.0 942 11/21/2022
2.1.0 327 11/8/2022
2.0.0 423 10/19/2022
1.3.1 420 9/21/2022
1.3.0 392 8/28/2022
1.2.0 409 8/9/2022
1.1.1 1,012 11/22/2021
1.1.0 328 9/1/2021
1.0.0 308 8/25/2021

Updated TwitchLib.EventSub.Core to the latest version to include the latest subscription type changes, Switched to .NET 7.0 Full release build