DEV Community

Brett Evanson for Twilio

Posted on

Daily Covid Case Counts by Text using Twilio Functions

My wife is pretty concerned with Covid. She's technically high risk, so we've been super careful. One thing that means is she is on top of the case counts in our area. To help her keep tabs on this I wanted something that would easily get daily case counts whenever I wanted. I figured I could find an API where I could pull case counts from, then hook a Twilio phone number up to that so I could text in a zip code and use Twilio Functions to process it, hit the API, and pull back case counts.

So that's what I'm going to walk through today. I'll share code snippets and screenshots of everything along the way.

Step 1 - If you don't have a Twilio account already, let's get an account set up. If you want $10 to get started and play around, use this link to sign up:

Step 2 - Buy a phone number. Once you have an account created, go to this link:
and then click the red and white plus sign to get a new phone number. Don't worry about setting up the number, we'll do that after we set up the Function.

Step 3 - The Twilio Function. To create a Twilio function go here:
and click on the red circle with the plus sign to add a new one.

Alt Text

Paste the code in below. I'll break it down into chunks and explain what it's doing.

const https = require('https');
exports.handler = function(context, event, callback) {

    let url = "" + event.Body + "&daysInPast=4";

    https.get(url, (res) => {
        newstring = "";

        res.on('data', (d) => {
            var obj = JSON.parse(d);

            obj.counties.forEach(county => {
                newstring = newstring + "County: " + county.countyName + "\r\n";
                county.historicData.forEach(data => {
                    newstring = newstring + + ": " + data.positiveCt + "\r\n";
            newstring = newstring + "\r\n";
        let twiml = new Twilio.twiml.MessagingResponse();
        twiml.message("Last 4 days Numbers: \r\n" + newstring);
        callback(null, twiml);

    }).on('error', (e) => {

Enter fullscreen mode Exit fullscreen mode

We are using's New York Times API. This takes a zip code and gives us the county data associated with that zip. Some zip codes span multiple counties, so we had to account for that in our loops through the response. The API returns a JSON object we parse into the "obj" variable. Loop through that object's "counties" attribute. This lets us print out the county name, and into the historicData attribute for the last 4 days counts.
So it would look something like this:

County: Salt Lake County
2020-08-02: 19408
2020-08-01: 19323
2020-07-31: 19086
2020-07-30: 18847

County: Utah County
2020-08-02: 7949
2020-08-01: 7808
2020-07-31: 7688
2020-07-30: 7584
Enter fullscreen mode Exit fullscreen mode

Take that string and pass it to the twiml.message function and return that in callback. This makes the Function kick back the correct response to Twilio.

Step 4 - The last step to building this is to connect the Function to the phone number you bought in step 2. On the phone number's settings page, set it to the Twilio Function where it says "A Message Comes In"

Alt Text

Finally, you can test it by sending a text message containing a zip code to your Twilio number and get current case counts for your area.

Alt Text

Top comments (3)

mapyourown profile image

It is very clear approach and useful but always having opt-in and opt-out option will be good from compliance reason and sending sms cost money and while people text to get information at the same time it is a good idea to build the relationship for future communication and other information out reach. I always recommend all my client to ask the users to text yes before getting any communication from the Twilio number.

jamesqquick profile image
James Q Quick

I might have missed this. Did you cover how to have this automatically triggered every day?

jamesqquick profile image
James Q Quick

Super cool. Love this!