I wrote a bot which Tweets quotes by Uncle Iroh for you to find calmness amidst the chaos of Twitter.
This project uses Twitter's Python API (Tweepy) and a few lines of Python code to achieve this. The complete code for the bot is in the repository linked in this writeup, and the writeup aims to give a brief overview of how it was written.
Getting StartedTwitter's API is slightly different from other APIs I have used, it requires you to register your app with Twitter beforehand. This is probably to make sure that you're not doing anything malicious with the data on Twitter. To register an app go to this link and follow the steps. This is the developer portal for Twitter, and you must sign in to the Twitter account you want to be associated with the bot first.
Depending on the permissions you require, Twitter may require longer to authenticate your app. In this case, I only requested read and write access - though I really only need write access.
Once your app is authorised, you will need to retrieve the API keys. This can be done by going to the developer dashboard, clicking on the project you have just made and clicking the key symbol on the right of it. This will take you to a page as shown below:
Click on 'view keys' and this will reveal your API key and API key secret - this is what authorises your Python script and allows access to this Twitter account so don't share it with anyone or check it into version control!
DependenciesWriting the bot requires a few external libraries, some of which are available by default but others (like Tweepy) you have to install using a package manager such as pip. The imports for this script are:
The script consists of many functions which will be explained in this writeup. The functions are accessed in the following order:
The code for authorising the bot is as shown below:
Line 29 makes an object which contains all the credentials for authorising the API. In my case, I set the API key and secret key as environment variables, but you can also input them directly. Using environment variables for keys is good practice as it prevents you from leaking them by mistake.
Lines 31 to 36 open up a browser window and ask you, the user, to authenticate the app by logging into the twitter account you're trying to link to the API. Following a successful login, Twitter will display to you a unique code which you must provide to the script. In this case, I have written the script so that it prompts for the code to be inputted in the command line.
Line 39 uses the unique code to finally authorise the app for the particular Twitter account!
As mentioned before, the 'auth' variable is an object which contains all the credentials required to login to the Twitter account using a Python script. I wrote this object to a text file so that I would not have to authenticate the script every time I ran it. This is especially useful as it means I could set up the script on a scheduler to run every day, making it truly autonomous.
The authorisation credentials are read in using the following function:
This completes the authorisation and returns the variable 'api' to the main script.
The QuoteI previously used some web scraping to retrieve a list of iconic Uncle Iroh quotes and put them in a text file called 'quotes.txt'. From here I could read the quotes line by line using vanilla Python. The function to get quotes is shown below:
The first line of the text file is an integer which acts as an index to which quote should be tweeted next.
TweetingNow that we have authorised the bot, and retrieved the quote to be tweeted, all that's left is to tweet the quote. This is performed by the following lines:
Line 55 does the tweeting.
I appended hashtags to the end of the quote that I retrieved so that it could also be discovered by the community on Twitter.
Finishing touchesNow that my bot was able to tweet one tweet at a time, I needed to schedule the script to fire once a day. I originally had the script hosted on a Raspberry Pi, where I used cron (a Linux scheduler) to tweet once a day - but I have since moved the script to AWS EC2 protocol (which also uses cron)
The layout of the code is specific to my use case, if you wanted to the script to run constantly instead of just once a day, you could bypass the whole section on saving the authentication credentials and just authenticate the bot in the script once! In fact, it is definitely risky saving the credentials without a hash table or encryption - but as this is not safety-critical or a serious project, I decided to leave it in plain text.
Comments
Please log in or sign up to comment.