Namaadhu Vaguthu Bot
Another bot, but this time it's useful for a whole country.
Ramadan 2019 is only a few days away and I thought I'd make something useful for people.
For the tutorial, go here.
Another bot, but this time it's useful for the whole country. You get to select what particular island's prayer times you want, and you can even add your location to the bot and it will remember your location, and send you the prayer times for the nearest island to you, getting rid of the slow process of going through all the atolls to find the island you want prayer times for. But enough about that. Let's see what I went through to get it where it is now.
Around this time last year, I had found and API that was dedicated to prayer times for the whole of Maldives. But this was the year I decided that I would finally integrate it to a working bot.
In the beginning, I did up the whole bot using the API that I had found. This made the bot to perform slower than I would have liked, and it would have meant user's would feel frustrated as it took so long to complete each operation. I did API requests to get all the atolls of the country, all the islands of the atoll and the prayer time for the specific island that was selected from all the previous operations. Every single one of these operations took over 1.2 seconds to complete. From sending the command from the bot and receiving a reply took 1.2 seconds. And when you account for user input time, it becomes even longer (depending on some users) and it's just a bad experience for the users.
I later found out that the prayer time API was on a shared host, meaning there would have been a lot of other websites or services that were running on it. I had to ditch making API requests cause it took so much of the users time.
For months, I kept thinking for ways to speed up the bot and data fetching process. I thought of caching the data that I get from the API and slowly get all the data for the islands that were being requested. But that would have taken a really long time and by then the API might have gone down.
And then it hit me, I'll just scrape the whole API and collect the prayer times for every single island there is. I wrote up a script that would go through every single atoll and get all the islands and it's associated prayer times. But this was when I ran into a roadblock. The system had almost every single year's prayer times. I thought that the API must have been huge and that's why it took so long. But a few minutes into research on how prayer times work in the Maldives, it was clear that all I need is the day and the month as the prayer times throughout the year don't change regardless of what year it is. Now ignoring the year, I scraped the API for everything.
After almost 1 hour of letting the scraper run, I ended up with 21 atolls, 194 islands and 70810 prayer times. And this was without taking years into the database. The database ended up being only 3.7MB (Megabytes), which was quite surprising as I was expecting somewhere around 10MB.
Once I got the data, I quickly changed up the bot code to use the database instead of making API requests. I went from 1-2 seconds of completing an operation using the API to less than one second using the database. In a perfect network environment, 4-8 ms from the user to Telegram and Telegram to my server. 1-2 ms to process the database search and 4-8 ms to send the reply back to the user. A substantial speed improvement from before.
This was all great and all, but it was just another dumb bot. I introduced a type of user "registration" where the user would send their current location and the bot remember it for future requests. This allowed the user to bypass all the steps from before and just send the specific command to automatically get prayer times for their location immediately.
It's been over 4 months ever since the newest updates were released in August 2019 and I have gotten almost a 100 users using the bot and out of which 44 of them have set their location for quick and easy prayer times. However, according to my statistics, only about 20 users come back every week.
The next thing I have planned for the Namaadhu Vaguthu Bot is to have a subscription service for the users, where they allow the bot to send them prayer time alerts, for their specific island. This will be implemented for Ramadan 2020 but until then, I'll stay on the search for efficient database queries to make this work for any and all users that I might receive during Ramadan 2020.
The bot runs 24/7 on my server that I have for this, and it has been running without a hitch for a really long time. This is one of the most stable bots I've ever written and I don't see it crashing on its own anytime soon and I intend to keep it that way. Now that I am not API dependent, I don't have to worry about shutting down the service just cause the API went down.