Home of Blushies & friends (づ。◕‿‿◕。)づ | Read the adventures of cute Blushies | Community projects (Twitch chat bots / tools / mini games etc)
Authenticate as user and connect to Twitch chat
Home » BlushyFace.Twitch.* » BlushyFace.Twitch.* examples » Authenticate as user and connect to Twitch chat

Authenticate as user and connect to Twitch chat

Previously we wrote an auth lib that takes care of authenticating users (blog.blushyface.com/2020/09/30/developers/examples/quick-guide-blushyface-twitch-authentication/) 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.Authentication.Enums;
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<Enum>() {

            // 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}");



Herrow, I'm BlushyFace and this is the place where you can read about the adventures of silly Blushies & other content I've made related to my Twitch channel community.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments