Schedule your Azure Mobile Service Pinger - Part 2

This is a continuation of  my previous blog. As an update to the Scheduler script I have incorporated the script with the Email and  sms module. I have used Sendgrid to send the email and Smslane to send the sms.

The scenario is whenever the url or web service is down a sms and an email will be sent to the administrator.

Following is the script

function Pinger() {
       
    //Import the modules that you will require to perform this request
   
    var req = require('request');
    var util = require('util');
    var sql;
    var responseStatus;
    var time = new Date();
    var SendGrid = require('sendgrid').SendGrid;
   
    //Send request to the url which you want to monitor. Note this is a get request
    req.get({
    uri:"http://www.microsoft.com/",
    headers:{'content-type': 'application/x-www-form-urlencoded'},      
   },
  
   //Write the callback module for the same
   function (error, response, body) {
   if (!error && response.statusCode == 200) {
       //If the response is successful insert the result with the status code in the TodoItem table
       responseStatus = "Success"; 
                
    }
    else
    {
        //In case of failure insert the failure response code in the TodoItem table
        responseStatus = "Failure";

        //Module to send an alert email to the administrator
        sendmail();   

        //Module to send an alert sms to the administrator
        sendsms();   
    }
   
    //Insert command in SQL
    sql = "INSERT INTO TodoItem (text) VALUES ('"+time.toDateString()+" "+time.getHours()+":"+
       time.getMinutes()+":"+time.getSeconds()+" Status:"+responseStatus+" , Response Code: "+
       util.format('%s',response.statusCode)+"')";
      
    //Execute the SQl statement  
    mssql.query(sql, {
        success: function(results){
            console.log(results);
                    }               
    })   
    }
);

//Send an email
function sendmail(){
    var sendgrid = new SendGrid(<<Sendgrid username>>, <<Sendgrid password>>);      
    sendgrid.send({
        to: <<recepient email address>>,
        from: <<sender email address>>,
        subject: 'URL Monitoring status failed',
        text: 'Failure'
    }, function(success, message) {
        // If the email failed to send, log it as an error so we can investigate
        if (!success) {
            console.error(message);
        }
    });   
}

//Send an alert sms in case of a failure
function sendsms(){
    var request = require('request');
   
    //Request sent to smslane api to send sms
    request.get({
        url: "http://smslane.com/vendorsms/pushsms.aspx?user=<<smslane username>>&password=<<smslane password>>&msisdn=<<recepient mobile number>>&sid=WebSMS&msg=SMS&fl=0",
        headers: {'content-type':'application/x-www-form-urlencode'},
        body: "SMS from AMS"
       
    },
     function(err, resp, body){
        console.log(body);
    });
}
}

Hope this helps. Azurify your holidays with Node.JS :)

Comments

Post a Comment

Popular posts from this blog

Firebase authentication with Ionic creator

Big Data - SWOT Analysis

LINKEDIN api call using NODE.JS OAUTH module