Daily Covid Case Counts by Text using Twilio Functions
Tags: javascript, twilio
Published: 2020-08-10 16:53:05

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: https://www.twilio.com/referral/6M48j2

Step 2 - Buy a phone number. Once you have an account created, go to this link: 
https://www.twilio.com/console/phone-numbers/incoming 
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:
https://www.twilio.com/console/functions/manage
and click on the red circle with the plus sign to add a new one.  

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6p5f6jd8ehdpzx3khdon.png)

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 = "https://localcoviddata.com/covid19/v1/cases/newYorkTimes?zipCode=" + 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.date + ": " + 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) => {
      console.error(e);
    });
};

```


We are using localcoviddata.com'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
```

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](https://dev-to-uploads.s3.amazonaws.com/i/ycdu9euo0sl10or1uz2j.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/2ufukjgdlx4o5llm6vgj.jpg)