Forem

Cover image for Getting daily SMS alerts about COVID-19 using Node.js and Twilio
Ishaan Sheikh
Ishaan Sheikh

Posted on • Edited on

4

Getting daily SMS alerts about COVID-19 using Node.js and Twilio

Visiting a web page daily for COVID-19 data is a tedious task especially if you are a lazy programmer like me😄. In order to solve this problem, I have created a notification system that sends me data about worldwide Coronavirus cases daily at a specified time.

In this tutorial, I'm going to show this how you can make your own system like this one.

Setting up Twilio account

In order to send a message, you need Twilio credentials. Go to Twilio console and get the Twilio number which we will use to send SMS using API.

From your dashboard, you need your ACCOUNT SID, AUTH TOKEN, and TRIAL NUMBER, copy them and save them in a .env file in your project's root directory.
It should look something like below

ACC_SID='your-account-sid'
AUTH_TOKEN='your-auth-token'

FROM='your-twilio-number'

# Also add your number here (recommended)
TO='your-number'
Enter fullscreen mode Exit fullscreen mode

Sending a message

Now we need to install Twilio package for node.js

npm install twilio
Enter fullscreen mode Exit fullscreen mode

Now let's test our credentials by sending a message.

const accountSid = process.env.ACC_SID;
const authToken = process.env.AUTH_TOKEN;

const twilio = require("twilio");
const client = new twilio(accountSid, authToken);

client.messages
    .create({
      body: "Hello World!",
      to: process.env.TO,
      from: process.env.FROM
    })
    .then(message => console.log(message.sid));
Enter fullscreen mode Exit fullscreen mode

Now run the file using

node index
Enter fullscreen mode Exit fullscreen mode

If all goes well you'll receive an SMS with the text "Hello World!"🙌

Getting COVID-19 data

We will use this API to get the latest data about COVID-19.

To get the data in Node.js we will use the request library since I found it quite user-friendly.

npm install request
Enter fullscreen mode Exit fullscreen mode

To send data in the SMS

request(
    "https://covidapi.info/api/v1/global",
    { json: true },
    (err, res, body) => {
      if (err) {
        return console.log(err);
      }

      var result = body.result;

      // Format message
      var msg = `\n Coronavirus Stats (IND): 
                \nConfirmed: ${result.confirmed}
                \nDeaths: ${result.deaths}
                \nRecovered: ${result.recovered}
            `;

      sendNotification(msg);
    }
  );


// Send message
function sendNotification(msg) {
  client.messages
    .create({
      body: msg,
      to: process.env.TO,
      from: process.env.FROM
    })
    .then(message => console.log(message.sid));
}
Enter fullscreen mode Exit fullscreen mode

Setting up a cronjob

in order to set up a cronjob in Node.js, we can use a library called node-cron

npm install node-cron
Enter fullscreen mode Exit fullscreen mode

Now we have to run the above code every day at 8:00 AM (say). Wrap the above code to send SMS every day at 8 AM with the latest data.

const twilio = require("node-cron");

cron.schedule("0 8 * * *", () => {
  request(
    "https://covidapi.info/api/v1/global",
    { json: true },
    ...
    ...

});
Enter fullscreen mode Exit fullscreen mode

Bonus

Let's also add a reminder to wash our hands every two hours. We can add another cronjob to send SMS

// Cron job to send message every 2 hour from 8-23 daily
cron.schedule("0 9-23/2 * * *", () => {
  sendNotification("Its time to wash your hands 🖐");
});
Enter fullscreen mode Exit fullscreen mode

Below is the GitHub repository where you can find all the code. Also, give it a star⭐ if you like.🤗

GitHub logo frikishaan / corona-notification

An app that sends notification (SMS) every day about number of cases using Twilio API.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (1)

Collapse
 
m121 profile image
Mateo Perez Salazar

Thank you!

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay