loading...

COVID-19 Helper Whatsapp Bot - A Twilio Hackathon Submission

alessandrojcm profile image Alessandro Cuppari ・4 min read

What I built

Category: Interesting Integrations

I set up to build a Whatsapp Bot using Twilio Autopilot which would help the user with several tasks in his daily quarantined life; several ones stayed just as ideas (hows that for feature-creep huh?) and my bot got the following function:

  • Statistical data of the COVID19 pandemic by country:
    • "How are they doing in Spain?"
    • "I'd like information on the USA"
  • Historical data of the COVID19 pandemic by country:
    • "How was Ireland doing on march 15th"
  • A self-screening process (with a little help from AI):
    • Here, the bot will ask the users if they are feeling the most common symptoms of COVID-19
    • Then, along with other variables (do they live in a country with confirmed cases? have they had contact with someone that looks ill?), would advise the users if they should seek medical attention
    • Of course, this self checker is merely informative, by no means replaces the diagnosis of a health professional

But first, show me dah GIFS!

greeting
latest data query
historical query
screening query

Link to Code

You can join the sandbox by sending a Whatsapp message to: +1 (415) 523-8886, first text join using-similar and then say hi!

GitHub logo alessandrojcm / covid19-helper-bot

A helper Whatsapp Bot for COVID-19 related information

COVID19 Helper Bot

buddy pipeline DeepSource buddy pipeline

A Whatsapp Bot to help get information about COVID19, aiming to participate on the Twilio & Dev 2020 Hackathon.

Using Twilio Autopilot and Python 3.8

Stack

  • Python3.8.2
  • Poetry
  • Fastapi
    • Pydantic
  • Fauna DB
  • Hosted on Heroku
  • Pytest
  • Loguru
  • Requests
  • Sentry
  • Click

Kickstarted with: https://github.com/Dectinc/cookiecutter-fastapi

Project structure

Files related to application are in the app or tests directories Application parts are:


├── app
│   ├── api - Api routes
│   │   └── routes
│   │       └── autopilot - Twilio Autopilot Dynamic actions
│   ├── core - Critical configuration (sessions, logging, etc)
│   ├── custom_routers - FastAPI custom routes
│   ├── error_handlers - Custom error handlers
│   ├── middlewares - FastAPI/Starlette middleware configuration
│   ├── models - Pydatic models
│   ├── scripts - Helper scripts (mainly cli stuff)
│   ├── services - Utilites for interaction with external apis and logic too heavy for the routes
│   └── utils

The project

The one million dollar ideas

So first of all, when I saw the hackathon announcement I knew I had to participate. Right away I also knew that I wanted to make a Whastapp Bot and I wanted to do it with Python. But alas, what should the bot do? I'd started brainstorming ideas like crazy, here are some of then:

  • Query a specific statistic (ie: how many recoveries in Spain)
  • Query by more specific location (ie: how many cases in San Francisco)
  • Take the user's location and show open grocery stores near then
  • Send the user the latest news for their country daily
  • And there are a couple more left out

So, pretty feature creep huh? Some of those features were left out because of lack of time and others for technical reasons (for example, the Whatsapp API does not let you take the user's location; at least not yet). But I knew that I had to implement the self-screening process.

The stack, oh the stack

well, I knew more or less what I wanted to do, now with the stack. I wanted to it in Python and I wanted to do it with Fastapi, since Twilio has a helper library for Python that was a no brainer.

Now, the fun part was my database of choice: FaunaDB. Why did I choose it? Well, first of all, it has a generous free tier, and second, they advertise heavily for being a solution for the JAM stack; so I was curious to see how it fitted with a more traditional use case (spoiler: it can, but with some gotchas).

So I set sail with my shiny stack, and boy it was a ride. First, because it was my first time with an ASGI framework; and thus I had to wrap my head around Python coroutines and async routes. Things that you would normally do in WSGI frameworks are different here (middlewares, for example); but, overall, I had a blast working with Fastapi: I learn a lot in the process and was nice to work with a framework designed for the modern Python.

But Fauna, well that was another beast. I come mainly from a traditional relational database background, so jumping to NoSQL was a challenge of its own; there were a lot of gotchas in the process: where are my primary keys? If this value is unique why does my query return an array? Where are my migrations? How can this record be repeated if all its fields are the same? And all kinds of surprises.

Last but not least, the bot

At this point, I was sure developing the bot would be the bigger challenge. At first, it seemed like so: I couldn't get the Autopilot requests to hit my API; it turned out it was a case of me skipping a little of pretty important documentation (specifically how to parse application/x-www-form-urlencoded requests with FastAPI). But, once I had that sorted out, the bot itself turned out to be a breeze to develop.

Additional Resources/Info

The APIS I used are:

  • Novelcovid API, an API that aggregates data from several sources. I choose the one from the John Hopkins Universite
  • Endless Medical API, AI-powered trained with real records and diagnosis that suggests outcomes based on a patient's symptoms. Thanks to Lukasz Kiljanek for kindly answering my inquiries!

Conclusion

Overall, this was a great project and not only got me to learn several things in the process; at first, I was a little disappointed that I couldn't squeeze more features in, but seeing all the new concepts I managed to grasp in only a month made it a worthy ride.

It also got my first post on DEV published! So that alone was worth it (I may be following up with more in-depth articles about the code 😉). So that's all, happy coding and keep safe!

Discussion

pic
Editor guide
Collapse
germmand profile image
Germán Aguilera

Uffffff Mano, ¿por qué tú eres tan mostro? ❤️😍