Creating a simple Twitter bot with Python

Maria Fernanda Azolin
Level Up Coding
Published in
6 min readJun 3, 2020

--

This is going to be an easy and fast tutorial to create a simple Twitter bot using the Python language and the Tweepy library.

This material can be followed by anyone (even if you know very little about coding), and it will cover the basics from creating the accounts you will use, what you need to install and some examples of what you can do with a Twitter bot. Maybe it gets a little long since I tried to divide it in small pieces and explain them all, but you can easily skip a few parts if you find some over-explained.

Now, what you'll need to do:

  1. Create your twitter developer account
  2. Install python
  3. Create the bot app
  4. Write some (very short) code
  5. Tweet!

1. Create your twitter developer account

So, before anything else, you'll need to have a twitter developer account. With this account, you have access to the Twitter API we are going to need to develop this bot.

Go to the twitter developer website.

Click "Apply for a developer account"

Click in the purple "Apply for a developer account" button and log into your regular twitter account if you haven't already.

Choose the "Making a bot" option

Select the "Making a bot" card and go to the next page.

Fill the following fields of the form as they appear. There's no secret in this part, just provide the information that is requested. When you reach "How will you use the Twitter API or Twitter data?" page, answer something like this:

After that, accept the Developer Agreement and submit application.

This part can be different for everyone. Sometimes the application is accepted right away, and sometimes it takes up to 1–2 weeks. Once your developer account is okay, proceed in this tutorial.

2. Install python

You'll need Python3 installed. Two ways you can do that.

  1. Using Homebrew, just type in your terminal:
$ brew install python3 && cp /usr/local/bin/python3 /usr/local/bin/python

2. Or download it from here.

After installing, check if everything is okay, typing in your terminal:

$ python3 --version

This should return the current version of python installed in your PC in case everything went fine. Now, install the library we are going to use in the command line:

$ pip install tweepy

(If it doesn't work try pip3)

3. Create the bot app

Go back to the Twitter Developer website, and click in your username in the top right corner of the screen. Choose the option "Apps" from the dropdown menu. Then "Create an app".

Provide an app name, description and the other required fields. Click "Create" twice.

With the app created, check the tab Permissions to see if your app has reading and writing permissions. In case it doesn't, just edit it.

Next, go to the Keys and Tokens tab and copy your API key, API secret key, Access token and Access token secret somewhere. We are going to use it later.

4. Write some (very short) code

Now we are gonna do some coding. Use the text editor from your preference (I personally use VSCode). Other nice options are Atom or Sublime Text. And you can also use the native text editor form your PC if you don't want to download anything.

Create a folder, and inside that folder, create a python file, and then open it. You can do this using your terminal with the following:

MacOS and Linux:

$ mkdir twitterBot
$ cd twitterBot
$ touch twitterBot.py
$ open twitterBot.py

Windows:

$ mkdir twitterBot
$ cd twitterBot
$ type nul > twitterBot.py
$ twitterBot.py

Now, inside the file you just created and opened:

import tweepy
import time

This will import the Tweepy and Time libraries, so we can use its functions. After the import, copy and past this lines of code:

CONSUMER_KEY = 'your API key number here'
CONSUMER_SECRET = 'your API secret key number here'
ACCESS_KEY = 'your access token here'
ACCESS_SECRET = 'your access token secret here'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

Substitute here the keys you saved from the app you created. This lines will configure what you need to read and write data from twitter. After that, type a simple print command, like this:

print(“just a few more steps to finish this bot”)

Now, you file should look something like this:

Let's test if everything is working fine. From your terminal, access the folder where this file is located and run it with python3. It should display the message you entered inside the print statement after you run this command:

$ python3 twitterBot.py

Now, let's effectively use the Twitter API. First, add this line to your code:

twitter_API = tweepy.API(auth)

Now, every time we want to use some function to write and read data from twitter we will call it with this variable `twitter_API`.

But what functions will we use? Well, there's a bunch of interesting ones. To check on them, read the Tweepy documentation. This documentation is very clear and complete, and you should be able to undestand and use it with no problems.

But let's start with a simple one. Let's get a list of your followers and make tweets mentioning everyone of them. Add this to the end of your code.

def mention_followers():followers = twitter_API.followers()for follower in followers:
twitter_API.update_status('Hello @' + follower.screen_name)
while True:
mention_followers()
time.sleep(3600)

Just that. Yep. Your twitter bot is ready!

So, what happend here: First you created a function called mention_followers. Inside that function, you got a list of your followers from the Twitter API, using the variable you created before (twitter_API). Then, you iterated the list and mentioned the followers using theirs names (with the API data screen_name).

Finally, you called that function in an infinite loop. The sleep function prevents Twitter from blocking the account. If you tweet to many times in a row (specially repeated tweets), this can be considered spamming.

5. Tweet!

And now, to see this working, navigate to the folder with your python file inside your terminal, and run the script with:

python3 twitterBot.py

If you open twitter now, you should see that your account just tweeted mentioning everyone of your followers.

What to do now?

Well, this was just a short example of what you can do with a twitter bot written in python.

Feel free to explore the many other options the API provides. You can easily do things like getting the mentions you receive, the most recent status from your timeline, and so on. Be creative!

Another thing you can do is hosting your script in the cloud (using some service like Heroku), so you can automate it even more.

Source code

The full source code from this tutorial can be found in my GitHub.

--

--

An iOS Developer and Computer Engineering student trying to write some tutorials