How to make a Twitter Bot in Python?
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.
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')
# Favorite the tweet
if LIKE:
tweet.favorite()
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:
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.
# Edit this config.py 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
FOLLOW = True
# 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.
SLEEP_TIME = 300
Next make a file credentials.py 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 = ''
Deployment
Run the twitterbot_retweet.py file from your Command Prompt/Terminal with this command.
$ python twitterbot_retweet.py
And it works!!