Official website for Linux User & Developer
FOLLOW US ON:
May
6

Code your own Twitter bot

by Liam Fraser

Create your very own Twitter bot that can retweet chunks of wisdom from this very magazine

Twitter is a useful way of sharing information with the world and it’s our favourite method of giving our views quickly and conveniently. Many millions of people use the microblogging platform from their computers, mobile devices and possibly even have it on their televisions. You don’t need to keep pressing that retweet button, though. With a sprinkling of Python, you can have your Raspberry Pi do it for you. Here’s how to create your own Twitter bot…

Save your mouse button by creating an automated retweeter
Save your mouse button by creating an automated retweeter

Resources

Internet connectivity
Latest version of Raspbian
Tutorial files

Step-by-step

Step 01 Installing the required software

Log into the Raspbian system with the username Pi and the password raspberry. Get the latest package lists using the command sudo apt-get update. Then install the Python Package installer using sudo apt-get install python-pip. Once you’ve done that, run sudo pip install twython to install the Twitter library we’ll be using.

Step 02 Registering with Twitter

We need to authenticate with Twitter using OAuth. Before this, you need to go to Twitter Apps page and sign in with the account you’d like your Pi to tweet from. Click the ‘Create a new application’ button. We called our application ‘LUD Pi Bot’, gave it the same description and set the Website to the Linux User website. The Callback URL is unnecessary. You’ll have to agree with the developer rules and then click the Create button.

Step 03 Creating an access token

Go to the Settings tab and change the Access type from ‘Read only’ to ‘Read and Write’. Then click the ‘Update this Twitter application’s settings’ button. The next step is to create an access token. To do that, click the ‘Create my access token’ button. If you refresh the details page, you should have a consumer key, a consumer secret and access token, plus an access token secret. This is everything we need to authenticate with Twitter.

Step 04 Authenticating with Twitter

We’re going to create our bot as a class, where we authenticate with Twitter in the constructor. We take the tokens from the previous steps as parameters and use them to create an instance of the Twython API. We also have a variable, last_ran, which is set to the current time. This is used to check if there are new tweets later on.

Step 05 Retweeting a user

The first thing we need to do is get a list of the user’s latest tweets. We then loop through each tweet and get its creation time as a string, which is then converted to a datetime object. We then check that the tweet’s time is newer than the time the function was last called – and if so, retweet the tweet.

Step 06 The main section

The main section is straightforward. We create an instance of the bot class using our tokens, and then go into an infinite loop. In this loop, we check for any new retweets from the users we are monitoring (we could run the retweet task with different users), then update the time everything was last run, and sleep for five minutes.

  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.