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 favorite tweets that contain a specific hashtag.
Setting Up Your Project
Note - We’ll be using go modules which is an experimental feature of Go 1.11
Before we can get started, we’ll need to create a new project directory and initialize it using modules:
This should create a
go.mod file within our directory and we should be good to get started writing our twitter bot!
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.
So, once we’ve created a new application within the twitter apps dashboard, we’ll have our credentials and we’ll be ready to start coding up our app.
We never want to hard-code credentials into our application’s source code in case we accidentally commit these credentials into github or somewhere public. Instead, we’ll want to set these as environment variables on our local machine and retrieve the credentials from there.
We can export these environment variables like so on MacOS:
Note - If you are developing this on Windows, replace the
setand this should still work
Once we’ve done this, we’ll be able to pick these values up safely within our code using the
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.
Let’s start off the process by creating a really simple
main.go file within our working directory:
And, just to sanity check that everything is working, let’s try and run this by calling
Awesome, now that we’ve got our foundation sorted, let’s start building up the rest of our Bot.
The first thing we’ll need, is a way to communicate with the twitter API in such a way that it knows who we are. We can create a simple
Credentials which will store our consumer and access tokens for authentication later on.
Perfect, we now have a means of connecting to the twitter API and validating whether the credentials we have used have worked.
We’ll want to update our
main() function to create a
Credentials struct and retrieve a new client for us using our newly implemented function:
We should now be able to run this using the same
go run ./... command that we used earlier. If our credentials have been set properly, we should see our account details being printed out in the client!
Ok, so now that we’ve got a client that works, we can try sending a few test tweets out using one of the libraries’ existing functions.
Remove the final
fmt.Printf() statement in your
main() function and let’s try sending a simple tweet:
And, if everything goes to plan, we should see that a new tweet from our account has popped up on our timelines:
Ok, so we’re able to send tweets, but how do we go about searching for tweets on a given topic?
To do that we could search for tweets using this handy function:
And when we run this, we should be able to see a list of tweets that have
Golang mentioned somewhere within them!
Now, imagine we swapped this out for
Amazon or any other major brand and plugged in a sentiment analysis system. We could chart things like the general sentiment for a brand across twitter over time, which would be pretty awesome!
That’s us reached the end of our journey for this particular topic! I’m hoping this tutorial has given you a good enough foundation to go off and build really cool systems that interact with various social media accounts.
Note - If you want to keep track of when new Go articles are posted to the site, then please feel free to follow me on twitter for all the latest news: @Elliot_F.
If you enjoyed this tutorial, you might also enjoy the following tutorials: