Home DevelopersExamples & updates Setting up the client and using chat commands

Setting up the client and using chat commands

by BlushyFace

General
To use the library you need to add it as reference in your project.

This guide covers connecting to the Twitch chat and sending a message to chat as bot / broadcaster etc and will not go into other features the library provides. It also does not cover everything such as the various available Twitch chat commands but by following the command examples below you should be able use the rest by checking what it requires.

Step 1 create the bot / broadcaster credentials
To setup the client you need to create a new Credential with a valid OAuth token -> var bot = new Credential(“oauth:..”); and optionally one as the broadcaster to receive additional messages.

var bot = new Credential("oauth:..");
var broadcaster = new Credential("oauth:..");

Step 2 create the client and connect to Twitch chat
Create the client and provide the earlier created credentials as parameters -> var client = new BlushyFace.Twitch.Chat.Client(bot, broadcaster);  Note that the first parameter is always the bot and the optional parameter is the broadcaster.

The client provides various events to subscribe to:

client.OnRawChatMessageReceive -> when a raw message is received from Twitch this event will be fired.
client.OnChatMessageReceive -> returns the chat message
client.OnUserJoin -> when an user joins chat this event will be fired
client.OnUserPart -> when an user leaves chat this event will be fired
client.OnSendRawMessage -> when sending a message to Twitch this event will be fired
client.OnSub -> when someone subs (such as re-subs / sub gifts but might not be complete) this event will be fired
client.OnBit -> when someone cheers this event will be fired

Note that bot client.OnRawChatMessageReceive & client.OnChatMessageReceive events will show every received messages so it looks like that messages are received twice in the console. To ignore received bot or broadcaster messages you can check below to ignore it.

To actually connect to the Twitch chat we do so by -> client.Connect(); by doing so it will verify if the provided tokens are valid and if not it will not connect.

// create the connection as bot
var client = new BlushyFace.Twitch.Chat.Client(bot);

// create the connection as bot & optional broadcaster to receive additional broadcaster messages
var client = new BlushyFace.Twitch.Chat.Client(bot, broadcaster);

//subscribe to various events
client.OnChatMessageReceive += OnReceivedMessages;
client.OnUserJoin += OnUserJoin;
client.OnUserPart += OnUserPart;
client.OnSendRawMessage += OnSendRawMessage;
client.OnSub += OnSub;
client.OnBit += OnBit;

// connect to Twitch chat
client.Connect();

Step 3 Join a channel and send a message as the bot or broadcaster
To join a channel -> client.JoinChannel(“blushyface”);
To leave a channel -> client.LeaveChannel(“blushyface”);

To send a chat message as the bot to the bot own channel [default] -> client.SendMessage(“send a message as bot [default] to it’s own channel”);
To send a chat message as the bot to another channel -> client.SendMessage(“send a message as bot [default] to a specific channel”, “channelname”);
To send a chat message as the broadcaster to another channel -> client.SendMessage(“send a message as broadcaster to a specific channel”, “channelname”, Enums.BotType.Broadcaster);

// join & leave a channel
client.JoinChannel("blushyface");
client.LeaveChannel("blushyface");

//send messages to chat
client.SendMessage("send a message as bot [default] to it's own channel");
client.SendMessage("send a message as bot [default] to a specific channel", "channelname");
client.SendMessage("send a message as broadcaster to a specific channel", "channelname", Enums.BotType.Broadcaster);

Step 4 Send a whisper as the bot or broadcaster and other commands
Commands can be accessed from client.Commands.X, it was client.X before but I felt it was better to group it in one place.

To send a whisper to “blushyface” as bot [default] -> client.Commands.Whisper(“blushyface”, “send from bot”);
To send a whisper to “blushyfacebot” as broadcaster ->  client.Commands.Whisper(“blushyfacebot”, “send from broadcaster”, Enums.BotType.Broadcaster);

Examples of other commands can be found below.

// sends a whisper [default is from bot]
client.Commands.Whisper("blushyface", "send from bot");

// sends a whisper as broadcaster
client.Commands.Whisper("blushyfacebot", "send from broadcaster", Enums.BotType.Broadcaster);


// change username color -> [color] [bot as default / broadcaster]
var color = Enums.UserNameColor.Green;
var sendTo = "blushyface";

//change broadcaster color to Green and send a message to chat to check the new color
client.Commands.Color(color, Enums.BotType.Broadcaster);
client.SendMessage($"check color {color}?", sendTo, Enums.BotType.Broadcaster);

//change broadcaster color to DodgerBlue and send a message to chat to check the new color
color = Enums.UserNameColor.DodgerBlue;
client.Commands.Color(color, Enums.BotType.Broadcaster);
client.SendMessage($"check color {color}?", sendTo, Enums.BotType.Broadcaster);

//change bot color to SpringGreen and send a message to chat to check the new color
color = Enums.UserNameColor.SpringGreen;
client.Commands.Color(color, Enums.BotType.Bot);
client.SendMessage($"check color {color}?", sendTo, Enums.BotType.Bot);

// host a channel from channel x to channel y -> the client can host different channels to other channels with the right permission to do so
client.Commands.Host("blushyface","blushyfacebot");

// unhost a specific channel -> the client can unhost different channels with the right permission to do so
client.Commands.Unhost("blushyface");
            
// same as with hosting -> raid channel from channel x to channel y / unraid from channel x ->  the client can raid / unraid different channels with the right permission to do so
client.Commands.Raid("blushyface", "blushyfacebot");
client.Commands.UnRaid("blushyface");

// check channel VIPs   
client.Vips("blushyface");

// colored message highlighted as [bot / broadcaster]
client.Commands.Me("test", "blushyface", Enums.BotType.Broadcaster);            

Step 5 That’s all folks
The library is by no means feature complete as of yet (for example API support is only partially implemented ) but I’m working on it as much as I can to make it a fully functional library for general use.

Features to implement or not fully working yet
-> separate / add more event based notifications including commands? -> ban / unban, host / unhost, whisper, user name color change events etc to reduce the OnChatMessageReceive spam to show only chat messages from users instead
-> add a permission based command system and check for required scopes before using them
-> add a self hosted project example to show how to create and use OAuth tokens with custom scopes directly from the library
-> add a web interface example to show how to use the client from a web browser
-> add a GUI (WPF) example to show how to use the client from an app
-> support for 3rd party API’s such as Streamlabs with additional events -> tip event?
-> support for the Twitch API v5 / Helix
-> support for Discord API -> for example show Twitch chat messages in discord or use command directly to Twitch chat etc
-> support for user plugins -> for devs that want to enable plugins for their users for example custom commands etc)

-> rename project files that I feel is out of place like -> BotType (bot / broadcaster / default) which is probably better named as something else
-> etc.

The library in itself doesn’t really do anything fancy and it’s up to whoever uses it to make the most of it with the features provided. Lets say you want to timeout a specific user because they said something you can write something like this ->
in -> OnReceivedMessages

add

if (e.User.Equals("blushyface"))
{
   client.Commands.TimeOut("blushyfacebot", "blushyface", 60);
}

That said, I would not recommend to write it like that directly in the event handler but that’s how you can time out a user that chats.

 

Screenshots

Console output.
The console output (when both bot and broadcaster credentials are provided) will show every received messages, to filter out bot messages change -> OnReceivedMessages by adding ->

if (e.BotType != Enums.BotType.Broadcaster) return;
... rest of code

 

Different user name colors

Ban message in client

Ban message in Twitch chat

Sub events, this includes new subs, re subs and submysterygift

Bit event

Project files

Full code example from tester project

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BlushyFace.Twitch.Chat;
using BlushyFace.Twitch.Chat.Helpers;
using BlushyFace.Twitch.Helpers;
using Jint.Parser;

namespace BlushyFaceTester.Tests
{
    public class ChatTester
    {
        public static void Main(String[] args)
        {

            #region setup bot / broadcaster

            // generating tokens can be done from: https://twitchapps.com/tmi/

            var bot = new Credential("oauth:..");
            var broadcaster = new Credential("oauth:..");

            #endregion

            #region  create the client to connect to Twitch chat and listen to various events (chat message, user join / part, sub event. bit event etc

            // create the connection, bot is required, broadcaster is optional [receive broadcaster only messages]
            var client = new BlushyFace.Twitch.Chat.Client(bot, broadcaster);

            // client.OnRawChatMessageReceive += ReceivedRawMessages;
            client.OnChatMessageReceive += OnReceivedMessages;
            client.OnUserJoin += OnUserJoin;
            client.OnUserPart += OnUserPart;
            // client.OnSendRawMessage += OnSendRawMessage;
            client.OnSub += OnSub;
            client.OnBit += OnBit;

            #endregion

            #region Connect to Twitch chat and join channels

            // connect to Twitch chat
            client.Connect();

            // join a channel
            // client.JoinChannel("blushyface");
            client.LeaveChannel("blushyface");

            #endregion

            #region Send a message / whisper to a specific channel as a [bot / broadcaster]


            // send chat to  channel [default as bot]
            // client.SendMessage("send text to default channel");

            // send text to a specific channel as a bot / broadcaster
            //var sendTo = "creativestreamer";
            //client.SendMessage("send text to specific channel", sendTo);
            //client.SendMessage("send text to specific channel as bot [default]", sendTo, Enums.BotType.Bot);
            //client.SendMessage("send text to specific channel as broadcaster", sendTo, Enums.BotType.Broadcaster);

            // sends a whisper [default is from bot]
            // client.Commands.Whisper("blushyface", "send from default");

            // sends a whisper as broadcaster
            //  client.Commands.Whisper("blushyfacebot", "send from broadcaster", Enums.BotType.Broadcaster);

            #endregion

            #region example how to use various commands as a [bot / broadcaster]

            // commands new -> client.Commands.X
            // commands old -> client.X 

            // change username color -> [color] [bot as default / broadcaster]
            var color = Enums.UserNameColor.Green;
            var sendTo = "creativestreamer";

            // client.Commands.Color(color, Enums.BotType.Broadcaster);
            client.SendMessage($"cool color {color}?", sendTo, Enums.BotType.Broadcaster);

            //color = Enums.UserNameColor.DodgerBlue;
            client.Commands.Color(color, Enums.BotType.Broadcaster);
            client.SendMessage($"cool color {color}?", sendTo, Enums.BotType.Broadcaster);

            //color = Enums.UserNameColor.SpringGreen;
            client.Commands.Color(color, Enums.BotType.Bot);
            client.SendMessage($"cool color {color}?", sendTo, Enums.BotType.Bot);

            //color = Enums.UserNameColor.HotPink;
            client.Commands.Color(color, Enums.BotType.Bot);
            client.SendMessage($"cool color {color}?", sendTo, Enums.BotType.Bot);

            //color = Enums.UserNameColor.HotPink;
            client.Commands.Color(color, Enums.BotType.Broadcaster);
            client.SendMessage($"cool color {color}?", sendTo, Enums.BotType.Broadcaster);

            // change username color -> [hex color] [bot / broadcaster] -> turbo required
            client.Commands.Color("#4287f5", Enums.BotType.Broadcaster);

            // old way, do not use -> will not work anymore but it's here to show how it was before
            // client.Color(Enums.UserNameColor.Green, Enums.BotType.Broadcaster);

            // bans a user -> [from channel], [username], [optional reason]
            // the bot can be mod in multiple channels so you can ban users in specific channels
            client.Commands.Ban("blushyfacebot", "a_d3mon", "this is a test, will unban immediately after :o)");

            // unban a user -> [from channel], [username]
            client.Commands.Unban("blushyfacebot", "a_d3mon");

            // sends a message to a channel, optionally send message as bot / broadcaster -> default as bot
            client.SendMessage("fancy color", "blushyface", Enums.BotType.Broadcaster);

            // host / unhost a channel
            client.Commands.Host("blushyface", "blushyfacebot");
            client.Commands.Unhost("blushyface");

            // raid / unraid a channel
            client.Commands.Raid("blushyface", "blushyfacebot");
            client.Commands.UnRaid("blushyface");

            // check channel VIPs
            client.Commands.Vips("blushyface");

            // colored message as [bot / broadcaster]
            client.Commands.Me("test", "blushyfacebot", Enums.BotType.Broadcaster);
            #endregion

            #region Misc -> mock data to chat, parse twitch chat / bit emotes examples etc

            // inserts various raw messages as it was send from Twitch chat
            MockData(client);


            var rawMessage = @"";

            // returns parsed chat emote id from raw message. Example: {300273041} => {..} = blushyLove emote
            var chatEmotes = BlushyFace.Twitch.API.Helpers.TwitchEmotes.GetChatEmotes(rawMessage);

            // this is probably not a relevant example as it's used to parse them further in my web client
            // returns parsed bit emote. Example: cheer69 => [cheer_1] 69 , [..] = returns cheer_1 since 69 is lower than next tier (100), adds a default space + amount cheered 
            // [cheer_1] can be light / dark , animated / static / scale etc
            var bitEmotes = BlushyFace.Twitch.API.Kraken.Bits.GetBitEmotes("");

            #endregion

            Console.ReadKey();
        }

        #region client EventHandlers


        static void OnReceivedRawMessages(object sender, OnReceiveRawMessageArgs e)
        {
            Console.WriteLine($"[OnReceivedRawMessages] > {e.RawMessage}");
        }


        static void OnReceivedMessages(object sender, OnReceiveChatMessageArgs e)
        {
            var output = string.Empty;

            if (!e.Channel.Equals(""))
                output += e.Channel + " ";

            if (!e.User.Equals(""))
                output += e.Capability + " ";

            if (!e.Numeric.Equals(""))
                output += e.Numeric + " ";

            if (!e.User.Equals(""))
                output += e.User + ": ";

            if (!e.Message.Equals(""))
                output += e.Message;

            Console.WriteLine($"[OnReceivedMessages] > {output}");
        }

        static void OnUserJoin(object sender, OnChatRoomJoinArgs e)
        {
            Console.WriteLine($"[OnUserJoin] JOIN >> {e.Channel} {e.User}".Trim());
        }

        static void OnUserPart(object sender, OnChatRoomPartArgs e)
        {
            Console.WriteLine($"[OnUserPart] PART >> {e.Channel} {e.User}".Trim());
        }

        static void OnSendRawMessage(object sender, OnSendRawMessageArgs e)
        {
            Console.WriteLine($"[OnSendRawMessage] < {e.Message}".Trim());
        }
        static void OnSub(object sender, OnSubArgs e)
        {
            Console.WriteLine($"[OnSub] > {e.SystemMsg}".Trim());
        }
        static void OnBit(object sender, OnBitArgs e)
        {
            Console.WriteLine($"[OnBit] > {e.From}  {e.Bits}".Trim());
        }

        #endregion

        #region parse mock data 

        public static void MockData(Client client)
        {
            string rawMessage;

            //note -> chat emotes ending with _HF => other variants => _HF  _BW _SG
            rawMessage = @"";
            client.InsertRawMessage(rawMessage);

            // example #2
            rawMessage = @"";
            client.InsertRawMessage(rawMessage);

            // new sub event
            rawMessage = @"";
            client.InsertRawMessage(rawMessage);

            // resub event
            rawMessage = @"";
            client.InsertRawMessage(rawMessage);

            // subgift event
            rawMessage = @"";
            client.InsertRawMessage(rawMessage);

            // bit event
            rawMessage = @"";
            client.InsertRawMessage(rawMessage);

            // Skip Sub Mode redeem example
            // When subscribers only mode in chat. People can spend channel points to send a message to chat
            rawMessage = @"";
            client.InsertRawMessage(rawMessage);
        }

        #endregion

    }
}

Download
As this is an ongoing project there is no public download at the moment but check our discord for the latest updates and available test builds that contain example projects for you to break when I need people to test the library for me before I release it for public use.

 

Got feedback / suggestions etc?
If you have any questions / feedback / suggestions to make the library better let me know what you would like me to add feel and free to reply below, make a post on the community forums -> blog.blushyface.com/c/general-purpose-c-library-(wip)/ or join our Discord -> discordapp.com/invite/Z7fYft4

Notify of
guest
0 Comments
Inline Feedbacks
View all comments

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