Building a Cyclone Alert Monitoring System using Checkly, Playwright and Node.js
In today's world, real-time information is crucial to keeping people safe and informed, especially when it comes to natural disasters. Cyclones, for example, can be particularly devastating, and receiving timely alerts is crucial.
In this article, we will build a simple Cyclone Alert Monitoring System using Playwright and Node.js that fetches alerts from the National Weather Service API filters out repeated alerts, and sends an email notification when a new alert is issued.
Prerequisites
Before we begin, make sure that you have the following installed on your machine:
Node.js v14 or above
A text editor of your choice
A working email account
Setting up the project
Create a new Node.js project by running the following commands in your terminal:
mkdir cyclone-alert-monitoring
cd cyclone-alert-monitoring
npm init -y
Next, install the required dependencies by running the following command:
npm install playwright axios dotenv nodemailer nodemailer-sendgrid-transport --save
This command installs Playwright, a Node.js library for automating web browsers, Axios, a promise-based HTTP client for making API requests, dotenv, for managing environment variables, nodemailer, a module for sending emails, and nodemailer-sendgrid-transport, a transport plugin for nodemailer to use SendGrid as a service.
Create a new file in the project directory and name it index.ts
. This will be our entry point for the project.
Fetching Cyclone Alerts
The first thing we need to do is fetch Cyclone alerts from the National Weather Service API. We will use Axios to make a GET request to the API, which returns an array of alert objects.
const API_ENDPOINT = 'https://api.weather.gov/alerts/active?area={state}';
const STATE_CODE = 'IN'; // Replace with desired state code
const fetchAlerts = async () => {
const response = await axios.get(API_ENDPOINT.replace('{state}', STATE_CODE));
const data = await response.data;
return data;
};
Here, we define the API endpoint and the state code for which we want to fetch alerts. We create a fetchAlerts
function that sends a GET request to the API with the state code included in the URL. We then return the response data.
Checking for New Alerts
Next, we need to check for new alerts. We will use a checkAlerts
function that compares the latest alert ID with the previous one. If a new alert is found, we will send an email notification using the sendAlertEmail
function that we will define later.
let latestAlertId = null;
const checkAlerts = async () => {
const alerts = await fetchAlerts();
const newAlerts = alerts.features.filter((alert) => alert.id !== latestAlertId);
if (newAlerts.length > 0) {
latestAlertId = newAlerts[0].id;
sendAlertEmail(newAlerts[0]);
}
};
We create a variable latestAlertId
and initialize it to null
. This variable will store the ID of the latest alert that we fetch. We define a checkAlerts
function that first calls the fetchAlerts
function to get the latest alerts. We then use the filter
method to remove any alerts that have the same ID as the latest alert. If there are any new alerts, we update the latestAlertId
variable to the ID of the first new alert and call the sendAlertEmail
function with the new alert object.
Sending Email Notifications
To send email notifications.
Subscribe to my newsletter
Read articles from Jackson Kasi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jackson Kasi
Jackson Kasi
a simple man