Check the response code of multiple websites, then send an email and text message if the response code >299 (generally unfavourable).

We never expect website issues, but they are often a reality. The faster we can identify issues, the faster we can mitigate and fix. An email alert is great, but it doesn’t get much more reliable than a text message.

Link checking and email capabilities are built into AdWords scripts, but you’ll need to sign up to the Twilio API to send text messages.

An alert will trigger based on the following response codes:

  • 3xx Redirection
  • 4xx Client Error
  • 5xx Server Error

Setup

Setup is easy, just fill in email addresses, phone numbers, and Twilio API information.

Multiple links can be checked, but bear in mind an email and text will be sent per link. If 10 links have a response code of >200, 10 emails and text messages will be sent.

For the most up to date alerts, set to run hourly.

/**
* AutomatingAdWords.com - Link Checker with Email & Text Notification
*
* This script will check the response code of links provided
* It will then email and text if the response is >299
* Go to automatingadwords.com for installation instructions & advice
*
* Version: 1
**/
function main() {
  
  //START OPTIONS
  
  
  //Links to check (comma separated list)
  var links = ["http://www.website1.com", "http://www.website2.com"];
  
  //Email addresses to alert (comma separated list)
  var emails = ['example@example.com', 'example@gmail.com'];
  
  //Phone numbers to alert (comma separated list)
  var numbers = ["440000000000", ""]
  
  //Email any Twilio errors to
  var ERROR_EMAIL_ADDRESS = 'example@example.com';
  
  // The Twilio phone number or short code, as per the Phone Numbers Dashboard
  // https://www.twilio.com/console/phone-numbers/incoming
  var TWILIO_SRC_PHONE_NUMBER = '';
  
  // Your Twilio Account SID, see: https://www.twilio.com/console
  var TWILIO_ACCOUNT_SID = '';
  
  // Your Twilio API Auth Token, see: https://www.twilio.com/console
  var TWILIO_ACCOUNT_AUTHTOKEN = '';
  
  //END OF OPTIONS // THE GAME IS AFOOT
  

/**
 * Builds an SMS message for sending with Twilio and sends the message.
 * @param {string} dstPhoneNumber The destination number. This is a string as
 *     telephone numbers may contain '+'s or be prefixed with '00' etc.
 * @param {string} message The text message to send.
 */
function sendTwilioSms(dstPhoneNumber, message) {
  var request =
      buildTwilioMessageRequest(dstPhoneNumber, message);
  sendSms(request);
}

/**
 * Send an SMS message
 * @param {!SmsRequest} request The request object to send
 */
function sendSms(request) {
  var retriableErrors = [429, 500, 503];

  for (var attempts = 0; attempts < 3; attempts++) {
    var response = UrlFetchApp.fetch(request.url, request.options);
    var responseCode = response.getResponseCode();

    if (responseCode < 400 || retriableErrors.indexOf(responseCode) === -1) { break; } Utilities.sleep(2000 * Math.pow(2, attempts)); } if (responseCode >= 400 && ERROR_EMAIL_ADDRESS) {
    MailApp.sendEmail(
        ERROR_EMAIL_ADDRESS, 'Error sending SMS Message from AdWords Scripts',
        response.getContentText());
  }
}

/**
 * Builds a SMS request object specific for the Twilio service.
 * @param {string} recipientPhoneNumber Destination number including country
 *     code.
 * @param {string} textMessage The message to send.
 * @return {SmsRequest}
 */
function buildTwilioMessageRequest(recipientPhoneNumber, textMessage) {
  if (!recipientPhoneNumber) {
    throw Error('No "recipientPhoneNumber" specified in call to ' +
        'buildTwilioMessageRequest. "recipientPhoneNumber" cannot be empty');
  }
  if (!textMessage) {
    throw Error('No "textMessage" specified in call to ' +
        'buildTwilioMessageRequest. "textMessage" cannot be empty');
  }
  var twilioUri = 'https://api.twilio.com/2010-04-01/Accounts/' +
      TWILIO_ACCOUNT_SID + '/Messages';
  var authHeader = 'Basic ' +
      Utilities.base64Encode(
          TWILIO_ACCOUNT_SID + ':' + TWILIO_ACCOUNT_AUTHTOKEN);
  var options = {
    muteHttpExceptions: true,
    method: 'POST',
    headers: {Authorization: authHeader},
    payload: {
      From: TWILIO_SRC_PHONE_NUMBER,
      To: recipientPhoneNumber,
      // Twilio only accepts up to 1600 characters
      Body: textMessage.substr(0, 1600)
    }
  };
  return {url: twilioUri, options: options};
}
  
  
  
  for(var link_i in links){
  var link = links[link_i];
  var response = UrlFetchApp.fetch(link, { muteHttpExceptions: true} );  
  
  Logger.log(link + " Response Code: " + response.getResponseCode())
  
  if (response.getResponseCode() > 299) {
    
    Logger.log(link + " Response Code: " + response.getResponseCode());
    
    function sendSimpleTextEmail() {
      MailApp.sendEmail(emails,
                        'Website Response Code > 299',
                        link + " Response Code: " + response.getResponseCode());
    }
    sendSimpleTextEmail();
    
    for(var n_i in numbers){
    sendTwilioSms(numbers[n_i], "Response Code: "+response.getResponseCode());
    }
    
  }
    
  }//end link loop
  
}

Leave a Reply

Your email address will not be published. Required fields are marked *