Back in 2016, when I was preparing for GRE, I was falling short of days to prepare properly, especially for the verbal section. I had to learn over a thousand new words in a couple of weeks and improve my vocabulary somehow really soon. And I did just that by building a Twitter bot.
With 8+ hours going in at work every day, there wasn’t much time left for myself to learn all that had to be done. It did not feel ethical to me to study for GRE while I was at work, so I did not do that. However, I noticed that even at work everyone was checking notifications on their phone. I also checked every single notification on my phone – be it WhatsApp, Gmail, or something else. This gave me the idea that perhaps I could utilise my habit of checking notifications for some good – If there could be a system that would push notifications with GRE words and their definitions to my phone, my habitual glance at the notifications would end up benefiting me.
So, I decided that I needed to build a GRE Words App.
Did it benefit me? Well, I scored a 325 / 340. So, I would say it did. 🙂
Now, I could have gone with building a full fledged Android App, but that would have required a lot more time and effort, and every minute was crucial for my preparation. If you’re giving GRE, you know this. People don’t put up vocabulary chrome new tab extensions, or word meaning screensavers for no reasons.
So, I settled for building a Twitter bot, and let twitter take care of pushing me the notifications. Moreover, it was also safe to say that even if my manager or peers saw my phone, no one’s going to judge me for some Twitter notification. 😉
This gave rise to @GREWordVocab on Twitter. If you’re preparing for your GRE exam, you can subscribe to this twitter account. And voila, every half an hour you’ll have a twitter notification on your phone with a GRE word and its definition.
What I found a very nifty feature was to #hashtag the word. So, I could just click on the hashtag and it would show me other tweets using that hashtag, which often served as a good way to know real world examples for that word.
The code for the twitter bot is completely open sourced and decently documented on Github. Any Pull requests, or additions, or issues to the code are welcome.
The rest of this post is about how I built that bot, and how you could build a similar twitter bot as well.
How to build the GRE Twitter Bot
So, essentially, we want to build a Twitter Bot that tweets GRE word and its definition.
Note : You can also use this post to understand how to otherwise build any sort of Twitter bot.
This means we require at the very least :
- A way to post tweets programmatically
- A list of GRE words
- A way to find definitions of a word programmatically
- Code that binds all the above things together
- A place to host the code that will be powering the twitter bot
So, let’s look at how each of these get solved.
A way to post tweets automatically
Twitter allows to create applications that can read / write tweets (and even DMs). This happens via their APIs. We’re going to utilise that. The following are the steps we need to follow to do the same.
i. Create an Email Account
A twitter account always needs an email account. So, to tweet anything, first thing first is have an email account. Gmail, yahoo, anything works.
ii. Create a twitter account
Just head on over to https://twitter.com/signup and sign up a new twitter account using the email id you created above. You will have to verify your email.
iii. Add your mobile number to your twitter account
This is unfortunately required if you wish to build a Twitter app / bot.
If your new twitter account is opened, go to Profile – > Settings -> Mobile. From there, you can add your Mobile number.
You will receive a SMS for verification and you need to verify using the same.
You can also remove your mobile number once you’re all set up and running.
iv. Apply for a twitter developer account
We actually need to “Create a Twitter App”, but if we try to do that, Twitter asks us to apply for a developer account first.
There are a few simple steps for this, which I’ve detailed in the following slideshow. Head on over to https://developer.twitter.com/apps to start.
v. Create a Twitter App
Now that we have a Twitter developer account, we can create a Twitter App. Anything and everything that is trying to access, modify or generate twitter data programmatically is called a Twitter App.
And it needs API Keys and Access Tokens to do so. Essentially, these are the things we also need to tweet using code.
Follow the following simple slideshow steps to create your app.
Congratulations! We’ve finally gotten the things we needed, and those are these credentials :
- API key
- API secret key
- Access token
- Access token secret
These can be used to tweet using a simple python program given you have python library Twython installed (hint : pip install twython)
A list of GRE words
A quick search on google led me easily to GRE words lists. However, upon reading a bit, I realised Manhattan ones and Princeton ones are said to be more accurate and complete.
So, I just downloaded these lists and used them in my app.
A way to find definitions of a word
Now, if I have the word, I need to have a way to find their definitions. I did a quick search as to which online dictionary websites have free APIs and wordnik came on top. Their pricing said that I could make a 100 API calls to them for free every hour, which was more than sufficient.
What made wordnik even better was that they had a python library already built. So, instead of writing API calls myself, I just had to install that package and call some methods.
Signing up was quite easy too. Just head over to wordnik and click on ‘Login or Signup’. Enter your desired username, password and email and verify the email. And you’re in!
Then head over to https://developer.wordnik.com/ and sign up for getting the API key. Just enter your username there. While they do say that it could take upto 7 days for your API key to arrive, for me it arrived in an hour [back in 2016].
You will receive a mail about when your wordnik API key gets generated. And you can always login back to wordnik and go to your profile to see it or regenerate/change it. It would look somewhat like this :
Once you have it, it’s simple to install wordnik library (pip install wordnik), and the code to fetch definition looks as simple as this :
Code that binds all the above things together
Alright, if you were able to understand the above two pieces of code, one of which finds definition of a given word, and the other which can tweet from the bot’s account, then it wouldn’t be very hard to understand what happens in the actual code here.
Essentially, the only differences are:
- API Keys and Access Tokens are all picked up as environment variables
- Randomly a file is chosen from among Manhattan or Princeton GRE Words list files
- Randomly a word is chosen from the file
- Definition of the word is found out by above logic in
- The definition is tweeted in a nice format in
A place to host the code that will be powering the twitter bot
Initially, this GRE Bot used to run on a RaspberryPi. You could just as well host it on any linux machine. I used a cron which used to just run this file every 20 minutes every day.
Step 1. Open crontab for editing
Step 2. Add an entry for executing this bot. If we had to tweet out a word every 20 minutes (Say, 10th, 30th and 50th minute of every hour from 9am to 10pm)
10,30,50 9-22 * * * /usr/bin/python /home/pi/GRE-BOT-Project/AwesomeGreBot.py
However, now I’ve adapted the repository to be Heroku compatible. Heroku for those of you who don’t know is a free service to host apps. It was especially famous for hosting python apps back in the day. So, this means I don’t need to do anything on my own hardware (RaspberryPi or any other machine). Heroku will take care of hosting my app 24/7, for free! 😀
How to make a simple python app Heroku compatible would probably take another blog post. However, I have documented a bit about it here.
So, essentially, with it being Heroku deployable, you could just fork my github repo, change the word lists, or anything you wish to do in the code, and deploy it on Heroku. You’ll just need to give it the github repository link and Heroku will auto-understand how to deploy the app, thanks to
runtime.txt files in the repository.
And we’re done
That’s all it is. Now,
- You could just use the existing bot and that would take no more than couple of minutes of subscribing to @GreWordVocab. Turn on the bell Notifications for every tweet to get notification for every word on your phone.
- Or, You could change the word lists by forking a branch, and then deploy your own version on Heroku. Heroku is free, and since the repository is heroku-compatible, the whole thing should take less than an hour to set up once you have Twitter and Wordnik credentials ready.
- Or, you could try building the whole bot yourself. It’s a good experience. 🙂 Besides this post, it would help you to checkout the Readme of my repository as well.
Lastly, apologies for this whole repository is still on Python v2.7. Back then it was considered ‘Okay’ to be on v2.7. If you’re up for it, make a Pull Request that makes it Python v3 compatible and I’d be all smiles to accept it. 🙂
Hope this post was helpful. Subscribe to my blog for more sick automation posts as well as articles on AI and Machine learning. I’m trying to restart my tech blogs and many more blogs are on the way. 🙂
Also share if you found it useful.
April 11, 2019 at 3:18 am
Good one brother 🙂
I have just started to get my hands dirty on github and iOS app development.
Would like to try making something similar using your post, but after I know enough.