How to make a Twitter bot

by Darius Kazemi on September 30, 2013

in bots,programming

Probably every other day some very well-intentioned person asks me: “Can you teach me how to make a Twitter bot?” If that person is a programmer, no big deal, I show them some code and they’re happy. But sometimes this is followed up with, “I don’t really know how to code.”

The thing is, there are a ton of ways to make a Twitter bot. Sometimes I point them to my article about how I made @metaphorminute. I could also point them to the code and annotated source for @twoheadlines. All of those resources assume some level of programming expertise — that the reader is comfortable with the command line, and installing language runtimes and package managers and all that kind of thing.

Then there’s Tully Hansen’s article about how to make bots without learning to code. The tools listed there are really great for simple bots that RT things, or that consist of queued-up, pre-written content.

But I get the sense that when people ask me if I can teach them, it’s because they want to make bots like the ones I make. And there are really two pieces to the process of making a Twitter bot: the Twitter piece, and the bot piece.

The Twitter piece

You need to know what Twitter is as a platform, and what its affordances are. For example, some of my bots favorite their own tweets that are RT’ed by other people. I would not have been able to do this if I hadn’t known that: a Twitter user can fav their own tweet, and you can detect when someone RT’s your stuff.

You need to understand what an API is. You also need to understand how to talk to a REST API via OAuth 2, because that is what Twitter’s API uses. Even if you use a library to talk to the Twitter API, you’re going to have to understand the basics of this stuff.

All of this stuff can be packaged up for you, more or less. In fact, I’ve mostly done it here with my examplebot repository. If you follow the instructions, you can easily have a bot that RT’s tweets on a set interval containing a keyword you specify. But if you want to make something a little more creative, we get into the murkier waters of…

The bot piece

If you want to make a creative, interesting bot, you need to understand computer programming. Seriously. You don’t have to be a good programmer, but at the most basic level you need to be able to read API documentation, search for stuff on StackOverflow, copy/paste/modify that code into your own, debug error messages from servers, and run things on the command line.

What goes into the generation of the actual content for a bot? Well, sometimes I use the Wordnik API. This means I have to understand what Wordnik as a technology is, how to talk to a REST API, and how to parse a JSON response, and what JSON-P is. On a meta-level, I will use Programmable Web’s API search to search existing APIs and evaluate them to see if they’re of use to me. Those APIs usually return JSON data or XML data and I have to be ready to parse both. If APIs aren’t available for the data I want, I use screen scraping techniques, which involves making an HTTP request, loading the results into a nice screen scraping library, and learning how to parse HTML/XML using the screen scraper. Often I find myself using regular expressions if I want to extract a bit of information encoded in plain text in a particular way. I sometimes resort to using image processing utilities to generate images, which requires some knowledge of computer graphics and graphics encoding.  Of course, almost every bot I make requires some kind of file I/O, which I usually need to look up how to do because I always forget that stuff. And then we need to be able to set up a program that does stuff at a given time interval. Oh, and one of the things that helps me write bug-free code is having a workflow set up with a task runner that is always checking my code for errors and sometimes even running unit tests if I’ve been particularly fastidious. This does not even begin to address actually deploying the bot, for which I need to know how to use one of several cloud application hosting services.

I hope you get the picture: making bots is technically challenging.

The reason I am able to make Twitter bots is because I have been programming computers in a shitty, haphazard way for 15 years, followed by maybe 5 years of less-shitty programming. Every single sentence in the big preceding paragraph, every little atom of knowledge, represents hours of banging my head up against a series of technical walls, googling for magic words to get libraries to compile, scouring obscure documentation to figure out what the hell I’m supposed to do, and re-learning stuff I’d forgotten because I hadn’t used it in a while. (Seriously, I think I re-learned regular expressions on five separate occasions in the last 20 years and it only stuck a couple years ago.)

Learning to program computers is not easy. It’s hard, and there are a lot of people who want to tell you otherwise, and please don’t listen to them. But also: learning to program is very rewarding, and lets you do some pretty amazing things. (Programming is not special in this regard! Here are things that are hard to be proficient at, yet are rewarding: woodworking, dancing, painting, sculpting, composing music, playing an instrument, playing a sport, playing an e-Sport, writing, public speaking, etc etc etc.)

Um, gee, that was a lot of ranting…

Phew. Yeah. I guess I needed to get that off my chest. Anyway, I’m really grateful that there are people like Tully who can put together a list of reasonable resources for people who can’t program. And I know people who are building tools to help people make Twitter bots. But tools are never going to get you a full range of creative expression. So, if you’re asking me, “I don’t know how to program, can you teach me how to make a Twitter bot?”, I will first point you to Tully’s article, and then if that doesn’t satisfy you, I will point you here.


Dan October 1, 2013 at 10:02 am

I don’t think I have ever said this to you before, but many thanks for these posts. They have been a great way to learn how to leverage APIs to do some fun things. The posts are also inspirational in that you continue to drive home the point that to do anything rewarding, it requires perseverance.

Darius Kazemi October 1, 2013 at 4:03 pm


Tully Hansen October 1, 2013 at 10:31 pm

Thanks for the shout-out! And, of course, your ongoing creativity, dedication and commitment to Sparkle Motion the #botALLY cause.

For the #botCURIOUS, I’d also recommend Patrick Rodriguez (@LightAesthetic’s) piece on Google Apps for Twitter bots, which sits somewhere between you and I in terms of the level at which it’s being pitched:

MichaelPaulukonis October 2, 2013 at 10:45 am

Thanks for all of the info! I ran across your stuff sometime this summer, and have been digesting it slowly. I’ve been coming around to looking at my web/text experiments as gaming-interaction (of a poor, frustrating sort), so it is very interesting to see how you’ve come to text experiments (how I’m characterizing your twitterbots, at any rate) from gaming.

What the range of your Heroku costs for the various twitter-bots?

Darius Kazemi October 2, 2013 at 11:09 am

Heroku is free for Twitter bots. The way it works is every app gets one free process (or “drone”). Each twitter bot only requires one process to run, so it’s free. Hooray!

MichaelPaulukonis October 15, 2013 at 10:54 am

Oh, that’s even worse than I expected. I’ll never see sunlight again….

Pierre Chevalier October 5, 2013 at 6:10 am

Ok I’m not sure this is the right place to ask for advice, since the article above is precisely about the fact that non-coders should expect hard struggle if they take the bot making path. So if this is a nuisance, apologies.

There is obviously something I don’t understand about the way .js script work, or maybe about the way google’s newsfeed works. In order to make a French version of twoheadlines, I’m trying to have @twoheadlines grabbing headlines in or e.g. in
I can run the original code without problem but as soon as I modify this :

request(baseUrl + ‘/news/section?ned=us&topic=’
because i want to change it for :
request(baseUrl + ‘/news/section?ned=fr&topic=’

all I get when running the bot is an “undefined” message.

I have the feeling I am missing something very obvious, do you have any idea of what it is?

Ok so anyway yes, making a bot must be technically challenging, you’re right.

Pierre Chevalier October 5, 2013 at 11:05 am

I think I got it.
Only the US and UK editions of google news display “hot trends” in the left menu, all the other editions (including french) don’t.

Darius Kazemi October 6, 2013 at 5:28 pm

Yeah, that’s exactly what’s going on.

Wally Glutton December 19, 2013 at 2:18 pm

I gave a presentation on building Twitter Bots in Ruby last month at the BSides Winnipeg security conference.

The slides from my talk are available:

A believe a video of the talk will eventually surface here:

Darius Kazemi December 20, 2013 at 9:10 am

Thanks for the links!!

Comments on this entry are closed.

{ 2 trackbacks }

Previous post:

Next post: