Aditya Bose

Software developer and part-time memelord

Twilio Hackathon 2020

Over the past month, I have been hacking around with the Twilio API for WhatsApp to create some simple bots by following their documentation and tutorials.

Twilio was hosting a hackathon throughout April on Dev to solve challenges in communication, and one of the themes was the ongoing Coronavirus pandemic. This seemed like a good opportunity to test out my ideas for a Covid Tracker as a WhatsApp bot.

We have already seen how we can import twilio to a python script to use it’s APIs to send messages to users.

In this project, we would create a Flask app and use Twilio’s twiml APIs to send meaningful information based on user queries.

Our original intent from the first post was to be able to get notified on the latest Covid-19 case stats. The easy solution that I wanted to implement was a Twitter bot that tweets every time it receives updated stats from it’s trusted data sources. Since I have yet not recevied Developer access for Twitter, I deceided to implement an information system related to Covid-19 over WhatsApp. It is not free to send self initiated messages, and to be able to send such messages, one needs to apply for a Business Account with WhatsApp which again goes through an approval process which again takes several weeks. We are gonna worry about all that later, and start building and testing right away using the Twilio Sandbox for WhatsApp.

My main objective for the Hackathon was to show daily and total Covid-19 cases in India. Using the location services of WhatsApp, we could also get nearby case statistics andn essential services information for users to have an idea of the neighbourhood they are in.

The project is developed using Python3.8 with the Flask web microframework, and uses Twilio’s Programmable SMS API for WhatsApp. Let us start by first installing flask and twilio in a new virtual environment, after which we can start developing by importing these dependencies in our code.

The next step is to source the data. For country level and total global Covid-19 stats, Worldometers is probably the most popular site on the Internet. They have paid licences starting $35/month to access their data APIs, but I didn’t wish to pay any money to get started with the project which is mainly an experimental one for educational purposes. So, using an open source MIT licence for fair and non-commercial use, I assumed I could safely scrape the publicly available data from their site and get all the country level stats at a given moment.

Given below is a script that we could write to fetch the current stats from Worldometers on the global cases, deaths and recoveries:

response = requests.get('https://www.worldometers.info/coronavirus/')
doc = html.fromstring(response.content)
country_names = doc.xpath(
    '//table[@id="main_table_countries_today"]/tbody/tr/td[2]/a[contains(@href, "country/")]/text()')
print(country_names)

The result will be a list of names of countries as they are written in the Worldometers site in the main stats table. To scrape the website more reliably, I decided to use the lxml Python package to parse the site XML using xpath.

Luckily, getting India specific data is easier. The site covid19india.org provides a free and open JSON API which we can access using simple HTTP GET requests to get state level, district level, patient level, and overall stats. The data is crowdsourced and maintained by the volunteers involved with the project. Check out the API information page here.

We use the requests Python package and Python’s inbuild json module to send a request to an API and get response in a Python data type which will be the JSON equivalent of the data like a dict.

A typical request and response code would look something like this:

response = requests.get(api_url)
return response.json()

When users send their location in their message, we obtain the location coordinates from the request.values object of the request payload that WhatsApp receives. To encode and decode locations and places, we can use a third party geolocation service like Google Maps API. For the purposes of the hackathon, I used the free and public Nominatim Open Street Map API from the geopy Python package. Using the location data, we were able to provide case stats by district, zone information, as well as essential services available nearby.

Using The Bot

The app is hosted for free on Heroku. You can start interacting with the bot right now by sending the message join came-told to Twilio’s shared WhatsApp number.

You can find the code for the project on Github along with the instructions to clone and run it locally with the Twilio Sandbox for WhatsApp using ngrok to expose the local instance of your app to Twilio’s webhook endpoint URL. You can also read my submission post on dev.to.

Stay tuned for more updates on this project!