The Story of my First Bot

Hey there, ya I know, I missed a previous week's post. I started playing Rocket League and was too busy to do any work πŸ˜…. If you don't want to read the complete post. Here is the TLDR:

  • It is programmed in python, the language of flexibility.

  • It calls Twitter API every 10 seconds for new twitter mentions.

  • If there is a new mention, it will add all the necessary parameters into a Pandas Dataframe.

  • It will be pinging COWIN servers for every unique pincode in the dataframe for vaccine slots for next 3 days every 3 seconds.

  • If it founds a match with parameters, it will reply to the tweet with number of available slots, type of vaccine available and dates of said slots.

  • Check the bot out in action on Twitter.

  • Check out the under the hood of bot on GitHub.

Just your friendly neighbourhood bot that will notify you once there is an open slot for #CovidVaccine near you. Tweet at me with subscribe \ \<18+/45+\> and I will keep you updated. Follow the tweet format in the image.

Retweet and Follow for more updates! πŸ™

β€” CowinHelper (@getmyslot) May 5, 2021

If you are here, I suppose you want to know more. Let's get started with how I got the idea for such a bot. I wanted to get vaccinated and was playing out with an idea for a simple python script that would alert me if a slot opened up. At that point I came across this tweet.

If you are 18+ and live in Bangalore, the only way to get vaccine appointments is to use python scripts that ping CoWin's public API and setup notification alerts via Telegram, Twitter or SMS.

Yes, the vaccination drive is now officially a hackathon.

β€” Praveen Gopal Krishnan (@peegeekay) May 3, 2021

I thought instead of making a python script that would just be helping me, why not Β go a step further and create something that could help people with no knowledge of how to make a bot. Tweet was the initiator, now I have to figure out Β to overall feasibility of the idea and architecture of the bot.

I had used Twitter API and Tweepy (the SDK that helped me easily call twitter's endpoint in python). The first thing I did was to check what are the requirements for making calls to Cowin API. I found using the endpoint which required pin code to get slots pretty handy and easy enough to integrate into my bot.

You might have came across the tweets (mostly from brands) that notifies you about some event if you like it or retweet it. I planned to use that behaviour for my bot. A user would tweet at the bot with their command \, their pincode and the age category they were looking for \<18+/45+\>. I sketched out how every thing should work on my iPad and got to programming.

I usually start my python projects in Jupyter Notebooks, which makes it easier to prototype and code basic outline of the project. I broke down the bot into to two parts. A "front-end" which will interact with users i.e. handling all twitter mentions and a "back-end" getting updated slots using the COWIN Api and responding to tweets with available slots.

The communication between the APIs and processing the data were the easy part, I was able to complete it in couple of hours of deep work. The real trouble began when I transferred my code from the Jupyter notebooks to python files. I wanted my bot to call the Cowin API every 3 seconds ( based on max calls allowed from a single IP Address.) and Twitter API every 10 seconds.

Also I didn't want any of the two calls to block the code execution.The only solution that I could come up with was using asynchronous function calls. And as a noob in multithreading, I don't know much about the how Async functions worked in Python. It took me around 2 days to finish the code after completely writing the logic in about 1. Hell of a ride!

My future plans include to write in logic for allowing people to unsubscribe ( as of now if some one tweets unsubscribe I manually go and delete that person in my database πŸ˜‚). I would love to have capability of allowing people to subscribe based on their district, and city. Also the most important part, transfer to an actual Database, Dataframe aren't the best to store long term data from what I have heard from my friends ( although I am frequently exporting it to csv).

If you want to make your own bot, I would refer this article. It helped me understand the logic of Cursors in tweepy.