Anatomy of a Twitter Bot


The past couple of weeks I've been looking back on code I wrote during and shortly after the bootcamp. It's strange because there's been a fair few 'Who wrote this?' moments, including for an early project, which was building a Twitter bot with Node.js.

The now-retired bot tweeted a picture of concrete architecture every hour, using the Unsplash API and package for random images and a package called Twit to interact with the Twitter API. The whole thing ran continuously on a Raspberry Pi. You can find the repo on GitHub.

In steps, the bot:

At the time I thought that while it was a very simple bit of code it was fairly slick. However with a couple of months hindsight that is definitely not the case.

Concrete Pics on Twitter

There are a lot of nested functions and callbacks that make it pretty hard to read what's going on. In a future refactor I'll separate these out into separate files with asynchronous functions, give them better comments(!), and have everything called by a main init function.

By taking this approach it would be easy to modularise the whole thing to add and remove functionality provided by the Twitter API, and turn them to different things. I saw a great one recently that tweets randomised Haikus, which is a form I'd like to experiment with.

There's a few other problems with redundant code and error handling, but the fun thing is that I can come back to this in my downtime (I'm employed now by the way, more on that some other time) with the benefit of more knowledge every time.

More to come on in terms of little programs like this!

More posts