RSS to Twitter Bot Using Ruby
July 2023 Edit: The following bot does not currently work due to Twitter’s deprecation of the v1.1 API. The gems used in this script do not support v2, and most developers of tools for interacting with Twitter’s API seemed to have moved on. Pinball Map does not have plans to try and get our Twitter bots working again.
Additionally, due to recent events, in particular the dissolution of the Trust & Safety team, Twitter currently has very sketchy content and Pinball Map is no longer actively using it.
Why, it’s another post about Twitterbots! Here’s our latest and our final RSS to Twitter Bot. This is how we tweet “new machine added to…” tweets without having to manually click tweet.
Previously on this blog: New Method to Auto-Tweet Map Updates Reviews of various Twitter feed services
Present day: Now that we’re all caught up, here’s a brief explanation of what happened since we used that python RSS to Twitter Bot mentioned in the “New Method” post. Actually, truth be told - what did happen? That very complicated-looking python script would run periodically via a cron and “check for new items” in the RSS feed. But then one time we made a slight update to the formatting of the text in the RSS item (think we removed an unnecessary double space between two words), and the dumb bot thought that “every item in the feed was new” and then it tweeted like 100 things at once (from four diff accounts of ours). This resulted, as you might expect, in a loss of followers, and a shadow ban by Twitter HQ, and a some other dramatic things.
So we decided to not use other people’s complex python madness, and instead write our own elegant ruby script. Here it is (click the .rb there). And the good thing about this is that it doesn’t attempt to identify “new items” by comparing the exact text within a giant list of old items. No, it just looks at the timestamps of items in the RSS feed, and asks, “Was this RSS item created in the last 10 minutes? If so, tweet!” And then we have it run every 10 minutes. So, it’s nicely contained - it can never freak out and re-tweet 100 things.
Here’s how you can use this yourself (for example, if you’d like to create a pinball map twitterbot that tweets out particular region’s updates):
We run this on a Raspberry Pi, since it’s good to have an “always on” computer.
Install Ruby and more
I always use rvm to install ruby (so, the first link below). But there are other options: rbenv, snap. Whatever suits you.
This bot uses the chatterbot gem. Install it with
gem install chatterbot (on the pi). Documentation is here, but you won’t need much of that.
Grab the .rb and .yml files from the github repo and put them on the pi. I use scp to do that. Like
scp twitterbot.rb email@example.com:myBots
cron is basically a scheduler. Example for running it every 10 minutes:
*/10 * * * * /bin/bash -l -c '/home/pi/Documents/Bots/pbm-twitterbot/twitterbot.rb'
Make sure that your .rb file has permission to be executed from cron. This works for us:
chmod u=rwx,g=r,o=r twitterbot.rb. And update the .rb script to point to the RSS feed you want to use.
In the folder with the .rb file, type
gem install to install the project’s required gems (actually, this might not be needed in this case…?).
You’ll need a Twitter Developer account
- Start a twitter account.
- Use a gmail/yahoo/hotmail account or they’ll mysteriously insta-ban you because their spam sniffing is a total pile of crap.
- Go to dev.twitter.com and apply for an account and create an “app” and get your credentials. You may get banned again at this point.
- Put them in the .yml file like
:consumer_key: blah :consumer_secret: bleh :access_token: bloh :access_token_secret: bleh
Something not clear, or not working? Let us know!