How to Code a Simple Twitter Bot for Complete Beginners

Sarah
Dev Tutorials
Published in
8 min readNov 2, 2015

--

Recently, I decided I wanted to make a Twitter bot but I had no knowledge of coding besides html for basic Tumblr theme adjustments and in my workplace’s CMS.

I did what any curious person would do and Googled it. After clicking on a few articles and finding myself extremely overwhelmed by the language and lack of comprehensive instructions, I found Lauren Orsini’s piece for ReadWrite the easiest to follow.

However, there are a lot of blanks in there, and it assumes you have some knowledge of Terminal and coding, which I didn’t — I’d only ever used Terminal for the occasional Easter Egg.

So, I’ll be basing this tutorial on that piece, but filling in essential blanks that might be vital for someone as clueless as I was.

After hours of struggling with the process due to these blanks, I finally got my bot alter ego up and running: @sahimbot. So, I decided to create a bot for @IAmAGreg and @oilcantim: @oilcangreggers, which is way funnier than my own bot.

Once you figure out what you’re doing and you have set up all the prerequisites, the process of creating a bot takes no longer than fifteen - twenty minutes.

This tutorial will assume you own a Mac, which makes this exclusive by default, I know, but it’s the only way I know how to do it. I am by no means an expert, but this works.

If you want to make a bot baby i.e. a bot based on more than one account, the maximum this code will let you use is two base accounts, unfortunately. Also, your base accounts must have tweeted more than 3600 times. The bot only has the ability to tweet: not retweet, @reply, or DM.

I’m currently trying to suss out how to use Mispy’s twitter_books which allows for a more “sentient” bot because again, there are no simple tutorials out there. Once I do figure it out, I will write up a step-by-step tutorial for that.

To simplify things, it’s actually very useful to think of what you’re doing as a machine of sorts. Your Twitter bot needs some fuel, and something to convert that fuel into energy so it can operate. So, the code where you set the parameters of the bot is your fuel, Terminal is your fuel tank, Git is what transfers the fuel from the tank to be converted into energy, and the hub is Heroku, receiving, converting, and diverting this energy to run your bot. I guess this makes you the fuel pump!

First thing’s first, you need to download and install Atom. A lot of my issues derived from editing the code in TextEdit, Apple’s rich/plain text editor. This will ensure that everything is typed perfectly.

Next, you need to install a Heroku toolbelt for Python. This is where it gets scary, I know, but it’s actually incredibly simple.

Head on over to Heroku and sign up for an account. When asked to specify what code you’ll be using, click Python. The annoying thing is you do need to add your credit or debit card to Heroku to “verify” your account. It fortunately doesn’t charge you for anything, and it is a legitimate service, but we’ll get to that later.

Once you’re signed up, click here to to go to the Python development centre and download the Toolbelt.

Install the package as you normally would an app, but you might notice that installing hasn’t done anything – this is because it needs to be enabled in Terminal.

Search for the application “Terminal.” Once opened, type heroku into it, and press enter.

As you can see, this properly installs Heroku on your computer. You will see a window pop up that looks like this:

You need to click “Install.” Then, it will find the software and install it.

Once that has finished, you need to type heroku login and hit enter.

Type your email, hit enter, then your password, and again, hit enter. Your success message will look like this:

Now, you have everything installed!

You now need to set up a Twitter account for your bot. It must have a mobile number linked to it, but you can remove it once the process is complete. I assume you know how to sign up for Twitter, otherwise you wouldn’t be reading this!

I have decided to make a bot combining Donald Trump and Cher’s tweets, because some part of me is definitely sadistic: @Cher_Trump.

Once you’ve signed up, you need to click here to create an app attached to your account. After clicking “Create an app,” you can fill anything in the form, but it’s best to name it appropriately for the bot.

Agree to the terms, sign up, and then you will be taken to this page:

I’ve obscured the keys because they essentially enable you to hack into the Twitter account and tweet from it.

Click on “Keys and Access Tokens.”

You need to scroll down and click “Generate access token and token secret,” which I forgot to take a screenshot of, but here is the end result:

Keep this tab open as you’ll be needing it very soon.

Next, you need to head over to Tom Meagher’s Github page, and click “Download ZIP.” These are the files necessary to build your bot, otherwise known as a repository.

Get off that tab, it’s too busy.

Once you’ve downloaded the essential components, it’s time to open the folder:

Remove the ‘README’ and ‘local_settings_example.py’ files from the folder.

Open up the local settings file in Atom:

You need to change all instances of to . Do NOT copy and paste from here, as your quotes need to be one hundred percent straight. Once you have done that, it should look like this:

Changing the code is pretty self-explanatory: under #configuration, you need to fill in the corresponding keys from the Twitter tab we left open from earlier.

  • Where it tells you to #fill your bot name, delete that text and replace it with the name of your bot, in this instance ‘Cher_Trump’.
  • Under SOURCE_ACCOUNTS, fill in the account or accounts you want to base your bot on without the @.
  • Under ODDS, change the 8 to 1.
  • Leave the ORDER as it is — it doesn’t seem to like having a nonsensical setting.
  • Change True under DEBUG to False.
  • Put your bot’s Twitter account under TWEET_ACCOUNT.

It should look something like this:

You’ll then want to save it as ‘local_settings.py’ in the same folder:

The folder should now look like this:

Now it’s time to put all of this into Terminal! I promise we’re almost done here.

In terminal, type cd, hit space, then drag the folder into Terminal. This stands for “change directory,” so we’re going to prep this folder to send to Heroku.

It should look like this:

Hit enter.

Now, you need to type in git init and hit enter. This initialises Git, which will deliver the files to Heroku.

Type git add . and hit enter; the space between add and the full stop is intentional. This essentially collates the files you wish to send to Heroku.

Then git commit -am “Add all files”, enter. The “commit” should tell you that it’s saying “Yup, I want to do this.” It’s like a roll call for your files, ensuring each file you want to send to Heroku is there.

Then heroku create [double hyphen]stack cedar, enter.

Here, you’re asking Heroku to create a space for Git to deliver the files to. In this case, you can see this space is called “intense-citadel-7242.”

Then git push heroku master, enter.

Here, you’re telling Git to to send these files to Heroku.

And, for the almost final step, type in heroku run worker and hit enter.

You’re now testing this app you’ve built by telling Heroku to run it.

You see where it says “Dear I greatly appreciate your support this morning.”? That’s actually a success message and our first tweet from the bot!

Now, we need to schedule how often we want this bot to tweet.

Go back into Terminal, and type in heroku addons:add scheduler:standard, hit enter, then heroku addons:open scheduler, and hit enter. If you have not added your card to Heroku, now’s the time to do so. In fact, it will prompt you to do so.

Once you’ve done that, type in heroku addons:open scheduler again, and Terminal will open this web page.

Click “Add new job.”

Copy exactly what I have written here, but where it says “Next due”, you can set at what minute past the hour the app will run. A tweet will not be sent out every hour. As Orsini explained, it’s more of a random number generator, but chances are you’ll get 14–20 tweets a day out of the bot.

Click save and your bot is all set up! Congrats!

Send me your bots! I’ll be keeping a close eye on what kind of monsters @Cher_Trump generates.

--

--

Freelance writer. As seen in The Guardian, Pitchfork, Rolling Stone, Playboy, and your nightmares. @SarahSahim