How to Make a Twitter Bot: The Definitive Guide

Before you start making bots, consider reading these essays and articles. Also worth browsing: resources for cleaning up your bot's language.

Browse Botwiki Guides

So you want to make a friendly/useful/artistic Twitter bot and probably have a bunch of questions, like How often should my bot tweet? or Why am I getting this weird error message? and How do I even make a Twitter bot?

Hopefully this ambitiously titled guide will answer your questions — and you are always free to ask for more help in the Botmakers community.

Note on needing a phone number

One tricky part of making a bot for Twitter is that if you want your bot to be able to actually post on Twitter, rather than just read from it, you will need to add a phone number to your account.

There is a few ways to solve this:

What kind of a bot should I make?

Let’s first talk about the kinds of bots you absolutely should not make. Start by reviewing Twitter’s Rules and best practices and their Automation rules. There are also API rate limits, but we will get into that shortly.

The bottom line is this: don’t let your bot annoy people. Try to avoid interacting with people who don’t follow your bot or knowingly initiate the conversation either through a tweet with your bot’s handle, or a direct message.

There certainly are clever examples of bots who do interact with random Twitter users who didn’t explicitly opt-in, but this is a bit of a gray area and you should use your best judgement here.

Also, just because some information is public, it doesn’t mean it deserves exposure.

And it hopefully doesn’t even need to be said that you shouldn’t make bots that harass people, post spam, or do any other malicious activity. Remember, bots should always punch up, never punch down.

As for what your bot should do, well, here are a few tips:

For even more inspiration, check out these essays and articles.

Making a (Twitter) bot is not always a straightforward process, with steps to go through. Sometimes the decisions you make affect each other, for example, you can start with an idea and then decide what language and hosting platform you’ll use. But you can also first look at the various platforms you can use to host your bot and play with their strengths and limitations. More on that in an upcoming section.

How do I make a Twitter bot?

With most of the non-technical questions out of the way, it’s time to actually build your Twitter bot.

Botwiki has a lot of tutorials and there are many open-source Twitter bots and Twitter bot project templates on Glitch that you can use.

Just find one that’s close to your own bot idea and take it from there.

How often should my Twitter bot tweet?

Alright, you might have a rough idea what your bot is going to be posting about now, great! How often should it post though?

As I mentioned earlier, Twitter has limits on how often you can call their API, so you should keep those in mind.

Other than that, it’s all about striking the right balance. If your bot tweets every time a certain event happens, it’s okay if your bot posts more frequently, as long as it stays under the API rate limit.

But also consider that some people don’t want their home timeline cluttered with just one account, so for bots that don’t work with real-time data, spacing the tweets out makes a lot of sense. As I said above, one good rule to follow is to make sure your bot doesn’t annoy people.

There are bots that post every hour, once or twice a day, or even just once throughout the whole year.

Here are some thoughts from a member of Botmakers:

Hard question. I look at it as a logarithmic-scale line starting at once a minute and ending somewhere around once a week or month.

There is a hard upper bound of 25 tweets per 15 minutes per account.

There are multiple approaches you can take to figured this out, and they’re mostly situational:

The easiest approach is to have your bot post as often as it can, like if you’re running @congressedits, you want to catch all edits.

If you have too much information for this and need to attenuate the flow, or if your corpus is fixed or precomputed, then it comes down to questions like, do I want to follow this bot myself? How often would I like to hear from it if so? Is there prior art that I am mimicking? If it’s a fixed corpus, how long should it run before it ends? How often would others like to hear from it? should it be annoying/how much so?

If you’re unsure, post every 30 or 60 minutes, tweak it later if you don’t like it.

— @air_hadoken

 

Do I need to attribute content tweeted by my bot?

Absolutely!

Can I make a bot that tracks keywords or hashtags?

From Twitter’s Automation rules:

Automated Retweets: Provided you comply with all other rules, you may Retweet or Quote Tweet in an automated manner for entertainment, informational, or novelty purposes. Automated Retweets often lead to negative user experiences, and bulk, aggressive, or spammy Retweeting is a violation of the Twitter Rules.

As explained above, there are some gray areas when it comes to automating certain actions on Twitter, including automated retweets.

One way around getting your retweet bot suspended is to make your bot private. This way you can follow the topics you’re interested in without bothering anyone since notifications from private accounts don’t show up to people your bot is not following.

The biggest drawback here is that it will be harder for others to discover your bot, and as of February 2017, there is no way to approve follower requests for a protected account via the Twitter API, so you will have to handle follower requests manually.

Can I make a bot that posts Twitter polls?

There is officially no API endpoint for Twitter polls, but a few people figure out how to do this anyway. Here are some starting points:

The Botwiki team also manages a Twitter list of bots posting polls.

Where should I host my bot?

There is a number of platforms you can use depending on how much you’re willing to spend (no worries, free options exist) and how comfortable you are dealing with web servers.

Also, when coming up with ideas for a new bot, it might actually be helpful to start here. For example, Cheap Bots, Done Quick! is a site that lets you make bots using Tracery and hosts them for free. It doesn’t require advanced programming skills, but is flexible enough so that you can get really creative.

Glitch gives you more freedom while taking care of most of the technical challenges for you (and comes with some cool libraries pre-installed). And it’s also free to use.

For a comprehensive list of available hosting platforms, see the Hosting platforms page on Botwiki.

Why is my bot not working?

Twitter will send you an error message that should help you understand why your bot stopped working.

For example, if you’re using the Twit library, here’s how you can see the error message:

T.post('statuses/update', { status: 'hello world!' }, function(err, data, response) {
  if (err){
    console.log(err);
  }
})

It’s possible that your bot tweets or reads the data from Twitter too often, then you just need to adjust the frequency. It’s also possible that your bot was suspended, in which case you will also get an email notification and instructions on what to do next.

If you’re not getting any error messages from Twitter, and the bot doesn’t seem to be suspended, the problem might be with your code. It’s perfectly fine to ask for help, in fact, that’s why the Botmakers community exists. Glitch even has an “ask for help” feature built right into it.

Use the same email (sort of) for multiple accounts

One neat trick you can use when registering for a new account is adding +SOMETHING to your email address, for example, stefan+mynewbot@botwiki.org. This will let you use the same email address for multiple accounts — and this works for many websites, not just Twitter.

Stop the Twitter email spam

Twitter doesn’t really differentiate between “regular” user accounts and bots. Bots are really just automated user accounts — in fact, you could easily add some automation to your own account while you’re still using it as a human.

One of the problems with this setup is that Twitter will happily send emails and other notifications to your bot as if it was a real person, unless you specifically opt-out.

Wrap up

I hope this guide will help you deal with some of the biggest questions and challenges of making friendly Twitter bots. Be sure to join us in the Botmakers community. We are artists, journalists, educators, tinkerers, bot enthusiasts, seasoned developers, and always happy to help a fellow botmaker out 🙂

If you’d like to leave me feedback and suggestions for this guide, feel free to send me an email!

PS: See more of my tutorials here.

Special thanks

Stefan

Creator of Botwiki and Botmakers, Botwiki editor and Botmakers community manager.