TwitchLib / TwitchLib.EventSub.Webhooks

Provides an easy way to setup a Twitch EventSub Webhooks Server
6 stars 7 forks source link

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.3.0
.NET CLI > dotnet add package TwitchLib.EventSub.Webhooks --version 2.3.0
PackageReference <PackageReference Include="TwitchLib.EventSub.Webhooks" Version="2.3.0" />
Paket CLI > paket add TwitchLib.EventSub.Webhooks --version 2.3.0

Breaking Changes in Version 2.0

Version 2.0 contains some breaking changes.

Disclaimer

The usual requirements that Twitch has for EventSub webhooks do still apply!

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