A Twitter bot with node.js, Lambda, and S3

I created a twitter bot: https://twitter.com/dobbsbot. It tweets the Book of the SubGenius (well, the preface and first chapter, at least), line by line. It’s only a matter of time before it has more followers than I do.

There are a million ways to create a twitter bot, but wanted to see how easy it would be to do it with AWS Lambda. Turns out, it’s really, really easy.

There are two primary challenges to creating this kind of twitter bot: 1) crafting a corpus of text to tweet, and 2) determining how to make the bot run.

The first challenge was made easier by having the source text available online, but I still needed a way to break into tweet-sized chunks. Adam Dynamic’s blog post about his War and Peace twitter bot provided an excellent example of using the Python Natural Language Processing Toolkit, along with some customizations, to tokenize the source material into ~140 character size segments. Thanks, Adam! I tweaked his code, ran my text through it, did some minimal by-hand modifications, and ended up with a 534-count set of tweets. If I post one every ten hours, it should take about 220 days to complete.

The second challenge was pinning down the inner workings of the automation. Lambda is a serverless architecture and doesn’t maintain state between sessions, so I’d have to find a way to remember the next line to tweet. I thought about using a database to store each line and flag each one when posted, but that seemed too heavy-handed. Instead I decided to put the source file in S3 bucket, and interact with it from Lambda. Lambda would read the file, grab the first line, tweet it, move the line to the end of the file, and then write the entire text back to S3. The bonus here is that the bot can continue to tweet from the beginning of the book after reaching the end.

I wrote the node.js code locally to test out the interactions, and then prepped to move the code to AWS. Joel Grus wrote a great tutorial on getting started with started with node.js and Lambda, and with that I was able to get it up and running in practically no time at all. I set up a scheduled event to trigger the function every ten hours, and voila, I have a twitter bot.

As always, source code here: https://github.com/benjaminlhaas/dobbsbot. Keep in mind this is just a proof of concept that I banged out in two days, so please forgive the sloppiness and crappy error handling. Use this in production at your own risk!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.