Building a Twitch.tv Chat Bot with Go – Part 1

Part 2

For those that are unfamiliar, Twitch.tv is a live-streaming platform for all things creative and related to games. Not necessarily just video games. Content creators play board games, roll-play games (e.g. Dungeons & Dragons), and everything in between. On this platform, streamers are able to interact with a live chat room just for their channel (with a delay).

To facilitate the streamer and chat interaction, Twitch uses a variant of IRC, and provides documentation for their implementation on their Twitch Developers page. This allows developers to create chat bots that can moderate chat rooms, interact with chatters, and automate certain tasks for streamers. There are several well known ones, with many features, but the chat bot built in this walkthrough won’t be as complex.

Important Notes

The code for this project can be found on GitHub and is under the MIT license. Anyone who is developing a chat bot for Twitch chat should read and abide by their Developer Agreement and their Terms of Service. You should also be aware of the IRC message limits which could result in a timeout (temporary ban) or even a lengthy IP ban, if they are broken. Therefore you should follow the rules, and make an alternate account just for your bot (in case of mistakes).

On that note, I recommend having two accounts in total, one for testing the bot and one acting as the bot. That way you can test the bot in your own chat, rather than someone else’s chat (which is generally a bad idea).

Getting Started

In this walkthrough we’ll go step by step and fill out all the features that will be needed for the chat bot to function in a live chat room.

Here are the requirements for this project:

Once you have the requirements sorted, we can get started!

Go ahead and run the following in your terminal, if you haven’t already:

$ go get github.com/foresthoffman/twitchbot

Then you can navigate to the package’s source:

Linux/MacOS

$ cd $GOPATH/src/github.com/foresthoffman/twitchbot

Windows

> cd %GOPATH%/src/github.com/foresthoffman/twitchbot

After which you can turn back time by running the following Git command:

$ git checkout -b walkthrough step-0

This will create a brand new branch called “walkthrough” and bring it to Step 0, where we’ll begin writing code.

If you get stuck and something won’t compile, you can always jump forward or backward a step with (where “#” represents the index of the step you want to jump to):

$ git reset --hard step-#

If you want to jump to the end, you can always run:

$ git reset --hard master

Step 0

Okay, so open up twitchbot.go in your favorite editor, and let’s look at what we’re working with.

package twitchbot

// TODO:
// 1. Connect to a Twitch.tv Chat channel.
//  a. Pass along necessary information for the connection.
//   i.   The IRC (chat) server.
//   ii.  The port on the server.
//   iii. The channel we want the bot to join.
//   iv.  The bot's name.
//   v.   A secure key to allow the bot to connect indirectly (not through the website).
//   vi.  A maximum speed at which the bot can respond.
// 2. Listen for messages in the chat.
// 3. Do things based on what is happening in the chat.

So, ignoring the licensing bit, we have a layout of what we need for the bot to function, in addition to some nice-to-haves. Let’s focus on the most important one, which is #1.

  1. Connect to a Twitch.tv Chat channel.

In order to accomplish #1 we need some data:

  • The IRC (chat) server.
  • The port on the server.
  • The channel we want the bot to join.
  • The bot’s name.
  • A secure key to allow the bot to connect indirectly (not through the website).
  • A maximum speed at which the bot can respond.

Using placeholder usernames and OAuth codes (you’ll use your own when we get to it), we have the following data:

  • irc.chat.twitch.tv
  • 6667
  • TwitchBotTest
  • TwitchBot
  • oauth:secretsecretsecretsecretsecret
  • once every 20/30 of a millisecond

So, we’ll need the bot to keep track of 5 strings and one time interval. So, let’s create a struct to handle that data, in twitchbot.go:

package twitchbot

import (
    "time"
)

type BasicBot struct {
    Channel string
    MsgRate time.Duration
    Name    string
    Port    string
    OAuth   string
    Server  string
}

Now we have some nicely named fields to hold our data. Here are some things to keep in mind though: Channel must be lowercase or else the bot will successfully connect to the server, but will be blind to any messages in chat; and by having an OAuth field, we have to hard code our bot’s password, which is not considered best practice.

Rather than having an OAuth field, let’s accept a path to a limited-access directory from which the bot’s OAuth token can be read at runtime. That way it can be easily changed out, without having to recompile our source code or restart the bot.

package twitchbot

import (
    "time"
)

type BasicBot struct {
    Channel     string
    MsgRate     time.Duration
    Name        string
    Port        string
    PrivatePath string // replaced the OAuth field
    Server      string
}

I’ve left out the field comments and todo-list to save space, but that’s it for Step 0. You can build & fmt your code now!

$ go fmt ./... && go build

Part 2

Thank you for reading! If you have any questions you can hit me up on Twitter (@forestjhoffman) or email (forestjhoffman@gmail.com).

Image Attribution

  • Go Gopher, Renee French (Creative Commons Attributions 3.0)
  • Glitch, Copyright of Twitch.tv