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 aTwitter Bot using Python.
Python as well as Javascript can be used to develop anautomatic 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 alsoDM the users if granted with the permission.
Requirements
Install Tweepy
For all this we will need a Python library calledTweepyfor accessing theTwitter 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$ cd tweepy$ pip install
3. Cloning the repository directly
$ pip install git+https://github.com/tweepy/tweepy
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 linkhttps://developer.twitter.com/en/portal/petition/essential/basic-info
- Enter the necessary details and await for your mail confirmation. Once confirmed, click onCreate an Appoption.
- Enter the necessary details to generate thesecret key and access tokens.
- Copy the keys and keep them safe.
Developing the Twitter Bot
Make a filetwitterbot_retweet.py and paste the following code.
Python3importtweepyfromtimeimportsleepfromcredentialsimport*fromconfigimportQUERY,FOLLOW,LIKE,SLEEP_TIMEauth=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)fortweetintweepy.Cursor(api.search,q=QUERY).items():try:print('\nTweet by: @'+tweet.user.screen_name)tweet.retweet()print('Retweeted the tweet')# Favorite the tweetifLIKE:tweet.favorite()print('Favorited the tweet')# Follow the user who tweeted# check that bot is not already following the userifFOLLOW:ifnottweet.user.following:tweet.user.follow()print('Followed the user')sleep(SLEEP_TIME)excepttweepy.TweepErrorase:print(e.reason)exceptStopIteration:break
Now make another file to specify what should your bot do. Name itconfig.py
Edit the#hashtag according to your choice and the like or follow option to eitherTrue or False.
Python3# 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 TweetsLIKE=True# Twitter bot setting for following user who tweetedFOLLOW=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 filecredentials.py and paste your access tokens carefully in between the single quotes ' '.
Python3# 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 thetwitterbot_retweet.py file from your Command Prompt/Terminal with this command.
$ python twitterbot_retweet.py
And it works!!
Make a Twitter Bot in Python