DEV Community

Marcelle Vargas
Marcelle Vargas

Posted on • Originally published at marcellecode.Medium on

How to make a Health Site Bot


banner how to make a health site bot

When we create a website be it for a client or personal, it’s important to guarantee the service is up. Thinking help with this, that’s the objective of this article. With that in mind, let’s create a bot using Node.js to let us know when our site responds to HTTP Status other than 200.

Step 1: Start the project

The first step of our project is to create the folder of our bot, so insert this line of code below on your terminal

mkdir health-site-bot && cd health-site-bot
Enter fullscreen mode Exit fullscreen mode

Now we’ll run a command for Node.js to create the basic structure necessary to run the project

npm init -y
Enter fullscreen mode Exit fullscreen mode

If everything is ok, now we’ll be creating the two files to define our bot.

touch config.js && touch server.js
Enter fullscreen mode Exit fullscreen mode

Step 2: Install the Libraries

Run all the lines below on your terminal

npm i dotenv && npm i express && npm i nodemailer
Enter fullscreen mode Exit fullscreen mode

Step 3: Editing Config.js

Let’s import the dotenv and path libraries, and export the global variables EMAIL and PASSWORD.

const dotenv = require("dotenv");
const path = require("path");

dotenv.config({
  path: ".env",
});

module.exports = {
  EMAIL: process.env.EMAIL,
  PASS: process.env.SENHA
};
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a .env file

The .env file is a file that saves information like credentials. With this method, we can make configurations for the production and development environments just set what .env file I want to use.

#.env
EMAIL=email@teste.com
PASS=123456
Enter fullscreen mode Exit fullscreen mode

How the .env file serves to store credentials it’s not so good if you save this on your repository. In the next step, we’ll configure the .gitignore to remove this file from our repository and prevent accidental commits.

Step 5: Configure .gitignore file

In this file we’ll add a name of two files for git not add when we make a commit.

touch .gitignore

node_modules
.env
Enter fullscreen mode Exit fullscreen mode

Step 6: Create a serve.js file

The serve.js is a file we use to configure our bot. So, the first thing we’ll do is, import the necessary libraries.

//serve.js
const nodemailer = require("nodemailer");
const config = require("./config.js");
const moment = require("moment");
const https = require("https");
Enter fullscreen mode Exit fullscreen mode

After that, we create a constant variable for storing the URL application we want to monitor.

const url = "https://www.google.com/"
Enter fullscreen mode Exit fullscreen mode

With this URL defined, we can create a function for making requests HTTP to verify the status. If this status is not equal to 200, we’ll call a function to notify us.

function healthCheck() {
  https.get(_url, function (res) {
    if (res.statusCode !== 200) {
      _sendMail();
    }
  });
}
Enter fullscreen mode Exit fullscreen mode

In this tutorial, our bot notifies us by e-mail. So, let’s create a function called “_sendMail()”

function _sendMail(statusHTTP) {
  var transporter = nodemailer.createTransport({
    host: "HOST_EMAIL",
    auth: {
      user: config.EMAIL,
      pass: config.SENHA,
    },
  });
}
Enter fullscreen mode Exit fullscreen mode

In the above code, we use a function from nodemailer to create a connection with e-mail host. The username and password used in this file are imported from the .env file, they are the email account data we will use to send the notifications.

var mailOptions = {
    from: "YOUR_EMAIL",
    to: "EMAIL_RECEIVE",
    subject: `ERROR WITH ${url}`,
    text: `Oops! Your service is having problems.
           Problem description:
           Received code: ${statusHTTP}`,
  };
  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log(error);
    }
  });
Enter fullscreen mode Exit fullscreen mode

The last part of our script is to create a function for call _sendMail if the website return status different than 200.

function healthCheck() {
  https.get(_url, function (res) {
    if (res.statusCode !== 200) {
      _sendMail();
    }
  });
}

healthCheck();
Enter fullscreen mode Exit fullscreen mode

At this moment, the serve.js file is like this

const nodemailer = require("nodemailer");
const config = require("./config.js");
const https = require("https");

const _url = 'URL_APP';

function _sendMail(statusHTTP) {
  var transporter = nodemailer.createTransport({
    host: "HOST_EMAIL",
    auth: {
      user: config.EMAIL,
      pass: config.SENHA,
    },
  });
  var mailOptions = {
    from: "SEU_EMAIL",
    to: "EMAIL_DO_REMETENTE",
    subject: `ERROR WITH ${url}`,
    text: `Oops! Your service is having problems.
           Problem description:
           Received code: ${statusHTTP}`,
  };
  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log(error);
    }
  });
}

function healthCheck() {
  https.get(_url, function (res) {
    if (res.statusCode !== 200) {
      _sendMail();
    }
  });
}

healthCheck();
Enter fullscreen mode Exit fullscreen mode

MarcelleCode is digital content creator about web development

Top comments (0)