Auth and connect to Twitch chat

Previously we wrote an auth lib that takes care of authenticating users ( and now we use that example to authenticate users to connect to Twitch chat.

To start out we will be creating a new Authentication instance (that already has the correct settings (client id / client secret / redirect URL) and runs a local web server) while adding a couple scopes so we can do chat things (read/send/whisper), after that we open a browser where we ask the user to authenticate themselves.

The application listens for auth events and when the user is successfully authenticated it will automatically login the user to Twitch chat and send a chat message. In Auth_OnTokenResponse we create the Twitch chat client but ideally you would move that out and create a Twitch chat client instance for each authenticated user but for the sake of an example it’s all there instead.

using System;
using System.Collections.Generic;
using BlushyFace.Twitch.Chat.V2.Chat; 

namespace BlushyFace.Twitch.Tester.Tests
    internal class Chat
        public Chat()

        private void Run()
            var auth = new Authentication(new[] {"http://+:8081/"});
            var scopes = new List<Scopes>() {

            // opens the browser for auth flow.
            auth.OAuth.OpenBrowserAuthorizationFlow(scopes: scopes);

            // listen for authenticated users.
            auth.OnTokenResponse += Auth_OnTokenResponse;

        private async void Auth_OnTokenResponse(object? sender, Dictionary<string, string> e)
            // create a new twitch chat client.
            var client = new Client(userName: e.GetTagValue("login"), token: e.GetTagValue("accessToken"), logLevel: Enums.LogLevel.None);

            // connect to twitch chat
            await client.ConnectAsync();

            // listen for chat message.
            client.PrivMsgEventHandler += Client_PrivMsgEventHandler;

            // join the channel to read chat messages
            await client.JoinAsync("blushyfacebot");

            // send a welcome message
            await client.SendMessageAsync("blushyfacebot", $"Hi i'm connected as {e.GetTagValue("login")}");

        // write chat messages in console
        private void Client_PrivMsgEventHandler(object sender, Twitch.Chat.V2.Chat.ResponseObjects.PrivMsg e)
            Console.WriteLine($"[{e.UserName}] {e.Message}");