Building a Twitch.tv Chat Bot with Go - Part 1
Tags: showdev, webdev, go, api
Published: 2017-12-20 04:12:58

---
title: Building a Twitch.tv Chat Bot with Go - Part 1
published: true
description: A walkthrough of how to build a basic Twitch.tv IRC chat bot.
tags: showdev, webdev, go, api
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ileql7czrfggl7slt9n.jpg
series: Building a Twitch.tv Chat Bot with Go
---

EDIT (5/13/2019): Since the time of writing the package has been renamed from "twitchbot" to "bot". However, the tagged releases used for this tutorial still use the old name.

For those that are unfamiliar, [Twitch.tv](https://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](https://tools.ietf.org/html/rfc1459.html), and provides documentation for their implementation on their [Twitch Developers page](https://dev.twitch.tv/docs/irc). 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](https://github.com/foresthoffman/bot) and is under [the MIT license](https://github.com/foresthoffman/bot/blob/master/LICENSE). Anyone who is developing a chat bot for Twitch chat should read and abide by their [Developer Agreement](https://www.twitch.tv/p/legal/developer-agreement/) and their [Terms of Service](https://www.twitch.tv/p/legal/terms-of-service/). You should also be aware of the [IRC message limits](https://dev.twitch.tv/docs/irc#irc-command-and-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:
- [Have Go installed](https://golang.org/doc/install)
- [Have two Twitch.tv Accounts](https://www.twitch.tv/signup) (one for you, and one for your bot)

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

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

```bash
$ go get github.com/foresthoffman/bot
```

Then you can navigate to the package's source:

Linux/MacOS

```bash
$ cd $GOPATH/src/github.com/foresthoffman/bot
```

Windows

```bash
> cd %GOPATH%/src/github.com/foresthoffman/bot
```

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

```bash
$ 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):

```bash
$ git reset --hard step-#
```

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

```bash
$ 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.


```go
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`:

```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.

```go
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!

```bash
$ go fmt ./... && go build
```

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

## Image Attribution

- Photo by <a href="https://unsplash.com/@casparrubin?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Caspar Camille Rubin</a> on <a href="https://unsplash.com/s/photos/twitch?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
- Glitch, Copyright of Twitch.tv