Next Thing CHiP as a Twitter Bot
There was a post that came across on Medium recently, How to Make a Twitter Bot in Under an Hour. It’s pretty straight forward, though it seems to be pretty geared towards non “techie” types, mostly because it’s geared towards people making the bot on a Mac and it uses something called Heroku to run the bot. Heroku seems alright, except that this sort of feels like an abuse of their free tier, and it’s not free for any real projects.
I already have a bunch of IOT stuff floating around that’s ideal for running periodic services. I also have a VPS is I really wanted something dedicated. So I adapted the article for use in a standard Linux environment. I used one of my CHiPs but this should work on a Raspberry Pi, an Ubuntu box, a VPS, or pretty much anything running Linux.
The first part of the article is needed, set up a new Twitter account, or use one you already have if you have extras. Go to apps.twitter.com, create an app and keys, keep it handy.
Install git and python and python’s twitter extension.
sudo apt-get install git
sudo apt-get install python-twitter
This should set up everything we’ll need later. Once it’s done, close the repository.
git clone https://github.com/tommeagher/heroku_ebooks.git
This should download the repository and it’s files. Next it’s time to set up the configuration files.
cd heroku_ebooks
cp local_settings_example.py local_settings.py
pico local_settings.py
This should open up an editor with the settings file open. It’s pretty straight forwards, you’ll need to copy and paste the keys from Twitter into the file, there are 4 of them total, make sure you don’t leave any extra spaces inside the single quotes. You’ll also need to add one or more accounts for the bot to model itself after. You’ll also need to change DEBUG = TRUE to DEBUG = FALSE as well as adding your bot’s username to the TWEET_ACCOUNT=” entry at the bottom.
Once that is all done do a Control+O to write out the file and Control+X to exit. Now it’s time to test out the bot with the following…
python ebooks.py
It may pause for a second while it does it’s magic. If you get the message ” No, sorry, not this time.” it means the bot decided not to tweet, just run the command again until it tweets, since we’re testing it at the moment. If it worked, it should print a tweet to the command line and the tweet should show up in the bot’s timeline. If you get some errors, you may need to do some searching and troubleshooting, and double check the settings file.
Next we need to automate the Twitter Bot Tweets. This is done using Linux’s built in cron. But first we need to make our script executable.
chmod 755 ebooks.py
Next, enter the following….
sudo crontab -e
Then select the default option, which should be nano. This will open the cron scheduler file. You’ll want to schedule the bot to run according to whatever schedule you want. Follow the columns above as a guide. For example:
# m h dom mon dow command
*/15 * * * * python /home/chip/heroku_ebooks/ebooks.py
m = minutes = */15 = every 15 minutes of an hour (0, 15, 30, 45)
h = hour = * (every hour)
dom = day of month = * = every day and so on. The command to run, in this case, is “python /home/chip/heroku_ebooks/ebooks.py”. If you’re running this on a Raspberry Pi, or your own server, you will need to change “chip” to be the username who’s directory has the files. Or, if you want to put the files elsewhere, it just needs to b e the path to the files. For example, on a Raspberry Pi, it would be “python /home/pi/heroku_ebooks/ebooks.py”.
If everything works out, the bot should tweet on schedule as long as the CHIP is powered on and connected. Remember, by default the bot only tweets 1/8th of the time when the script is run (this can be adjusted in the settings file), so you may not see it tweet immediately.
This is also a pretty low overhead operation, you could conceivably run several Twitter Bots on one small IOT device, with a staggered schedule even. Simply copy the heruko_ebooks directory to a new directory, change the keys and account names and set up a new cron job pointing to the new directory.