I have been curious about reddit and twitter bots for a while. If you aren’t sure what I mean, here’s a link to the xkcd transcriber bot just as an example.
People write all types of interesting bots, from Wikipedia fetchers, to user statistics, to comedic relief bots.I finally decided to dive in and give it a \
shot on my own. A bit of history; I began looking into this back in 2016 as a fun project. While I’m looking to change some things, This post will act a\
s a sort of a retrospective on what I have learned and the different choices I made while building this. If you want to take a look at my code, take a look at\
the GitHub page here (old link was gone). To do what? I wanted to write a bot which acts as both an image mirror/archiver/aggregator.\nI wanted ne\
w posts to be sent out to twitter. Essentially I wanted to use multiple APIs and tie them together into something new.
I’m usually a C++ programmer, but I had worked with Ruby before and noticed that there were some gems on GitHub that allowed one to interact with bot\ h the Reddit, Twitter, and Imgur APIs. After attempting to put together a couple small prototypes and swearing at my computer. I gave up on using Ruby to writ\ e my bot. Why? At the time the libraries I was attempting to use were old, buggy, and ill maintained. There were officially supported libraries for all these \ services written in Python. While I had not ever written any code in Python, this seemed like it would be easier than dealing with terrible libraries. For any\ one getting started, I would highly recommend using Python for your bot simply for the availability of high quality libraries. The ones I started off with wer\ e: PRAW, TwitterAPI, and Imgur
Update: It now appears that the Imgur python library is deprecated. I will need to find another way to access it.
For these services you need to register an application in order to get full access to their API. This is free. Some services will allow anonymous API acces\ s but typically the number of requests you are allowed to make is restricted. This part can be a little confusing so I will show you how to get registered. I \ will say right off the bat that it would be a good idea to set up a new email (or email alias) for all the services you will be signing up for.
Go ahead and create a new account for your bot. Navigate to preferences -> apps. Choose that you want to create a new app. For the app type, I would rec\ ommend you choose script. You will be provided with an app key, and an app secret. You will need both of those for your app to talk to Reddit. Here is a link \ to the documentation
You will need to provide a phone number to sign up for a Twitter account. I would recommend using a Google voice account. Go ahead and create your new twit\ teraccount and navigate in your browser to https://apps.twitter.com/. From there you can give your app a name, descipt\ ion and all that fun stuff. You will end up with a consumer key and secret, as well as an access token key and secret (4 different big long string). Take note\ of these as you will need them to go in your app. Read more from their documentation here
Once again, create a new account for your bot (yawn, I think you’re getting the idea). Follow the API docs here to get up and running.
While there is a lot of setup to do before you get started writing your bot. You only have to do it once. While I recommended python, I’m sure there \ are plenty of other languages out there which would serve you well depending on the particular task you need to accomplish. Most APIs these days transport dat\ a in the JSON format. Back in ye olden days we had XML responses. I’d recommend reading up on it a litt\ le bit. It’s a pretty easy to read format. In the next section I will go over some of the specific code I used in my bot.