Home DevelopersExamples & updates Authenticate as user and connect to Twitch chat

Authenticate as user and connect to Twitch chat

by BlushyFace
54 views

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()
        {
            Run();
        }

        private void Run()
        {
            var auth = new Authentication(new[] {"http://+:8081/"});
            var scopes = new List<Enum>() {
                                              Scope.Chat.ChatRead,
                                              Scope.Chat.ChatEdit,
                                              Scope.Chat.WhispersEdit
                                          };

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

 

Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments

Related Articles

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

LIVE NOW! CLICK TO VIEW.
CURRENTLY OFFLINE