How To Make A Twitter Bot In Under An Hour
Even if you don’t code that often!
Update (8/26/21): Much of the information in this post is now outdated. I recommend you follow tommeager’s README instructions in the Github repo used for this project. Feel free to use this guide as a basic reference, but the details on the repo are more accurate.
We recently aired a segment on the weird and wonderful world of Twitter bots. We even decided to make a Twitter bot of our own. It’s called @scifri_ebooks (after the Twitter-famous @horse_ebooks), and it has tweeted some pretty great things so far.
At the core of this particular bot is something called a Markov Chain. It takes awhile to explain, but in this particular case, it helps us build random sentences. If you plug in an existing Twitter account, it takes the words from those tweets, shuffles them, and makes new, sometimes strange, sentences.
We thought you might like to try making a Twitter bot of your own, so we’ve put together a how-to guide. If you’ve never coded before, don’t worry — you’ll be able to follow along with this tutorial. And you’ll get to learn a little about Python and Git while you’re at it!
Disclaimer: This open-source code will not work if your base Twitter account (the one the bot will draw from) has fewer than 3,600 tweets.
Disclaimer part deux: This tutorial is designed for people who are using Macs.
Total Time: < 1 hour
Here’s what we’ll be using:
- A command line interface (like the Terminal app on Mac)
- A text editor (like the TextEdit app on Mac, or Sublime Text)
- Git
- Heroku
If you don’t know what some of these are, DON’T WORRY! All will be explained in due time.
Alright.
Step 1: Set Up a New Account With Twitter
This is where your bot’s tweets will live. Setting up an account is pretty straightforward. If you add the word “ebook” in the username, the Twitter in-crowd will probably pick up that you’re making a bot similar to @horse_ebooks.
You’ll also need to enter your phone number in the next step to create the app.
If you’re creating a bot, but you’re planning on altering the code, I’d recommend you check out Twitter’s Rules for accounts and its rules for Automation. These are in place to protect against spam bots.
Step 2: Create a Twitter App for Your Bot
Awesome, you have a fresh new Twitter account. Now we have to set up the “bot” part of the account. Go to apps.twitter.com to get this started.
Click “Create New App.”
Here, you’ll enter in some basic details about your bot. You can always change these if you want. There’s also no need to put in info for the Callback URL field.
Step 3: Major Keys and Tokens🔑
Your new Twitter app comes with strings of numbers called keys and tokens. These numbers allow the open-source code to connect with Twitter, so it can do things like tweet for us.
Most of the keys are already there, but not everything we need is there automatically. We need to generate a thing called “access tokens.” Go to the “Keys and Access Tokens” tab and go to the bottom of the page; click on the button, “Create my access tokens.”
Important: Never share your keys with others. Bad things could happen.
On that same page, you’ll need four things:
- Consumer Key (API Key)
- Consumer Secret (API Secret)
- Access Token
- Access Token Secret
Okay, we’ll use those in a sec. Keep ’em handy. But first…
Step 4: Download the Repository
This is where Github comes in. If you haven’t heard of Github, it’s basically Dropbox for code.
This is the script we’ll be using, developed by user Tom Meagher. It’s written in Python and Ruby, but we won’t be messing with the code that much.
In the upper right hand corner, click the green button, “Clone or download.” You’ll probably want to choose “Download ZIP.” The script should appear as a zipped folder in your computer’s Download folder.
Step 5: Hack The Matrix (Modify the Settings File)
Go to the “heroku_ebooks” folder you just downloaded and unzipped. Navigate towards the file titled “local_settings_example.py.” I’d recommend using the Mac application TextEdit to open and modify the file.
This is where you’ll be spending most of your time. In this file, you’ll be putting in what Twitter account(s) you want the bot to be based off of, how often you want it to tweet, and how strange you want those tweets to be.
Keys
This file is also the place where you’re going to use those keys and tokens from waaay back in Step 3. So let’s start with that first.
At the top of the file, you’ll see spaces where you’ll be putting the keys and tokens in. In between the quotes, copy and paste your Consumer Key, Consumer Secret, Access Token Key, and Access Token Secret. They’re in the same order that they appear on that apps.twitter.com site.
Source Accounts
Now onto Source Accounts. This is the Twitter account your bot will be based off of. When you enter this info, just put the name of the account, without an “@” symbol. For example, if I wanted to base a bot off @scifri, it would look like:
SOURCE_ACCOUNTS = [“scifri”]
You can put in multiple accounts to make your bot more nuanced. The syntax rule here makes it look like this:
SOURCE_ACCOUNTS = [“scifri”, “iraflatow”, “nprscience”]
When I was setting this up, the script wouldn’t accept Twitter accounts with fewer than 3,600 tweets. According to the Github page, this is a known bug. Thankfully though, it’s not a show-stopper.
Odds
This is where we tell the bot how often we want it to tweet every time the script is run. It’s automatically set at 8 which means that every time the script is executed, there’s a 1 in 8 chance it will generate a tweet. For testing purposes, I’d recommend changing that 8 to a 1.
Order
This tells the script how much sense you want these tweets to make. I’ve only tried it set at 2, which seems to be a happy medium between believable to completely nonsensical, but you can try 1 or 3, if you want. The code doesn’t allow numbers that aren’t 1–3.
Debug and Static Test
These two options are for testing purposes, which you can skip. Set the Debug option to False, and you’re good to go. And you can leave the TEST_SOURCE option as is.
Tweet_Account
This is the final thing you’re going to touch in this file! Enter in your new bot’s Twitter account within the quotes and without the @ symbol,“@,” like above.
Now we’re going to create a new file. This is because the script we’re using is looking for a certain file name. In this case, it’s “local_settings.py.” TextEdit doesn’t automatically allow you to create a custom filetype like a Python file, so in the TextEdit app, click on TextEdit in the upper left hand corner, click “Preferences,” and then click “Plain Text” under Format.
Then, create a new file, paste your code in, and name that file “local_settings.py”, and save it.
Step 6: Heroku
We’re in the home stretch, guys! Now, you’re going to use a service called Heroku. It’s known as a cloud Platform as a Service. Basically, it’s where your code is going to live. Heroku will also take care of running the script and tweeting on a schedule. But first, you have to upload your code.
You’ll have to first sign up for a Heroku account. Go here to do that. On the signup page, under Primary Development Language, you can choose Python. It’ll send an account confirmation to your email, so take care of that, and then you’re ready to go.
You’ll also need to download the Heroku Toolbelt, which allows your command line interface to talk to Heroku. When it’s finished downloading, open the file to install it.
As part of this sign-up process, you’ll need to connect your credit card with Heroku. It’s lame that Heroku makes you do this, but it won’t charge you for what you’re going to be doing.
Now you’ll go into the command line. Launch the Terminal application. If you’ve worked with Terminal before, you can skip the next few paragraphs.
If you’ve never used the Terminal, don’t freak out. You know how you find files using Finder? It’s basically like that, except you’re typing commands to access and modify files (it does some other stuff too, but MORE ON THAT LATER).
For this next part, you need to get into the heroku_ebooks folder, which probably went to your Downloads folder. Here’s the command you’ll need to type; after you do that, hit enter:
cd downloads/heroku_ebooks-master
“Cd” stands for “change directory.” So with that command, you’ll dive into the heroku_ebooks folder.
Once you’re inside the folder, you need to set up git. Git is the middleman that gets your folder from the computer onto Heroku.
Git doesn’t come installed on a computer. It comes bundled in a suite of developer tools called XCode. Type in “git” in your Terminal and hit enter. If it’s not installed, there will be a prompt for you to install XCode. Once that’s finished, you can run the following commands. You’ll need to type and hit enter, one by one, like so.
git init
This makes a new git repository. Basically, it creates a space for you to put your code.
git add .
This tells git that it’s going to process all the files in your current folder.
git commit -am “Add all files”
This is a message that describes the action you’re taking with these files.
heroku create --stack cedar
This tells Heroku to create a space (a server) for your files on their site (formally called a Deployment Environment). This might take a few moments. It will then prompt you to enter the email and password associated with your Heroku account.
git push heroku master
This is the final step and “pushes” your folder to Heroku.
Note: If you want to change something in your settings file, or if you made a mistake, change the file, save, and run the following commands. Remember, you have to be in the heroku_ebooks folder to do this, which the first command takes care of after you launch Terminal.
cd heroku_ebooksgit add local_settings.pygit commit -am “Update local_settings.py”git push heroku master
If it’s working, you should get an output that has a bunch of lines starting with “remote: …,” all of which ends with something that should look like this:
Now, the moment of truth! Run the following command in Terminal to see if it’s working.
heroku run worker
This might not work perfectly every time. The algorithm doesn’t send out tweets if they’re too similar to previous tweets or if they’re too long. In those cases, the return text would read something like, “TOO SIMILAR:” or “TOO LONG:.” Here’s what it should look like when it works:
But keep running the heroku run worker command until something goes through. Keep the bot’s Twitter account open to make sure it does. (Pro tip: In Terminal, instead of retyping the command every time, just hit the up arrow, which calls the most recent command, and hit enter.)
Step 7: Set Up a Schedule
So far, the bot will only tweet if you run the heroku run worker command in the Terminal. You want to automate it, so it’ll take care of that work for you. This means you need to set up a schedule, which Heroku can also do.
Type and run:
heroku addons:add scheduler:standard
This tells Heroku to install the scheduler tool. If you didn’t add your credit card to your Heroku account earlier, this command won’t work. Here’s the link to do that again.
Next:
heroku addons:open scheduler
This will open the page to set up the scheduler. Click “Add New Job,” type in “worker” after the $ sign, and set the Frequency to “Hourly” (or whatever you want).
Note: If you have the ODDS variable in the settings file set to 1 and you have it tweeting hourly, it tweets about 70 percent of the time (due to it running into the TOO SIMILAR or TOO LONG problem). Just something to keep in mind.
Step 8: Enjoy!
Alright! We did it! Happy botting!