Let’s build a python twitter bot (Part 1)
If you are a heavy twitter user you might have noticed that to become popular you need to be constantly be active on the website for long hours. Not everyone has the time to do so on the daily basis. So what is the solution? What if I tell you that it is possible to be active on your favourite website even when you are not even near your computer or your phone and you can write the code for it yourselves. Here is where twitter api comes to our rescue. So this is a three part blog. This is part 1. Here I will try to explain what are we trying to achieve and how to setup the development environment. Second part will deal with increasing the features of the bot (make it smarter). If you have some experience in development, then you will probably enjoy part 2 and 3 more. Third part will be deploying it and linking it to a database. We can make dataset out of this and run some analysis. Possibilities are endless. But let’s start from the beginning. I hope all of my readers are familiar with basic Python 3. Even if you are not, don’t worry. I will explain my code and help you understand it as we proceed.
First things first. Make sure you have Python 3 installed. If it is not installed download it from here. If you are a linux/unix user you don’t need to do so, windows users also need to set path to python directory in environment variables.
So let us setup our development environment. Open terminal and install virtualenv for python by writing
pip3 install virtualenv
After installation move to the directory where you want to setup the bot. You should use cd to move to that directory from terminal. Now in terminal write
This code will create a virtual environment for our bot, named venv. Let’s activate this virtual environment. If you are a linux/unix user write
In windows, you’ll need to write
Now you should see
(venv) written at leftmost position. This means we are in virtual environment. Creating virtual environment is advised by all developers as it allows you to install different versions of the same package when need arises so. In this virtual environment, you’ll observe nothing is installed. For this part we are just going to need tweepy. You can install tweepy by following code in terminal:-
pip3 install tweepy
After you are done with that, let’s do easy parts first and create a twitter app. Go to https://apps.twitter.com and sign up with your twitter account. Once you are done with that just create a new app. Fill the details and copy-paste your consumer key, consumer secret, access token, access token secret in a file let us call it credentials.py.
#Paste inside the quotation marks
After you are done with that save and close this file. We won’t be opening that from text editor anymore. Make sure you don’t reveal these codes to anyone because one can use them to impersonate you. Therefore, we will use these values in our main code from credentials.py.
Now we will write a simple code to search twitter for a keyword and retweet that tweet. This will be based on REST Api of Twitter api. Wondering what’s REST api? Well REST stands for REpresentational State Transfer. It is an approach to communications in Web Services. And API is Application Program Interface. What you need to understand is that we do not need to install libraries or software to take advantage of a REST api design. You might ask why did we install tweepy then? Answer to that question is very simple. Tweepy is framework which has a lot of in built functions which take advantage of twitter api very efficiently. We would have written the code for those functions anyway. But since they already have done it for us why waste time and effort on that. Also tweepy has a very respectable arsenal of features.
Now the main code of our bot:-
This is the code for a basic twitter bot which gets tweets by searching for a user provided keyword. In the above code, we have used a cursor of tweepy which is very handy as it paginates the searches. Lines 6 and 7 are for authenticating our bot, basically telling twitter that it is the code for the app that we created before. Line 8 is the initialization of api which is what we will use from now on. Wait on rate limit simply means that the code will not crash even when it has reached the rate limit for that time duration, which is quite handy. Line 10 for loop is a cursor which searches for word “blockchains” in tweets. 50 represents number of tweets to be collected. So this loop will run 50 times. Now the three functions. Retweet, like and follow, are at 12,18 and 23 line. They are inside a try-except block to catch errors, if any. We don’t want to stop our bot due to an error do we? You might have wondered why are we using sleep(). It is because we are deliberately trying to slow down our code so that we don’t cross the rate limit of the twitter api. If we do cross the rate limit than our code won’t be able to do anything until the rate limit resets, usually 15 minutes. And if you cross rate limit multiple times, your app might get banned, and your tokens will get revoked. So tread carefully. To know more about rates:- https://developer.twitter.com/en/docs/basics/rate-limits
As I mentioned beforehand, it is not complete. We have tons of features that we need to add to it and then deploy it. I tried to keep it as simple as possible and this bot is nothing compared to what our final bot will be. In next part I will write more code and we will run the bot on a server so it truly becomes an automated task.
I will try to finish this series as soon as possible. Please be patient. If you liked this blog press the like button. If you did not love it, tell me about in comments section, I appreciate all kinds of feedback, and if you have any doubts or issues, ask in the comments section. I will try to help you all out.
Hope to see all you in part 2. 😄