Building a Twitch/IRC Bot in Go

Datetime:2016-08-22 22:59:32          Topic: Golang           Share

Golang makes it easy to connect to servers running IRC using the net package. Thus we’ll be building a Twitch Bot that echoes a user’s chat input.

First get an access token

Twitch doesn’t allow you to authenticate with a plain password and so you’re required to use an access token.

While it would be fun to roll out our own OAuth access token generator it’s simpler to use a third-party such as twitchapps as recommended by Twitch

Store the token you get after clicking Connect with Twitch

The code

Before proceeding you should obviously have the Go programming language installed.

First we connect to twitch’s irc server:

package main

import "net"
import "net/textproto"
import "strings"
import "bufio"

func main() {
    // connect to the twitch server
    conn, err := net.Dial("tcp", "irc.chat.twitch.tv:6667")
    if err != nil {
        panic(err)    
    }
}

Then we pass our credentials:

package main

import "net"
import "net/textproto"
import "strings"
import "bufio"

func main() {
    conn, err := net.Dial("tcp", "irc.chat.twitch.tv:6667")
    if err != nil {
        panic(err)    
    }

    // token, username, channel
    conn.Write([]byte("PASS " + "oauth:yourtoken" + "\r\n"))
    conn.Write([]byte("NICK " + "yourusername" + "\r\n"))
    conn.Write([]byte("JOIN " + "#yourchannel" + "\r\n"))
    defer conn.Close()
}

Lastly we create an endless while loop to listen/respond to chat messages:

package main

import "net"
import "net/textproto"
import "strings"
import "bufio"

func main() {
    conn, err := net.Dial("tcp", "irc.chat.twitch.tv:6667")
    if err != nil {
        panic(err)    
    }

    conn.Write([]byte("PASS " + "oauth:yourtoken" + "\r\n"))
    conn.Write([]byte("NICK " + "yourusername" + "\r\n"))
    conn.Write([]byte("JOIN " + "#yourchannel" + "\r\n"))
    defer conn.Close()

    // handles reading from the connection
    tp := textproto.NewReader(bufio.NewReader(conn))

    // listens/responds to chat messages
    for {
        msg, err := tp.ReadLine()
        if err != nil {
            panic(err)    
        }

        // split the msg by spaces 
        msgParts := strings.Split(msg, " ")

        // if the msg contains PING you're required to
        // respond with PONG else you get kicked
        if msgParts[0] == "PING" {
            conn.Write([]byte("PONG " + msgParts[1]))
            continue
        }

        // if msg contains PRIVMSG then respond 
        if msgParts[1] == "PRIVMSG" {
              // echo back the same message
              conn.Write([]byte("PRIVMSG " + msgParts[2] + " " + msgParts[3] + "\r\n"))
        }
    }
}

As you can see we split the message by spaces into parts because a typical irc message comes like this:

:[email protected]
PRIVMSG #channel :chatmessage

And we should handle it based on these parts. Most messages you’ll receive are here .

Demo and Source Code :





About List