Twitter Bot



A quick guide to building a twitter bot that helps people quit twitter.

Get Developer Access. It’s probably not too surprising that in order to create a Twitter bot, you need. The best Twitter bot is Twesocial. Twesocial is number one on our list because it is highly personable, has unique safety features, and prices its services reasonably. It caters to a wide range of customers while remaining specialized with Twitter.

Note — this tutorial assumes you have basic knowledge of javascript and node.js You can find the final code in this github repo.

Before we begin, we’ll take a look at what a twitter bot can do, and some examples of existing twitter bots.

What is a twitter bot and what can they do?

A twitter bot is an automated program that tweets, likes, follows, or does any other action that a normal (human) twitter user can do.

But, unlike a human, a twitter bot can perform tasks over and over without becoming bored, sleep deprived, or dead. Here’s some examples of what a twitter bot can do:

  • Monitor every tweet being tweeted everywhere in the world in real time.
  • Automatically respond to tweets that mention a specific word, phrase, or hashtag.
  • Collect vast amounts of data and display that data in real-time anywhere on the web.
  • Automatically tweet in response to some event. e.g. 70% chance of snow in Colorado.
  • Interfere with democracy by pushing false naratives that sway the electorate to the far-right.

Types of twitter bots and some notable examples

Generally, twitter bots fall into one of these categories…

Bots that tweet at regular intervals

@tinycarebot — tiny care bot tweets a self care reminder every hour, like “please remember to listen to some music that helps you feel calm”

@poem_exe — generates tiny poems (“micro-poetry”) every few hours.

Bots that tweet based on an event

@earthquakebot — Tweets about any earthquake that registers 5.0 or above.

Bots that reply to you performing some action towards the bot

This could be as simple as an auto-responder for your business. Or a complex chat bot.

@botifuldingbot — the now shut-down beautiful dingbats font changing bot. It got a little carried away.

Bots that reply to your tweets when you didn’t interact with it at all!

@yesyoureracist — This bot retweets any tweet that starts with the phrase “I’m not racist, but…” The results are always racist.

@grammar — A bot that corrects you’re your grammar.

This is the kind of bot we’ll be making in this tutorial. Let’s get started.

How to make a twitter bot…

I don’t really like twitter — it’s reinforces toxic behaviour and is complicit in the downfall of democracy. So we’re going to make a twitter bot that helps people quit twitter.

Our twitter bot will find people who seem like they’re on the edge of quitting twitter, and will give them encouragement and the instructions on how to do it.

Getting Started (non-coding bit)

Create an account

First you’ll need a twitter account for your bot. If you already have one, great. Otherwise go to twitter.com and follow the normal steps for making an account.

Create a developer account

You’ll also need to signup for a developer account. It doesn’t take long, just follow the instructions below…

  1. Navigate to https://developer.twitter.com/en/apply/user.html
  2. Click Apply (if you’re not logged into your twitter account, you’ll be asked to now.)
  3. Fill out the form. It contains a few steps including, why you want to join the twitter developer program, details about you, and the intended use of your bot.
  4. Create an app, navigate to ‘apps’ in the dropdown under your username it will take you to the apps page. Then click, ‘create an app.’ Fill out the form on the create and app page. It contains some repetitive information.
  5. Once you’ve created your app. You’ll be taken to the app details page. Click on “keys and tokens,” then generate consumer api keys, and access tokens. We’ll need all four tokens in a minute.

That’s the non-coding bit out of the way.

Coding our twitter bot

Running a node.js server.

Now you’ll need to set up a server running node.js If you don’t know node.js you’ll need to get familiar with it before we continue. There are lots of tutorials out there that cover how to make a basic node server.

Javascript is normally used just for front-end web development, for building functionality on a web page. Node allows us to use javascript, a language front-end developers are familiar with, on the back-end.

If you want to run your server for free, I recommend using a service like Heroku.

Installing and setting up ‘twit’

First install the twitter Api Client for Node. In your terminal type…

We’ll be making our twitter bot in one file called app.js First require ‘twit’ and create an instance of twit with your twitter app’s keys and tokens.

That’s the initial variables set up. Now we need to write the code that will run while our twitter bot is active. When our node server starts, we need it to connect to twitter and verify itself with the credentials we defined above.

Sending our first tweet

Let’s see if we can get the bot to tweet. We’ll create a tweet function and test it by having it tweet ‘hello world’ after it’s authenticated with twitter.

Okay, now run your node app. Hopefully it worked and you’ll see that your twitter account has tweeted ‘hello world.’

Monitoring Tweets

Now, we want our bot to reply to people based on what they tweet. To do that we need to start monitoring tweets, twitter gives us a way to do this — the streaming api. It sends us a stream of tweets based on a set of words or phrases.

Here I’ve chosen to use the phrases ‘leave twitter’, ‘quit twitter’, ‘delete twitter’.

Now, we can log all the tweets we’re receiving in the console. See how many there are?! We’re getting more than a tweet per second. All that #maga streaming across your console? That’s twitter!

Twitter doesn’t filter the tweets as as accurately as we’d like, so we’ll need to check each of these tweet for exact phrases we want to match. (If we replied to all of these tweets, we’d get banned in an instant.)

We also want to make sure we don’t reply to retweets or to any tweets sent by our own bot too.

Now let’s get it to tweet a reply whenever somebody tweets a tweet with one of our target phrases.

Tweeting a reply

To send a reply to a tweet, you’ll need to add the handle of the account you want to reply to, to the text of your tweet.

You’ll also need to include the unique ID of the tweet you’re replying to.

Also, we don’t want our bot to tweet the same thing all the time, so we’re also going to create an array of replies and pick one at random when we respond.

Our bot’s response will contain the handle of the account we’re replying to , a random response from our array, and then instructions on how to quit twitter.

testing the bot

We’ve already made sure our bot doesn’t reply to itself. (As this could cause an infinite loop and ultimately get it banned from twitter.)

To test your twitter bot, use a different account (maybe just ask a friend) and tweet something with one of the terms you want to match.

Release your bot into the wild.

Does your bot work? Great. Time to release it to the public. You can check out @qwitterbot here.

Bonus Round — Updating our profile if our bot gets rate limited.

Twitter has a limit on how many tweets your bot can send. You can read more about rate limits here: https://developer.twitter.com/en/docs/basics/rate-limiting

Your bot can tweet 15 tweets every 15 minutes — if you tweet more than that, your bot will be temporarily disabled.

You might want to let people know that your bot has been disabled, to do that we use the callback function on our tweet function. If it responds with an error, we check what caused the error.

If that error code is 88 we know it’s because we’ve reached our rate limit. We can update our profile name and description to let people know.

We also want to update the profile if the bot is no longer disabled.

First we create a global variable named isAsleep and set it to false. Then we write our function…

If you found this tutorial useful (or not), please let me know. (it’s the first coding tutorial I’ve ever written, so any feedback is highly appreciated)

Botter

And of course, send me your twitter bots!

By

Twitter Bot Maker

BotZachary M. Seward

Chief executive officer

Counted among Twitter’s 271 million active users are an untold number of bots. These accounts were created by people with a pulse but tweet without human intervention. Some are handy, others beautiful, many peculiar. Bots, it should be said, are some of the most productive members of Twitter’s society.

But while humans are showered with praise for their tweets, bots have not traditionally received the same love. Never again! We have compiled a list of Twitter’s best bots.

To determine eligibility, we didn’t perform a Turing test but did require that the account be automated (sorry, @Horse_ebooks) and still actively tweeting. Where it was possible to determine the bot’s creator, we noted it here, but who’s to say those people aren’t actually bots themselves?

Olivia Taters

Free Twitter Bot

Olivia isn’t real, but in many ways, she’s more real than many of the teenage girls whom the account emulates. Rob Dubbin accidentally created the bot while experimenting with language manipulation of real-life teenage Twitter accounts. Olivia will also reply to people who follow her, often with tweets more profound than any human could muster. (Oh, and never forget the time Olivia was chatting with another bot and Bank of America’s customer service account chimed in.)

Netflix Bot

The best bot is a useful bot, and this one proves its worth with a steady stream of videos that are newly available to stream on Netflix in the United States.

Dear Assistant

Another handy bot, created by Amit Agarwal, this one has answers to a wide range of questions—with the help of Wolfram Alpha, the intelligent search engine.

Pixel Sorter

This arty bot by Way Spurr-Chen takes any image you tweet at it and resorts the rows of pixels according to one of a few predetermined rules. The results, like other forms of algorithmic art, are often beautiful.

Quilt Bot

Then there was the time Pixel Sorter started flirting with Bob Poekert’s Quilt Bot, which applies a quilt fabric pattern to any image. If Twitter were just these two bots tweeting at each other, I wouldn’t complain.

Reverse OCR

Another algo art bot, and one of a few accounts on this list by Darius Kazemi, Reverse OCR draws random lines until optical character recognition software thinks it looks like a certain word.

Museum Bot

The art tweeted by this bot, also by Kazemi, was made by humans not algorithms. It features random images from the Metropolitan Museum of Art’s free online archive.

Pentametron

Bot art needn’t be visual, and one of the classics of the form looks for poetry in everyday musings. Ranjit Bhatnagar’s Pentametron retweets tweets that are written in perfect iambic pentameter. On the accompanying website, they become unintentional sonnets.

Accidental Haiku

In a similar fashion, this bot by Cameron Spencer looks for tweets unwittingly composed in the structure of a haiku. (If you like that, you might also enjoy the Tumblr, by Jacob Harris, that finds haikus buried in the New York Times.)

This Is Just to Say

More poetry: This one is more constructed than found art. The bot riffs nonsensically but sometimes beautifully off the famous poem by William Carlos Williams.

My Favorite Things

If your idea of poetry is more like The Sound of Music, then you might prefer this bot riffing off the musical’s most famous song.

Portmanteau Bot

Or you can invent language altogether, like this bot by thricedotted that mashes together real English words to create new ones.

Fake Bloomberg News

Some see poetry in odd places, like the often inscrutable headlines that come out of Bloomberg News. This bot, by Quartz’s own David Yanofsky, takes a few real headlines to form a new one that often makes just as much sense as the originals.

Fuck Every Word

You probably heard about the @everyword account that slowly tweeted every English word over seven years. It has completed its run, but a number of tribute accounts have been created, the best of which does the exact same thing with a profane flourish. It’s the perfect account for a bad day.

Awl Tags

Twitter Bot

Speaking of bad days, sometimes you just need to turn on caps lock, which this account has done since its creation. The bot tweets whatever evocative tags are applied to new stories on the Awl, which are often more enticing than headlines. (See also: Digg Kickers and Verge Pullquotes.)

Big Ben

Not all bots are profound. Some are just worth following for the occasional interruption. This account tweets, as you might imagine, on the hour every hour. It’s most useful if you’re in London, but still enjoyable elsewhere.

Auto Charts

Twitter Bot Followers

We love charts, so we had to include this bot, also by Kazemi, which generates nonsensical Venn diagrams and flow charts. But if you prefer actually good charts, then here are 22 Twitter accounts by humans that we recommend instead.