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.
- 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.
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.
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.
Next make a file credentials.py and paste your access tokens carefully in between the single quotes ‘ ‘.
Run the twitterbot_retweet.py file from your Command Prompt/Terminal with this command.
$ python twitterbot_retweet.py
And it works!!
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.