How to make a Twitter Bot in Python?
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.
Requirements
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 https://github.com/tweepy/tweepy.git
$ cd tweepy
$ pip install
3. Cloning the repository directly
$ pip install git+https://github.com/tweepy/tweepy.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 https://developer.twitter.com/en/apply-for-access
- 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 twitterbot_retweet.py and paste the following code.
Python3
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(api.search, q = QUERY).items():
try :
print ( '\nTweet by: @' + tweet.user.screen_name)
tweet.retweet()
print ( 'Retweeted the tweet' )
if LIKE:
tweet.favorite()
print ( 'Favorited the tweet' )
if FOLLOW:
if not tweet.user.following:
tweet.user.follow()
print ( 'Followed the user' )
sleep(SLEEP_TIME)
except tweepy.TweepError as e:
print (e.reason)
except StopIteration:
break
|
Now make another file to specify what should your bot do. Name it config.py
Edit the #hashtag according to your choice and the like or follow option to either True or False.
Python3
QUERY = '# anything'
LIKE = True
FOLLOW = True
SLEEP_TIME = 300
|
Next make a file credentials.py and paste your access tokens carefully in between the single quotes ‘ ‘.
Python3
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
|
Deployment
Run the twitterbot_retweet.py file from your Command Prompt/Terminal with this command.
$ python twitterbot_retweet.py
And it works!!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...