Writing A Twitter Bot in Golang

In this tutorial I’m going to be demonstrating how to build a twitter bot using go-twitter, a popular Go client library for the Twitter API.

I’ll be demonstrating how you can build a go based twitter bot that will be able to do such things as automatically reply to tweets and favourite tweets that contain a specific hashtag.

Connecting to Twitter

Just like with the Python version of this tutorial, you’ll have to create an app in twitter’s app control panel. Once you’ve created a new application, it should present you with all the secret tokens and keys that you need in order to proceed.

Writing our Basic Go Twitter Bot

Once you’ve got all the access tokens and secret tokens ready, it’s time to start implementing our Bot. Create a new file called twitter-bot.go and add the following:

package main

import (

func configure() {
    // Pass in your consumer key (API Key) and your Consumer Secret (API Secret) 
    config := oauth1.NewConfig("consumer-key", "consumer-secret")
    // Pass in your Access Token and your Access Token Secret
    token := oauth1.NewToken("access-token", "access-token-secret")
    httpClient := config.Client(oauth1.NoContext, token)
    client := twitter.NewClient(httpClient)
    demux := twitter.NewSwitchDemux()
    demux.Tweet = func(tweet *twitter.Tweet){
    demux.DM = func(dm *twitter.DirectMessage){
    fmt.Println("Starting Stream...")
    // FILTER
	filterParams := &twitter.StreamFilterParams{
		Track:         []string{"cat"},
		StallWarnings: twitter.Bool(true),
	stream, err := client.Streams.Filter(filterParams)
	if err != nil {
    // Receive messages until stopped or stream quits
	go demux.HandleChan(stream.Messages)

	// Wait for SIGINT and SIGTERM (HIT CTRL-C)
	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)

	fmt.Println("Stopping Stream...")

func main() {
    fmt.Println("Go-Twitter Bot v0.01")