Twitterbots - Overview
Updated January 2021
Content warning: page contains a potty-mouthed feline
Lets talk about Twitter...
No don't go, please stay on my site, please
Does anyone use it any more?What do you with your account when you've got bored of it? Archive it? Delete it?You could always automate it with the help of a Raspberry Pi?
The Raspberry Pi 3 running these accounts for the last few years failed recently and took out the SD card, so the accounts mentioned in the tutorial have now been deactivated due to my
lack of interest laziness
At the moment, I have four automated Twitter accounts, they are The Ford Fiesta, Vader the Cat, Ian Hill, and Arse Clock, all running from one headless Raspberry Pi 3B (which is buried in a drawer near my router with another Pi 3B, a 3B+, a dedicated NAS, and a gigabit switch) and all four of them are completely automated, including random tweets, new follower messages, and replying to mentions while saving everything it does to a log file which can be viewed via it's web page, the web page allows some basic control of the engine including sending tweets on demand
Pages in this series...
Page 1: Overview
Page 2: Tweeting randomly
Page 3: Tweeting pictures
Page 4: Tweeting new followers
Page 5: Tweeting replies to mentions
Page 6: Scanning for trending hashtags
Page 7: Automating everything
Once in a while either myself or "Vader" will get somebody complaining this is a fake automated account, either verbally, or via Twitter itself, and I'm posting the tweets myself. It makes no difference that Vader posts once, up to every three hours (or 10800 seconds) all day, every day, and the fact new followers are scanned for once every three hours, all day, every day, finally the mentions are scanned once an hour, on the hour, all day, every day.None of this make any difference whatsoever, so what better way to prove it is a real fully automated account, than to explain how it works and provide some of the source code...
My Twitterbot history
This is where it all started, as one of those random ideas I had during the #projectphone experiment
Yes, it did annoy people, a lot 🤣
Project Phone controlled Twitter by way of text messages, Twitter knew the phone number associated with the account, and post tweets on the timeline when texts from that number were received, and if anyone replied, or followed the account, a text message would be sent to the phone, which the Project Phone software would read, analyse, and respond to, all controlled by a scripting language called Mortscript, which controlled other apps installed on the system, it was a bit of a mess, but it did work
These days you need to create an app at Twitter, you will need one app for each account you want to automate, however it's possible to control them all from one script on the Pi, if you can get your head round it all!
We'll take a look at Vader's account, as it's a good place to start, has the most features, and "coincidentally" the most popular...
Caution! Vader is a real cat, AKA the world's angriest cat, and he tweets his innermost thoughts, usually about taking over the world, and the destruction of the human race, this means his language can be appalling sometimes
Today, my Twitterbots are a lot more sophisticated, Vader's Twitterbot has a total bank of 448 tweets, and depending on factors like the time of day, or day of the year etc, some of the tweets are pulled into another array where one gets chosen at random and tweeted, this of course can include variables, so Vader can tweet what the time is, the current day, pictures, or ask questions about whatever hashtag is trending at the time
The Python script can also scan for the top 20 trending hashtags and use them in it's tweets
Additionally there are scripts that check for new followers and any mentions that may have happened recently
Also, the script can tweet the occasional photo...
And some more random tweets...
You may notice the text VaderTheCat at the bottom of some of the tweets, this is the name of the Twitter app I wrote to control the account, if the tweet was posted by the official Twitter app, it would say Twitter for Android/iOS etc. This shows it's a third party app controlling the account, whether it's automated or not
Here are some screen shots of the web portal running on the Pi
You can see that it gives information about the Pi itself, and you can't do anything unless you log in. Once you've logged in, you can access each Twitterbot's control panel, perform a backup or reboot the server etc, and it shows if a script is running in the background
The control panel for each Twitterbot allows you to post a tweet or a picture on demand, see when the last tweet was sent, or when next tweet will be posted. You can also access the most recently saved followers or mentions lists, access the log files. Finally a recent addition was the ability to upload images to be tweeted to the system
If this sounds like something you want to do, then read on...
Create an app at Twitter
Firstly, you need to apply for a developer account at Twitter, so go to twitter on a PC, and log in to your account, then go to the Twitter Developers page and apply for a developer account (I never had to do this when setting up my apps, so it's new to me too)
Selecting Hobbyist and Making a bot seems the most appropriate, you must give a valid email and phone number (which must be verified) when setting up your account, and you can use the same number for multiple appsYou will need to describe exactly what you will use the app for, so be as honest as you can. Once complete you will need to confirm your email address, and wait for their decision...
Yes, it can take several days to receive the decision...
Once you get your account set up, go back to the Twitter Developers page and click on the Apps button in the top right, then Create an app
Fill out the form as best as you can, you will need to describe your intentions with the app, so be honest as you can, there is a minimum character count, so put as much as you can.
Don't enable sign in with Twitter
You'll need a website URL, I just put ianwilliamhill.co.uk
Once done, click Create
Read the terms and conditions reminder
Go to the permissions tab and set the access permission to read, write, and direct messages, then go back to the keys and tokens tab and regenerate the consumer API keys, then create the access token and access token secret keys.
Copy all four of these keys down somewhere now, if you navigate away from this page, Twitter may not let you access them again, and you'll have to regenerate them
That's because a recently added new and annoying feature is that you need to regenerate the keys if you make any changes to the app, and you only get once chance to view/save the keys, then you'll need to regenerate the keys again, so make sure everything is set up before you generate the keys, and you save them somewhere before closing the tab
Once you've gone through all the above to get the four access keys, replace the abcde... with your unique keys in the code below, once this is complete, you are finished with the Twitter Developer page, and it can be closed
Once that's all set up and you've logged in to the Pi, either by SSH or on the Pi itself type...
pi@raspberrypi:/$ sudo su
root@raspberrypi:/# apt-get update && apt-get upgrade -y
root@raspberrypi:/# apt-get install python-pip
root@raspberrypi:/# pip install twython
root@raspberrypi:/# apt-get install apache2 php