Twitter Bot in 5 minutes

About

Nowadays Twitter accounts are used for multiple purposes - for personal micro-blogging or by companies sharing news and updates.
Usually there is a real person behind the account that manages it.
However in some cases there is a need for some automation when a software manages the account and tweets news or updates.
This kind of software is called a bot. And in this mini-project we will build a very simple Twitter bot using Golang.
The scope of our project will be small - it will just tweet a static text, but afterwards it can be extended to whatever you like.

Link to Wikipedia: https://en.wikipedia.org/wiki/Twitter_bot

Live coding

Here is a live coding session that shows how it can be done.

Duration: 4:05 (coding - 3:57)

Disclaimer: never use this code in production. It was created for fun.

Breakdown

Let's break down the solution and comment on some complex or interesting things.

0:04 - the prerequisite for this project is a Twitter account that you own or manage.
In this case we will use @5minsofcode account - feel free to follow it to get latest news and updates.
If you don't have one - it's very easy to set it up. It will take only a couple of minutes.

0:09 - to interact with Twitter API you need to have an additional account on developer.twitter.com.
The registration process is also very simple and it links your developer account with you Twitter account.
To interact with your account you need to create a Twitter application. Standard account has limitations on a number of applications you can create or a number of API requests that you can send. But for our case it will be more than enough.
Also application can have several stages - Development, Staging and Production. This separation is used to implement proper CI/CD processes when changes are first implemented on Development stage, then promoted to Staging and after final testing are deployed to Production environment.
Our case is very simple and we can do it only with Development stage.

0:26 - after application is created we can see that some credentials are generated:

These credentials should be stored in secure way - if anyone gets hold of them you account becomes compromised.

0:27 - authentication configuration step.
We need to configure how clients (i.e. our bot) will authenticate itself. There are several authentication protocol options available:

For simplicity I decided to go with OAuth 1.0a - it has more convenient authentication process and should suit our needs.
For OAuth 1.0a we need to configure what our clients can do:

1:00 - starting the coding process.
As mentioned before we will use Golang. And as first step we are initializing the project using go mod init twitter-bot.
Technically - Twitter offers HTTP-based API and we can implement all operations ourself using Golang's http package. Or alternatively we can use some existing modules that already solve these problems for us:

We are installing both of thes modules:
go get github.com/dghubble/go-twitter/twitter && go get github.com/dghubble/oauth1

1:36 - all modules are installed and we can finally write some code.

1:50 - define 4 variables that will be used for authentication:

For now they are just placeholders - we'll fill them in a bit later by copying keys from Twitter developer account.

1:58 - OAuth 1 authentication process assumes that we have to create config and token.
config is based on consumerKey and consumerSecret. Similarly token needs accessToken and accessTokenSecret.
Refer to https://developer.twitter.com/en/docs/authentication/oauth-1-0a for more details.

2:10 - next we need to initialize a Twitter API client.
For this purpose we need to have a httpClient that is based on config and token that can be later used to cread a client.
In the end our client will be able to call Twitter API using the provided credentials.

2:20 - account verification.
This is an optional step to check that our connection works fine.
We want to check that all provided credentials are correct and we are connecting to the correct account.
For that purpose we can use user, _, err := client.Accounts.VerifyCredentials(&twitter.AccountVerifyParams{}).
It returns user struct containing information about our account. We are interested in user.ScreenName and user.Name.

2:44 - getting the credentials from developer.twitter.com.
We can regenerate Consumer Keys and Access Token and Secret. Note that these values are only shown once and you cannot reveal them again - make sure you copy them.
Copy the secrets into our Golang code.
Important: never store credentials in plain-text in your code. There are other ways how to manage secrets properly.
My preferred way here would be to create .env file (that is never commited/pushed to Git) and store the credentials there as key-value pairs.
Read more about it in the following sites:

Imporant: don't try to use the keys shown in the video - they are already revoked and will not work!

3:15 - first run.
The expected output in console is our Twitter account name.

3:24 - now it's time to tweet someting.
As this is a very simple example we will tweet a static text like New project is coming soon - stay tuned!.
To do it we can just use client.Statuses.Update("New project is coming soon - stay tuned!", nil).
Very simple!

3:48 - check results in our Twitter account.
Go to browser and refresh the Twitter page. You can see that new tweet appeared.

This concludes our mini project where we briefly touched Twitter API. There are few things that can be improved like:

Resources

Sources: https://github.com/5minute/examples/tree/main/twitter-bot