dark mode light mode Search Menu
Search

How to Make a Twitter Bot

Nicole on Flickr

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 haphazard way for 15 years, followed by maybe 5 years of less-haphazard 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.

This article is reprinted from a blog post, How to Make a Twitter Bot, on Darius Kazemi’s website, Tiny Subversions.

Learn More

Darius Kazemi

http://tinysubversions.com/
https://twitter.com/tinysubversions
https://github.com/dariusk/
https://twitter.com/tinysubversions/darius-kazemi-s-bots/members

The Botmaker Who Sees Through the Internet

A recent Boston Globe article about the author, and his views of the article and interview process.
http://www.bostonglobe.com/ideas/2014/01/24/the-botmaker-who-sees-through-internet/V7Qn7HU8TPPl7MSM2TvbsJ/story.html
http://tinysubversions.com/2014/01/thoughts-on-a-profile-of-me-and-my-bots/

How I Built the Metaphor a Minute Bot

http://twitter.com/metaphorminute
http://tinysubversions.com/2012/05/how-i-built-metaphor-a-minute/
https://github.com/dariusk/metaphor-a-minute

Two Headlines Bot

http://twitter.com/twoheadlines
https://github.com/dariusk/twoheadlines
http://tinysubversions.com/twoheadlines/docs/

Example Bot

https://github.com/dariusk/examplebot

Fake It "˜Til You Make It: A Basic Bot Primer For The Aprogrammatic

http://blog.tullyhansen.com/post/62774813528/fake-it-til-you-make-it-a-basic-bot-primer-for-the

Wordnik API

http://developer.wordnik.com

Programmable Web API Search Engine

http://www.programmableweb.com/

cheerio Screen Scraping Library

http://matthewmueller.github.io/cheerio/

Rubular Regular Expression Editor

http://rubular.com/

ImageMagick Image Processing Utilities

http://www.imagemagick.org/script/

Grunt: The JavaScript Task Runner

http://www.gruntjs.com

Heroku Web Hosting

https://www.heroku.com/

How Do I Learn to Program Computers Search Results

http://bit.ly/18I8ctf