What will we cover in this tutorial?
- To build a Bot to Follow Followers in Twitter using Python
- Link to how you can get your access tokens and consumer keys to get access to the Twitter API (needed)
- How to access the Twitter API
- Finally, full code example of a Python Twitter Bot to follow the followers your account does not follow already.
Step 1: Setup up environment
In order to get a connection to twitter you need to have access tokens and consumer keys. If you don’t already have that, or you do not know what it is, then I recommend you follow this tutorial.

You also need the tweepy library. You can install it by typing the following command in the command line or see here for more details.
pip install tweepy
Then you are ready to connect to the Twitter API.
Step 2: Connecting to Twitter API
The first thing your code should do is to connect to the Twitter API and return the tweepy api to your program.

import tweepy
def get_twitter_api():
# personal details
consumer_key = "__USE YOUR KEY HERE__"
consumer_secret = "__USE YOUR KEY HERE__"
access_token = "__USE YOUR KEY HERE__"
access_token_secret = "__USE YOUR KEY HERE__"
# authentication of consumer key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# authentication of access token and secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
return api
This code will authenticate and return the tweepy api.
Step 3: List followers and friends to follow back
Confused by the headline? Me, too. But here is where the magic happens.
The code simply explained.
- Retrieves all the users that follow you (followers)
- Retrieves those you follow (friends)
- Loops through followers and check if you follow them.
- If not, follow them back
def process():
api = get_twitter_api()
followers = api.followers_ids(api.me().id)
print("Followers", len(followers))
friends = api.friends_ids(api.me().id)
print("You follow:", len(friends))
for follower in followers:
if follower not in friends:
api.create_friendship(follower)
Full code example here
You can see the full code here.
import tweepy
def get_twitter_api():
# personal details
consumer_key = "__USE YOUR KEY HERE__"
consumer_secret = "__USE YOUR KEY HERE__"
access_token = "__USE YOUR KEY HERE__"
access_token_secret = "__USE YOUR KEY HERE__"
# authentication of consumer key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# authentication of access token and secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
return api
def process():
api = get_twitter_api()
followers = api.followers_ids(api.me().id)
print("Followers", len(followers))
friends = api.friends_ids(api.me().id)
print("You follow:", len(friends))
for follower in followers:
if follower not in friends:
api.create_friendship(follower)
if __name__ == "__main__":
process()
Next steps

- Deploy it to an cron job so it runs every hour.
- You can use PythonAnywhere (not sponsored by them)
Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON
- 70 pages to get you started on your journey to master Python.
- How to install your setup with Anaconda.
- Written description and introduction to all concepts.
- Jupyter Notebooks prepared for 17 projects.
Python 101: A CRASH COURSE
- How to get started with this 8 hours Python 101: A CRASH COURSE.
- Best practices for learning Python.
- How to download the material to follow along and create projects.
- A chapter for each lesson with a description, code snippets for easy reference, and links to a lesson video.
Expert Data Science Blueprint

Expert Data Science Blueprint
- Master the Data Science Workflow for actionable data insights.
- How to download the material to follow along and create projects.
- A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.
Machine Learning

Machine Learning – The Simple Path to Mastery
- How to get started with Machine Learning.
- How to download the material to follow along and make the projects.
- One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.