November 02, 2016

Simple Python Twitter Bot

Twitter Bots

Twitter has some amazing bot personalities; from celebrity impersonators, to genuinely useful automated announcements. It is also probably the first place anyone was exposed to bots and interacted with them. Some of my favourites are BIGBEN and CaptainMarkov. It is also very easy to make a simple bot, especially if you're familiar with python.

In this post, I'm going to walk you through the creation of Mirror_Bot_1000. Mirror_bot_1000 reads tweets when its username is mentioned, and then makes its own tweet with the mirror image.

It's a great example for the simple interactions with Twitter's API because it requires you to read a message, post a message, and keep track of where you left off. If you want to make your bot more complicated, you just have to modify the decision-making behind the tweets.

The entirety of the code in this example can be found on github here. An intro to python can be found here.

Account Setup

The first step is creating an account, and getting the keys to interact with the API. The requirement is simply a Twitter account with a phone number attached. Pro-tip: with Gmail you can make an e-mail alias to have multiple accounts under one address. For example,

Once your account is set up, we need to create and register a Twitter app. To do this, start by heading over to Twitter's apps landing page: Here you will log in, and select "Create a New App". It asks for a name, description, URL, and callback URL. Only the name and description are mandatory.

Once this is done, return to, and click on the new application. Navigate to the "Keys and Access Tokens" tab and scroll down to see:

Consumer Key
Consumer Secret
Access Token Key
Access Token Secret

You may have to click a button to generate the access token. Write these down, as you will need them shortly.

Twitter Bot Setup

To create this bot, you simply need a python interpreter (version 3 and up for this tutorial) and a text editor. You will also need to install the tweepy module.

This is as simple as opening a command prompt, and running the command:
$> python -m easy_install tweepy

Hello World: Posting a Status

Once you have installed tweepy and retrieved your authorization info (consumer and access keys), it's time to start interacting with twitter. The most basic interaction is simply to authorize your account, and post a message. This can be be seen in the following snippet:

Add this code to a file, and name it "". Replace the key and secrets with your own account information, and set the message to anything you would like.

To run this program, open a bash shell or command prompt window and run the following:
 $> python <path_to_file>/

If your account information was entered successfully, this will post the message to your twitter account!

Read a status

Now that we know our authorization is working and we're able to post status updates, let's try reading a status. More specifically, let's search for cases where your username has been mentioned, which is how most interactive bots gather input.

To read statuses, we will use the tweepy.cursor to look for any timeline_mentions. Then we will push all mentioned text to a list object, and return a dict with all the text from the tweets and the highest id. This id will be handy in the next step, to prevent us from repeating the same tweets.

It's ALIVE! Mirrorbot is born.

Now for la pièce de résistance! It's time to put it together into a bot. We need to create a script that will read mentions, reverse the text, and post this as our status. We will put all of this in a "while loop" that does all of the above, and then sleeps for a while to avoid making too many API requests. We will also save the message id so that on the next check only new messages are retrieved.

Note: In python there is a handy trick where you can just append [::-1]to a string to get the reverse. 

Here's how the body of the bot appears:

Run our shell command once again to start the program, and you'll have your very own living breathing twitter bot!

 $> python <path_to_file>/

Bonus: Setup a raspberry pi server for your twitter bot.

No comments:

Post a Comment