TwitchLib is a powerful C# library that allows for interaction with various Twitch services. Currently supported services are: chat and whisper, API's (v3, v5, helix, undocumented, and third party), PubSub event system, and Twitch Extensions. Below are the descriptions of the core components that make up TwitchLib.
- TwitchLib.Client: Handles chat and whisper Twitch services. Complete with a suite of events that fire for virtually every piece of data received from Twitch. Supports Twitch Rooms. Helper methods also exist for replying to whispers or fetching moderator lists.
- TwitchLib.Api: Complete coverage of v3, v5, and Helix endpoints. The API is now a singleton class. This class allows fetching all publically accessable data as well as modify Twitch services like profiles and streams.
- TwitchLib.PubSub: Supports all documented Twitch PubSub topics as well as a few undocumented ones.
- TwitchLib.Extension: EBS implementation for validating requests, interacting with extension via PubSub and calling Extension endpoints.
- TwitchLib.Unity: Unity wrapper system for TwitchLib to allow easy usage of TwitchLib in Unity projects!
- TwitchLib.Webhook: Implements ASP.NET Core Webhook Receiver with TwitchLib.
- TwitchLib.Client:
- Send formatted or raw messages to Twitch
- Chat and Whisper command detection and parsing
- Supports Twitch Rooms
- Helper methods
- Timeout, ban, unban users
- Change chat color and clear chat
- Invoke stream commercials and hosts
- Emote only, follower only, subscriber only, and slow mode
- Reply-to whisper support
- Handles chat and whisper events:
- Connected and Joined channel
- Channel and User state changed
- Message received/sent
- Whisper received/sent
- User joined/left
- Moderator joined/left
- New subscriptions and resubscriptions
- Hosting and raid detection
- Chat clear, user timeouts, user bans
- TwitchLib.APi:
- Supported Twitch API endpoitns:v3, v5, Helix
- Supported API sections:
- Badges, Bits, Blocks
- ChannelFeeds, Channels, Chat, Clips, Collections, Communities,
- Follows
- Games
- Ingests
- Root
- Search, Streams, Subscriptions
- Teams
- ThirdParty
- Username Changes courtesy of CommanderRoot's twitch-tools.rootonline.de/
- Moderator Lookup courtesy of 3v's https://twitchstuff.3v.fi
- Twitch Authenticaiton Flow courtesy of swiftyspiffy's https://twitchtokengenerator.com/
- Undocumented
- ClipChat
- TwitchPrimeOffers
- ChannelHosts
- ChatProperties
- ChannelPanels
- CSMaps
- CSStreams
- RecentMessages
- Chatters
- RecentChannelEvents
- ChatUser
- UsernameAvailable
- User
- Videos
- Services
- FollowerService: Service for detection of new followers in somewhat real time.
- LiveStreamMonitor: Service for detecting when a channel goes online/offline in somewhat real time.
- MessageThrottler: Service to throttle chat messages to abide by Twitch use requirements.
- TwitchLib.PubSub:
- Supported topics:
- ChatModeratorActions
- BitsEvents
- VideoPlayback
- Whispers
- Subscriptions
- Supported topics:
- TwitchLib.Extension:
- Developed to be used as part of an EBS (extension back-end service) for a Twitch Extension.
- Perform API calls related to Extensions (create secret, revoke, channles using extension, etc.)
- Validation of requests to EBS using extension secret and JWT.
- Interact with extension via PubSub.
For complete library documentation, view the doxygen docs here.
Below are basic examples of how to utilize each of the core components of TwitchLib. These are C# examples, but this library can also be used in Visual Basic.
using TwitchLib;
using TwitchLib.Models.Client;
using TwitchLib.Events.Client;
TwitchClient client;
ConnectionCredentials credentials = new ConnectionCredentials("twitch_username", "access_token");
client = new TwitchClient(credentials, "channel_to_join");
client.OnJoinedChannel += onJoinedChannel;
client.OnMessageReceived += onMessageReceived;
client.OnWhisperReceived += onWhisperReceived;
client.OnNewSubscriber += onNewSubscriber;
client.Connect();
private void onJoinedChannel(object sender, OnJoinedChannelArgs e) {
client.SendMessage("Hey guys! I am a bot connected via TwitchLib!");
}
private void onMessageReceived(object sender, OnMessageReceivedArgs e) {
if(e.ChatMessage.Message.Contains("badword"))
client.TimeoutUser(e.ChatMessage.Username, TimeSpan.FromMinutes(30), "Bad word! 30 minute timeout!");
}
private void onCommandReceived(object sender, OnWhisperCommandReceivedArgs e) {
if(e.Command == "help")
client.SendMessage($"Hi there {e.WhisperMessage.Username}! You can view all commands using !command");
}
private void onWhisperReceived(object sender, OnWhisperReceivedArgs e) {
if(e.WhisperMessage.Username == "my_friend")
client.SendWhisper(e.WhisperMessage.Username, "Hey! Whispers are so cool!!");
}
private void onNewSubscriber(object sender, OnNewSubscriberArgs e) {
if(e.Subscriber.IsTwitchPrime)
client.SendMessage($"Welcome {e.Subscriber.DisplayName} to the substers! You just earned 500 points! So kind of you to use your Twitch Prime on this channel!");
else
client.SendMessage($"Welcome {e.Subscriber.DisplayName} to the substers! You just earned 500 points!");
}
For a complete list of TwitchClient events and calls, click here
Note: TwitchAPI is now a singleton class that needs to be instantiated with optional clientid and auth token. Please know that failure to provide at least a client id, and sometimes an access token will result in exceptions. The v3 subclass operates almost entirely on Twitch usernames. v5 and Helix operate almost entirely on Twitch user id's. There are methods in all Twitch api versions to get corresponding usernames/userids.
using TwitchLib;
using TwitchLib.Models.API;
private static TwitchLib.TwitchAPI api;
api = new TwitchLib.TwitchAPI("client_id", "access_token");
var subscription = await api.Channels.v5.CheckChannelSubscriptionByUserAsync("channel_id", "user_id");
var allSubscriptions = await api.Channels.v5.GetAllSubscribersAsync("channel_id");
var userFollows = await api.Users.v5.GetUserFollowsAsync("user_id");
var channelFollowers = await api.Channels.v5.GetChannelFollowersAsync("channel_id");
bool userFollowsChannel = await api.Users.v5.FollowChannelAsync("user_id", "channel_id");
bool isStreaming = await api.Streams.v5.BroadcasterOnlineAsync("channel_id");]
await api.Channels.v5.UpdateChannelAsync("channel_id", "New stream title", "Stronghold Crusader");
For a complete list of TwitchAPI calls, click here
using TwitchLib;
TwitchPubSub pubsub = new TwitchPubSub();
pubsub.OnPubSubServiceConnected += onPubSubConnected;
pubsub.OnListenResponse += onPubSubResponse;
pubsub.OnBitsReceived += onPubSubBitsReceived;
pubsub.Connect();
private void onPubSubConnected(object sender, object e) {
// MY ACCOUNT ID, MY OAUTH
pubsub.ListenToWhispers(0, "oauth_token");
}
private void onPubSubResponse(object sender, OnListenResponseArgs e) {
if (e.Successful)
MessageBox.Show($"Successfully verified listening to topic: {e.Topic}");
else
MessageBox.Show($"Failed to listen! Error: {e.Response.Error}");
}
private void onPubSubBitsReceived() {
MessageBox.Show($"Just received {e.BitsUsed} bits from {e.Username}. That brings their total to {e.TotalBitsUsed} bits!");
}
For a complete list of TwitchPubSub functionality, click here
See the Extension README here.
Björn has kindly created a guide for using TwitchLib with Unity. To view the guide, click here..
- Recent commits in projects using TwitchLib: Link
- Bacon_Donut's VOD on building a Twitch bot using TwitchLib: twitch.tv/videos/115788601
- Prom3theu5's Conan Exiles Dedicated Server Updater / Service - Steam Github
- Von Jan Suchotzki's German Video Tutorial Series - His Website Youtube
- DHSean's TwitchAutomator Reddit Github
- Moerty's Avia Bot, a fully featured bot that is a good example of a built out bot: https://github.com/Moerty/AivaBot
- HardlyDifficult's Chat Bot Creation VODs: #1 #2 #3 #4
- Prom3theu5's TwitchBotBase - github.com/prom3theu5/TwitchBotBase
- Trump Academi's ASP.NET TwitchLib Implementation - trumpacademi.com/day-9-twitch-bot-and-mvc-asp-net-implementing-twitchlib/
- ubhkid's Zombie Twitch chat game/overlay - reddit.com/r/Unity3D/comments/6ll10k/i_made_a_game_for_my_twitch_chat/
- FPH SpedV: Virtual Freight Company Tool - sped-v.de
- Foglio's Tera Custom Cooldowns - Tera Custom Cooldowns
To install this library via NuGet via NuGet console, use:
Install-Package TwitchLib
and via Package Manager, simply search:
TwitchLib
You are also more than welcome to clone/fork this repo and build the library yourself!
- Newtonsoft.Json 7.0.1+ (nuget link) (GitHub)
- WebSocketSharp-NonPreRelease (nuget link) (GitHub)
- Cole (@swiftyspiffy)
- SkyHuk (@SkyHukTV)
- Nadermane (@Nadermane)
- BenWoodford (BenWoodford)
- igor523 (igor523)
- jxlarrea (jxlarrea)
- GlitchHound (GlitchHound)
- Krutonium (Krutonium)
- toffaste1337(toffaste1337)
- Mr_Examed (Mr_Examed)
- XuluniX (XuluniX)
- prom3theu5 (@prom3theu5)
- Ethan Lu (elu00)
- BeerHawk (BeerHawk)
- Tobias Teske (Syzuna)
- LuckyNoS7evin (luckynos7evin)
- Peter Richter (DumpsterDoofus)
- Mahsaap (@Mahsaap)
This project is available under the MIT license. See the LICENSE file for more info.
:)