WDI week 10: Airbnb, Node.js and Thanksgiving


Update: hugbot has since been fixed so it now sends hugs when you mention @hugsorpugs on Twitter.

We started this week with our final field trip of WDI: Airbnb. I really enjoyed the tour and Q&A session with their engineers. Their office is amazing, and I love their product and general style. It sounds like an interesting place technically, too.

Given that this was a super short week due to Thanksgiving, we were tasked with building either a small app, or expanding on something we'd already built. But I had something a bit different in mind that I'd been wanting to make for a while: a Twitter bot.

I thought this might be a good chance to do it for a couple of reasons. Firstly, because I knew the simplest implementation of it could be *really* simple. Once I had the basic idea working, I could then expand on it, gradually adding more features to see how far I could get. Secondly, it was a deliberate choice to go for something that wouldn't need a front end. With no visual design or multi-faceted app considerations to worry about, I could focus purely on the code.

I also wanted to break out of my Railsy comfort zone and learn something new by building it in a technology I hadn't used before. After a bit of research, I picked Node.js. I'd been curious about it for a while, with the added bonus of giving me the chance to write more JavaScript (yay!).

The basic idea for my bot was to search for anyone Tweeting phrases like 'I want a hug', and send them hugs in response. So I did some reading about the basics of Node, figured out the which parts of Twitter API I wanted to use, set up my API keys, and I was pretty much ready to go.

Node.js doesn't do much at all on its own (which makes you realize just how much magic Rails does for you behind the scenes). Node can do a lot of things, but you have to implement them all yourself. Fortunately, it also has an active community of people who have already written the code to do most of the regular things you might want to do already. For example, I found ntwitter, a Twitter client complete with the methods I would need to use the Twitter Streaming API.

After I'd got the Twitter authorization stuff working, it was actually pretty simple to get the right data from the API. The Streaming API doesn't allow you to search for phrases with the kind of specificity that I was after, so I had to add a second layer of filtering using regex, which was another new thing for me to figure out.

Once I had the bot successfully Tweeting hugs, I decided to implement a small chance of a user receiving a picture of a pug, instead of the standard hug Tweet (hence the bot's name, @hugsorpugs). Ideally it would've pulled a random pug picture from a Google image search, but I didn't have time to do this, so ended up just hard-coding in a few URLS for the pug pictures.

After this, the main problem was playing around with how frequently my bot Tweeted to ensure this fell within Twitter's rate limits, and to try to avoid its account being blocked for spamming.

Unfortunately, what the bot does is actually in breach of Twitter's rules, so after being suspended a couple of times (even after experimenting with minimal Tweet rates), I decided not to try to host it permanently anywhere. I might make a modified version that requires people to ask the bot directly for hugs before it'll reply, but for now, I'm just really pleased I got it to work.

It's also given me a whole range of of ideas for other (more legitimate) Twitter bots, and could easily be adapted to work for these.

Either way, it was an amazingly fun little project to work on, and I was especially happy that I managed to do it without asking for any help (the practice at being more self-sufficient is obviously paying off!). I'm planning to write another post where I'll go into the code and talk about exactly how I did it when I get some more time.

For now, you can visit (sadly inactive) hugbot at https://twitter.com/hugsorpugs or check out the code at https://github.com/negomi/hugbot.