Building a twitter bot using node, feedparser and simple-twitter (part 1)
Information Flaneur 2023-05-21
Summary:
Last weekend I should have been doing a lot of things. Washing. Working on my VALA conference paper. Reading the many books on my TBR pile. Instead, I made a Twitter bot. I’m still astonished that it actually worked. Here I’ll explain how I did it and by extension how you can make your own, if you’d like to.
A little background - why Node?
A little bit of background first - because hey it’s my blog so you have to indulge me. If you just want to get stuck into writing the bot, skip down to ‘Getting Started’, below.
’Learn to code’ has been on my New Year Resolutions list two years running. Until now, the last program I wrote was in BASIC on a BBC Microcomputer when I was twelve years old. That was quite a while ago. After completely failing to even get started in 2014, I was determined to actually learn something this year. I asked Twitter that annoying question - “Which language should I learn?” and got some patient and thoughtful answers from people I respect - Andromeda Yelton, Coral Sheldon-Hess, Cecily Walker, and Chris Cormack among many others. In among all the back and forth, there were two clear instructions that most people seemed to agree on:
- choose a project you want to work on, and learn whatever language it’s written in.
- learn JavaScript second.
I faffed around for a few months, before having to break one of those rules in order to follow the other one. After many conversations about library software and patron privacy, I decided to try building a very simple model for an encrypted library circulation system using Mylar. Mylar is built on Meteor, which is a framework for building web applications based on Node. The whole point of Node is that it allows JavaScript on the server. This is pretty useful, because it allows you to code in all JavaScript all the time.
I already had some exposure to Node, because Ghost, the open source software I use for this blog, is built on Node. So I decided to try building my secure LMS prototype using Meteor, and learn some JavaScript and Node along the way. Turns out Rule 1 was absolutely correct - I’ve learned a bunch and stuck at it, because I had an actual project to work on. I had to break Rule 2, but I feel ok about that.
Why a Twitter bot?
I’ve been wanting to build a Twitter bot for a while. Watching what Tim Sherrat has built over the years has been fascinating. I was particularly taken by his subversive Operation Random Words, which appeared shortly after the Australian Government announced Operation Bring Them Home, following on from their wildly successful gulag-building program Operation Sovereign Borders. If you’re not familiar with a Twitter bot, the basic idea is that they are a Twitter accounts that are controlled by software rather than humans. Lots of them are spam bots that randomly Favourite or Follow, some are political statements like Operation Random Words, and others are just stupid fun, like Robot J. McCarthy, which retweets tweets containing words like ‘commie’ or ‘socialist’, with a McCarthyesque comment.
The problem with Tim’s bots is that they are written in Python. This was actually one of the most popular suggestions when I asked ‘Which language?’, but I’ve made my bed and now I need to lie in it, so I looked for a solution in Node and JavaScript. The attraction of a Twitter bot was that it seemed like a reasonably sized challenge (not to big, but not too simple), and I’m familiar enough with them that I understand the basic principles in terms of Twitter behaviour. There are, after all, only so many things you can do on Twitter. In all, it took me most of a weekend to build, but that’s mostly because I was learning as I was doing it (which of course was most of the point).
The final trigger was actually the online conversation that happened at the end of June regarding the life and death of blogging, and in particular a desire from several blogging librarians to see more Australian librarians blogging. Alisa Howlett blogged about her proposed solution to tackle the problem from the supply end - (re)starting a group blog. She recently followed this up with a call for volunteers. I was thinking about how to assist by making library blogs more visible in the day to day conversation. The project I came up with was a version of Code4Lib’s InfoPeep, but for Australian GLAM bloggers -