Git Product home page Git Product logo

twitchlib's Introduction

TwitchLib - Twitch Chat, API, and PubSub C# Library

Travis CI Build Test NuGet Pre Release

Overview

TwitchLib is a C# library that attempts to harness Twitch Chat, Twitch API, and Twitch PubSub into a single package. Using TwitchLib, you can connect to a Twitch channel's chat and send and receive chat messages as well as send and receive whisper messages! You can also fetch general Twitch API data like followers or user details as well as make authenticated channel modifications like stream title and game, as well as actions like commercials and and resetting of the stream key. Additionally, the TwitchLib project contains an example project that demonstrates the majority of functionality presented in the library.

Documentation

I'm in the process of developing a website for very rough documentation and usage of this library. Parts of it is "done" and available if you are interested: http://swiftyspiffy.com/TwitchLib

Sample Implementation

TwitchClient client = new TwitchClient(new ConnectionCredentials("my_username", "my_oauth"), "my_channel");

client.OnConnected += clientConnected;
client.OnMessageReceived += clientMessageReceived;
client.OnWhisperCommandReceived += clientWhsiperReceived;

client.Connect();

client.SendMessage("A chat message.");
client.SendWhisper("whisper_receiver", "A whisper message.");

TwitchApi.Streams.BroadcasterOnline("my_favorite_streamer");
TwitchApi.Follows.GetFollows("my_favorite_streamer");
TwitchApi.Subscriptions.GetSubscriberCount("my_favorite_streamer", "my_favorite_streamer's_access_token");
TwitchApi.Streams.RunCommercial(TwitchApi.CommercialLength.Seconds180, "my_favorite_streamer", "my_favorite_streamer's_access_token");

Availability

Available via Nuget: Install-Package TwitchLib

TwitchClient

  • Initiailized using channel and ConnectionCredentials
  • Chat Events:
    • OnLeftChannel - Similar to OnJoinedChannel, but fires when the client leaves a channel.
    • OnIncorrectLogin - Fires when an invalid login is returned by Twitch
    • OnConnected - Fires on listening and after joined channel, returns username and channel
    • OnDisconnected - Fires when TwitchClient disconnects.
    • OnMessageReceived - Fires when new chat message arrives, returns ChatMessage
    • OnNewSubscriber - Fires when new subscriber is announced in chat, returns Subscriber
    • OnReSubscriber - Fires when existing subscriber resubscribes, returns ReSubscriber
    • OnChannelStateChanged - Fires when channel state is changed
    • OnUserJoined - New viewer/chatter joined the chat channel room.
    • OnUserLeft - Viewer/chatter left (PARTed) the chat channel.
    • OnChatCommandReceived - Fires when command (uses custom command identifier) is received.
    • OnMessageSent - Fires when a chat message is sent.
    • OnUserStateChanged - Fires when a user state is received.
    • OnModeratorJoined - Fires when a moderator joins chat (not necessarily real time)
    • OnModeratorLeft - Fires when a moderator leaves chat (not necessarily real time)
    • OnHostLeft - Fires when a hosted channel goes offline
    • OnExistingUsersDetected - Fires when list of users message is received from Twitch (generally when entering the room)
    • OnHostingStarted - Fires when the joined channel begins hosting another channel.
    • OnHostingStopped - Fires when the joined channel quits hosting another channel.
    • OnChatCleared - Fires when a moderator sends a clear chat command (channel).
    • OnUserTimedout - Fires when client detects a viewer was timedout (moderator, viewer, timeout duration, timeout reason, channel).
    • OnUserBanned - Fires when client detects a viewer was banned (moderator, viewer, ban reason, channel).
    • OnModeratorsReceived - Fires when a list of moderators is returned by Twitch (this happens by calling GetChannelModerators in the client).
    • OnChatColorChanged - Fires when confirmation is received from Twitch that chat color has been successfully changed.
    • OnNowHosting - Fires when the home channel begins hosting another channel.
    • OnBeingHosted - Fires when the library sees that another channel is hosting the broadcaster's channel. YOU MUST BE CONNECTED AS THE BROADCASTER.
    • OnSendReceiveData - Fires whenever the TwitchClient sends or receives data to/from Twitch. Excellent for debugging/custom interpretation.
    • OnViewerTimedout - Fires whenever a user is timedout in chat.
  • Whisper Events:
    • OnWhisperReceived - Fires when a new whisper message is received, returns WhisperMessage
    • OnWhisperCommandReceived - Fires when command (uses custom command identifier) is received.
    • OnWhisperSent - Fires when a whisper is sent.
  • SendRaw(string message) - Sends RAW IRC message
  • SendMessage(string message) - Sends formatted Twitch channel chat message
  • ChangeChatColor(ChatColor) - Requests a change in chat color to Twitch. You should listen to OnChatColorChanged event, and be aware that the change requires a few seconds to propagate to all viewer's browsers.
  • SendWhisper(string receiver, string message) - Sends formatted Twitch whisper message
  • Handled chat message types
  • Disconnect - Disconnects chat client from Twitch IRC
  • Reconnect - Reconnects chat client given existing credentials
  • JoinedChannel(string channel) - Client will attempt to join passed in channel.
  • LeaveChannel(string channel) - Client will attempt to leave channel.
  • GetChannelModerators - Sends a request for all of the channel moderators (you MUST listen/handle the OnModeratorsReceived event).

TwitchAPI (All methods have accompanying Async methods. Append Async to method name)

  • BroadcasterOnline(string channel) - Async function returns bool of whether or not streamer is streaming
  • GetTwitchChannel(string channel) - Async function returns TwitchCHannel of a specific channel
  • UserFollowsChannel(string username, string channel) - Async function returns bool if a user follows a channel
  • GetChatters(string channel) - Aysync function returns list of Chatter objects detailing each chatter in a channel
  • UpdateStreamTitle(string status, string username, string access_token) - Async function that changes stream title
  • UpdateStreamGame(string game, string username, string access_token) - Async function that updates a streams's game
  • UpdateStreamTitleAndGame(string status, string game, string username, string access_token) - Async function that updates a stream's status and game
  • ResetStreamKey(string username, string access_token) - Async function that resets the stream key of a channel
  • GetChannelVideos(string channel, [int limit], [int offset], [bool onlyBroadcasts], [bool onlyHLS]) - Async function that returns list of TwitchVIdeo objects
  • RunCommercial(Valid_Commercial_Lengths length, string username, string access_token) - A sync function that runs a commercial of variable length on a channel
  • GetChannelHosts(string channel) - Async function that returns a string list of channels hosting a specified channel (undocumented)
  • GetTeamMembers(string teamName) - Async function that returns a TwitchTeamMember list of all members in a Twitch team (undocumented)
  • ChannelHasUserSubscribed(string username, string channel, string access_token) - Returns true or false on whether or not a user is subscribed to a channel.
  • GetTwitchStream(string channel) - Returns TwitchStream object containing API data related to a stream. Throws StreamOfflineException and BadResourceException.
  • GetTwitchStreams(List channels) - Returns list of Stream objects for each channel passed in.
  • GetTwitchFollower(string channel) - Returns asc or desc list of followers from a specific channel, returns list of TwitchFollower objects.
  • GetUser(string username) - Returns a User object which represents a User object Twitch has.
  • GetUptime(string channel) - Returns TimeSpan object representing time between creation_at of stream, and now.
  • GetChannelFeed(string channel, int limit = 10, string cursor = null) - Returns a FeedResponse which houses all feed posts, comments, reactions, etc.
  • SetClientId(string clientId) - Sets ClientId for inclusion in all API calls per Twitch requirement.
  • SetAccessToken(string accessToken) - Sets access token so that it is no longer needed to call an api calls (token stored in memory).
  • GetFollowedUsers(string channel, int limit = 25, int offset = 0, Common.SortKey sortKey) - Gets a list of users a user follows.
  • SearchChannels(string query, int limit = 25, int offset = 0) - Search uses a term for channels and returns a list of channel objects.
  • SearchStreams(string query, int limit = 25, int offset = 0, bool? hls = null) - Search uses a term for streams and returns a list of stream objects.
  • SearchGames(string query, bool live = false) - Search uses a term for games and returns a list of game objects.
  • FollowChannel(string username, string channel, string accessToken) - Follows a specific channel.
  • UnfollowChannel(string username, string channel, string accessToken) - Unfollows a specific channel.
  • GetBlockedList(string username, string accessToken, int limit = 25, int offset = 0) - Returns a list of Block objects, each featuring a user object and an update date/time.
  • BlockUser(string username, string blockedUsername, string accessToken) - Blocks user, returns a Block object.
  • UnblockUser(string username, string blockedUsername, string accessToken) - Unblocks user.
  • GetChannelEditors(string channel, string accessToken) - Retrieves a list of User objects representing users that are channel editors.
  • GetChannelBadges(string channel) - Fetches a list of Badge objects representing each badge available in a channel.
  • GetChannelsObject(string channel) - Fetches a Channels object from an undocumented endpoint (I think), which contains steam Id and other interesting properties.
  • GetChannelFromSteamId(string steamId) - Fetches the name of a channel given a user's Steam Id (if their Steam is linked to their Twitch).
  • GetFeaturedStreams() - Fetches the featured streams (front page streams), along with the stream object, stream text, and other properties.
  • GetGamesByPopularity() - Fetches the top games by viewer count, includes listings with game objects, viewer counts, and channel counts.
  • PostToChannelFeed() - Posts to channel with the ability to tweet out the post as well. Returns Tweet URL and Post object.
  • DeleteChannelFeedPost() - Deletes a channel feed post by the passed in post Id.

TwitchPubSub

  • Events:
    • onOpen() - Fires when connection is successful to Twitch PubSub service. You should listen to a topic in this eventhandler.
    • onError() - Any error in the service will fire this event along with an exception.
    • onClose() - If service is disconnected intentionally or by error, this will fire.
    • onListenResponse() - Fires when a response to a listen command is sent. Supports successful (no error), and failed (error) responses.
    • onTimeout() - This fires when a timeout occures. Includes user timedout, timeout duration, timeout reason, and moderator who did the timeout.
    • onBan() - This fires when a ban occures. Includes banned user, ban message, and moderator that did ban.
    • onUnban() - This fires when an unban occures. Includes unbanned user, and moderator who did the unban.
    • onHost() - This fires when a channel is hosted. Properties include moderator and hosted channel.
    • onBitsEvent() - This fires when bits are sent to chat, it a includes significant amount of data on the event.
  • Supported Topics:
    • chat_moderator_actions - This topic allows for listening to moderator events in chat.
    • channel-bitsevents - This topic allows for listening to events when bits are sent in chat.
  • Connect() - Connects to Twitch PubSub service (YOU HAVE 15 SECONDS TO LISTEN TO A CHANNEL BEFORE BEING DISCONNECTED, you should use onOpen event to begin a listen)
  • Disconnect() - Disconnects from service

Twitch Services

  • FollowerService - Monitors channel for new followers on custom interval and query count values. Fires event when new followers are detected. (OPTIONAL)
  • MessageThrottler - Property object that can be assigned to either Chat or Whisper clients, fires events and blocks sending of messages given a specific time period in order to prevent Twitch ToS violations. (OPTIONAL)

Twitch Token Generator

A lot of you have been messaging me asking how to generate an access token for a specific priviledge. I've gone ahead and made a tool to do just this. Should also make testing much easier. https://twitchtokengenerator.com

Examples and Implementations

  • TwitchLibExample - This project is included in this repo as a master example project.
  • PFCKrutonium's TwitchieBot - This project implements the bot using VisualBasic.
  • Bacon_Docunt VOD on building a Twitch bot using TwitchLib: twitch.tv/videos/115788601

Libraries/Dependencies Utilized

  • Newtonsoft.Json (7.0.1) - JSON parsing class. Used to parse Twitch API calls.

Support/Discussion

The Twitch Discord server has a #developer channel that has constant discussion about developing for the Twitch platform. You can likely get support and discuss ideas there. A link is below: https://discord.gg/0gHwecaLRAzrRYWi

Contributors

License

MIT License. © 2016-2017 Cole

twitchlib's People

Contributors

glitchhound avatar jxlarrea avatar latenightbuddha avatar mast3rm1nd avatar prom3theu5 avatar rafaltra avatar somebodyodd avatar spiritoffice avatar swiftyspiffy avatar toffaste1337 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.