How to build a nonsensical Twitterbot on the Raspberry Pi


Want to create a nonsensical Twitterbot based on existing Twitter accounts and run the bot from your Raspberry Pi? Then this guide is for you.

This guide shows you how to set-up a Twitterbot in a similar vein to Horse_ebooks.

The bot is based on Heroku_ebooks and its content is fuelled by the tweets of another account or accounts.

Want to see a working bot built using this method? Check out: @ebookek.

This guide assumes that you have some familiarity with using a Raspberry Pi and using Terminal.

What you’ll need

Before we get started, you’re going to need a few things:

  • A Raspberry Pi running Raspbian Jessie OS
  • OR a Raspberry Pi with an installation of Python 2.7.9 that is set as the default Python
  • A user account with sudo access
  • Connection between your Raspberry Pi and the internet via Ethernet or WiFi
  • Keyboard, monitor and mouse connected to your Raspberry Pi
  • A mobile phone with a number that you can register to the Twitterbot’s account
  • A PC to do some configuration stuff on
  • Twitter account/s to pull Tweets from e.g. your own Twitter account – this is not the bot’s account

NOTE: while this guide does refer to using a text editor to configure files, you are welcome to use your preferred Python IDLE (Integrated Development and Learning Environment).

Step 1 – Register a Twitter account for the bot

Using the PC – because Twitter will insist on using the mobile version when you access via Raspbian’s default browser – register a new Twitter account for the bot. During registration, sign-up using an email address.

The email address cannot already be registered with another Twitter account.

TIP

If you have an existing Gmail account, you can set-up an “alias” for the bot and use that same account for it, i.e. <your normal email address before the domain>+bot@gmail.com. The alias doesn’t have to be “bot” but it will make it memorable.

Once you have an account, go through Twitter’s steps to verify it via email, then add a phone number to the account and follow Twitter’s verification steps for it. Twitter will likely prompt you to add a phone number to the account after you login for the first time.

You can also add a phone number to the account from https://twitter.com/settings/security when logged in as the bot. Choose to “verify login requests” and follow the onscreen instructions.

TIP

Don’t forget to give the bot some personality – pick an avatar, give it a description, and set a banner image.

Step 2 – Create a Twitter Developer account and app

Still on your PC, go to https://apps.twitter.com/ and choose to “Create New App” while logged in as the bot account. (If you haven’t managed to verify the mobile number for the bot’s Twitter account you will be prompted again now.)

setting-up-rapberry-pi-twitter-bot-1

The name can be the same as your bot’s Twitter handle. You can describe how the bot is in the vein of Horse_ebooks in the “Description”. For website, either put a placeholder as suggested, or link back to your own personal Twitter account.

You can leave “Callback URL” blank.

Agree to the “Developer Agreement” (assuming you do, of course) and press the “Create your Twitter application” button.

TIP

Remember that while it is the aim of this guide to produce a working Twitterbot, your bot will have to abide by Twitter’s rules for excessive usage and spamming. Details on rate limits are featured in the Developer Agreement.

Once you get onto the next screen, go to your app and then “Permissions”. Set the app’s permissions to “Read and Write”.

setting-up-rapberry-pi-twitter-bot-2

Then go to “Keys and Access Tokens” and choose to generate “Consumer Key and Secret”.

Step 3 – See if your Raspberry Pi has PyPip

Switching over to your Raspberry Pi, open up Terminal. You need to check if you have PyPip (Python Index Package) installed on your Pi. The bot runs in Python 2 so you would type into Terminal:

sudo apt-get install python-pip

If you already have it then messages in Terminal will state that it’s installed or update it to the latest version. If you don’t have it then the package will be downloaded and installed.

Step 4 – Install python-twitter on Raspberry Pi

The bot uses python-twitter to help it communicate with Twitter. Using Terminal type in:

sudo pip install python-twitter

And press enter.

This will download and install python-twitter.

TIP

We’re using sudo to do a lot of the installation as the Pi’s regular access won’t allow you to.

Step 5 – Get python-twitter code onto Raspberry Pi

You need to clone python-twitter’s GitHub repository in order to have everything it needs. In Terminal, type in:

sudo git clone git://github.com/bear/python-twitter.git

And press enter.

Once it’s finished cloning the repository, type in and press enter to change directory:

cd python-twitter

From here, type in and press enter:

sudo pip install -r requirements.txt

Once all of that is done configuring, you should now have everything you need to start setting up heroku_ebooks.

Step 6 – Download Heroku_ebooks

On your Pi, go to Heroku_ebooks and choose to download the bot’s zip file.

Once you have the zip, unzip them into a directory under your user account, like:

/home/pi/awesome_bot-master

Step 7 – Configuring Heroku_ebooks – Twitter keys and tokens

You’ll need both your PC and your Raspberry Pi for this step. Get your Twitter app for the bot open on Twitter via your PC so that you can see your Consumer Key and Consumer Secret.

setting-up-rapberry-pi-twitter-bot-3

TIP

My access tokens are blacked out in these images, because you should never share these with anyone.

Copy your Consumer Key, Consumer Secret, Access Token Key and Access Token Secret into a .txt file and put that .txt file onto your Raspberry Pi (or be prepared to manually type them into the main configuration file during the next step).

Step 8 – Configuring Heroku_ebooks – local_settings_example.py

In the directory on your Pi that you unzipped the Heroku_ebooks files to, open up local_settings_example.py in a text editor. Copy all of the contents in that file and paste them into a new text file.

Save this new document as local_settings.py. Close the example file.

Fill in the Consumer Key, Consumer Secret, Access Token Key and Access Token Secret from your Twitter app, keeping the single quotation marks.

Then fill in the rest of the information asked for, following the instructions given beside them.

TIP

You can use more than one Twitter account as a source for your bot, just follow the formatting instructions given in the local_settings.py file.

DEBUG and STATIC_TEST should be set to False.

ODDS will affect the chance of a Tweet being made by your bot. If you always want it to Tweet, (so long as the Tweet assembled isn’t similar to an existing one), set it to 1.

ORDER only works on 2 or 3.

heroku-ebooks-setup-file-example-v1

The example file, as shown on Heroku_ebooks GitHub page.

Save local_settings.py once you’re done making your changes. Then exit it.

Step 9 – Configuring Heroku_ebooks – changes to markov.py

At this point, your bot will most likely run without many problems, however there are still a few more tweaks to make.

If you use the bot as it is now it will not add a space between the penultimate and last words of a Tweet. To fix this, we’re going to use a fix put forward by Robbie Mitchell.

Open up markov.py in a text editor. Use Ctrl + F to find sentence += res[-2] + res[-1]. Once you have that line, edit it so that it reads:

sentence += res[-2] + " " + res[-1] (Yes, there is a space between those two quotation marks.)

Then save the file and exit it.

Step 10 – Configuring Heroku_ebooks – optional changes to ebooks.py

It’s preferable to have a large pool of Tweets the bot has to draw from. To do this you can include replies. Open ebooks.py in a text editor. Use Ctrl + F to find exclude_replies. Right now it says True beside it.

Change that to False, save the file and exit it.

Step 11 – Running your bot for the first time

Okay, you should now have everything sorted so that the bot will run.

Open Terminal and change your directory so that you’re in the directory where you saved your bot’s files. Then type in and press enter:

python ebooks.py

And your bot should run.

TIP

If your bot doesn’t run and returns a message saying something like “3 No, sorry not this time” then that’s the odds for it Tweeting at work. Go into your local_settings.py and set ODDS to 1 if you want the bot to run each time it’s activated.

Step 12 – Automating your Twitterbot – the shell script

At the end of the day, you don’t want to be manually telling your bot to Tweet from your Raspberry Pi. You want it to run at set intervals, while it’s happily sat in a corner.

To do this we will need to use a shell script and crontab. What shell script do you need?

Open a text editor (an actual text editor, not IDLE) on your Pi and type in the following:

#!/bin/bash
python ~/[name of the directory with your bot in it, without these square brackets]/ebooks.py

Save the file in the same directory as your bot files with a memorable name and with .sh after it. For example:

run_bot_run.sh

But this is isn’t enough: we need to make it executable.

Open Terminal, change directory to where your bot files are. And then type in and press enter:

sudo chmod +x [the name of your shell script file, without these brackets].sh

The shell script is now executable.

Step 13 – Automating your Twitterbot – crontab

Still in Terminal type in:

crontab -e

and press enter.

If this is your first time using crontab, select the nano editor. Once in nano, shift your cursor all the way down to after # instructions and leave a free line between them and where you will be working.

crontab explains how its timing intervals work, but crontab.guru is a great way of checking if the time you entered is what you think it is.

Say you want the bot to run half-past every hour, in crontab type in:

30 */1 * * * ~/[name of the directory with your bot in it]/[the name of your shell script file].sh

(Minus the square brackets.)

Hit Enter after typing it in to leave a space and then Ctrl + X to quit, press Y to save and then press Enter again to exit crontab.

Step 14 – Testing automation and running it several times a day

Still not sure if it’s working?

You can have more than one set of instructions saved in crontab.

If you want to be sure that your automated bot’s working, without it tweeting loads, then try setting a time interval after your current time that’s 10 minutes ahead of you. For example, if your local time was 10:15AM, set the crontab to:

25 */1 * * * ~/[name of the directory with your bot in it]/[the name of your shell script file].sh

Use crontab.guru to help you figure out more times. You can have the bot run more than once an hour by setting different intervals in crontab on different lines:

15 */1 * * * ~/[name of the directory with your bot in it]/[the name of your shell script file].sh

30 */1 * * * ~/[name of the directory with your bot in it]/[the name of your shell script file].sh

45 */1 * * * ~/[name of the directory with your bot in it]/[the name of your shell script file].sh

It’s alive!

Well done. If you’ve gotten this far you’ll probably feel like having a good stiff drink, as I did once I reached this stage. There are a lot of aspects that can be altered in the bot’s commands if you’re more familiar with Python and python-twitter.

TIP

Always keep a back-up of your files before you go tinkering too much.

Acknowledgements

I originally got the bot working by partially following the instructions given by Lauren Orsini in this guide posted on Read Write for setting up Heroku_ebooks.

Thank you to Dave Griffiths of foam, my Nerds Assemble co-host Paul Wood, Tunnocks Teasnake and Robin Duckett of Diodes Ltd for helping me to get the original Heroku_ebooks bot working on a Raspberry Pi rather than in Heroku, plus figuring out crontab and shell scripts.

Read about how I built my Twitterbot in our Adventures in Tech series.

Raspberry-Pi-in-case-with-keyboard-mouse-v2


More posts you might like…

Radix’s Adventures in Tech #3: Tanglebots, assemble!

In our ongoing quest to get under the hood of more technology, Fiona tries her hand at coding, electronics and robotics.

Create B2B tech marketing content that really works

Get regular advice and insights from our team of specialist B2B tech writers and account managers, direct to your inbox.