Skip to content
Related Articles

Related Articles

How to make a Twitter Bot in Python?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 22 Jul, 2021

Twitter is an American microblogging and social networking service on which users post and interact with messages known as “tweets“. In this article we will make a Twitter Bot using Python.

Python as well as Javascript can be used to develop an automatic Twitter bot that can do many tasks by its own such as:

  • Retweets the tweets with particular #hastags.
  • Favourites/Likes the tweets with particular #hashtags.
  • Follows the users who tweets with particular #hashtags.
  • Can also DM the users if granted with the permission.


Install Tweepy 

For all this we will need a Python library called Tweepy for accessing the Twitter API. We can install tweepy in three ways:

1. Using pip command

$ pip install tweepy

2. Clone the GitHub repository of tweepy

$ git clone
$ cd tweepy
$ pip install

3. Cloning the repository directly

$ pip install git+

Sign up for Twitter Developer Account

  • Sign up for a separate account for your Twitter Bot and then apply for Twitter Developer Account following this link 
  • Enter the necessary details and await for your mail confirmation. Once confirmed, click on Create an App option.
  • Enter the necessary details to generate the secret key and access tokens.
  • Copy the keys and keep them safe.

Developing the Twitter Bot

Make a file and paste the following code.


import tweepy
from time import sleep
from credentials import * 
from config import QUERY, FOLLOW, LIKE, SLEEP_TIME
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print("Twitter bot which retweets, like tweets and follow users")
print("Bot Settings")
print("Like Tweets :", LIKE)
print("Follow users :", FOLLOW)
for tweet in tweepy.Cursor(, q = QUERY).items():
        print('\nTweet by: @' + tweet.user.screen_name)
        print('Retweeted the tweet')
        # Favorite the tweet
        if LIKE:
            print('Favorited the tweet')
        # Follow the user who tweeted
        # check that bot is not already following the user
        if FOLLOW:
            if not tweet.user.following:
                print('Followed the user')
    except tweepy.TweepError as e:
    except StopIteration:

Now make another file to specify what should your bot do. Name it

Edit the #hashtag according to your choice and the like or follow option to either True or False.


# Edit this file as you like
# This is hastag which Twitter bot will
# search and retweet You can edit this with
# any hastag .For example : '# javascript'
QUERY = '# anything'
# Twitter bot setting for liking Tweets
LIKE = True
# Twitter bot setting for following user who tweeted
# Twitter bot sleep time settings in seconds. 
# For example SLEEP_TIME = 300 means 5 minutes.
# Please, use large delay if you are running bot 
# all the time so that your account does not
# get banned.

Next make a file and paste your access tokens carefully in between the single quotes ‘ ‘.


# This is just a sample file. You need to
# edit this file. You need to get these
# details from your Twitter app settings.
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''


Run the file from your Command Prompt/Terminal with this command.

$ python

And it works!!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!