Skip to content

Instantly share code, notes, and snippets.

@marcelog
Created May 1, 2016 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelog/3f463cd98d5ce41528eb58bfd7d7bcb5 to your computer and use it in GitHub Desktop.
Save marcelog/3f463cd98d5ce41528eb58bfd7d7bcb5 to your computer and use it in GitHub Desktop.
Send HipChat Notifications with SNS and Lambda
/*
Test it through SNS with a payload like:
{
"default": "{\"text\": \"hello world\", \"color\": \"yellow\"}",
"lambda": "{\"text\": \"hello world\", \"color\": \"yellow\"}"
}
*/
var http = require('http');
var https = require('https');
var buffer = require('buffer').Buffer;
var util = require('util');
var querystring = require('querystring');
function hipchat(message, color, callback) {
var token = 'your_hipchat_v1_token';
var body = querystring.stringify({
message: message,
message_format: 'text',
color: color,
notify: 1,
from: 'message_source',
room_id: 'a_room_name_or_id'
});
var options = {
host: 'api.hipchat.com',
port: 443,
path: ('/v1/rooms/message?format=json&auth_token=' + token),
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body)
}
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
});
res.on('end', function (chunk) {
callback();
});
});
req.on('error', function(err) {
callback(err);
});
req.write(body);
req.end();
}
exports.handler = function(event, context, callback) {
var text = event.text;
var color = event.color;
hipchat(text, color, callback);
};
@flocsy
Copy link

flocsy commented May 9, 2017

This is the version that works with HipChat API v2

var http = require('http');
var https = require('https');
var buffer = require('buffer').Buffer;
var util = require('util');
var querystring = require('querystring');

function hipchat(message, color, callback) {

  /***** Fill in the values below *****/
  var token = '<get room token from https://your-company.hipchat.com/rooms/tokens/room_id>'; // Room Notification Token for v2
  var room_id = '<room name or id>';
  var from = 'Lambda'; // this will be after the "dot" in the HipChat message header: <Auth Token name> · <from>
  /*******************************/

  var body = querystring.stringify({
    message: message,
    message_format: 'text',
    color: color,
    notify: 1,
    from: from,
    room_id: room_id
  });
  // for API V2:
  var options = {
    host: 'api.hipchat.com',
    port: 443,
    path: ('/v2/room/' + room_id + '/notification'),
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': Buffer.byteLength(body),
      'Authorization': 'Bearer ' + token
    }
  };
  var req = https.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
    });
    res.on('end', function (chunk) {
        callback();
    });
  });
  req.on('error', function(err) {
    callback(err);
  });
  req.write(body);
  req.end();
}

exports.handler = function(event, context, callback) {
  var text = event.text;
  var color = event.color;
  if(event.Records) {
    console.log(event.Records[0].Sns.Message);
    var msg = event.Records[0].Sns;
    var deployMsg = JSON.parse(msg.Message);
    var subject = msg.Subject;
    var status = deployMsg.status;
    text = msg.text;
    if(status === 'SUCCEEDED' || /SUCCEEDED/.test(subject)) {
        color = 'green';
    } else if(status === 'CREATED' || /CREATED/.test(subject)) {
      color = 'yellow';
    } else if(status === 'INPROGRESS' || /INPROGRESS/.test(subject)) {
      color = 'yellow';
    } else {
        color = 'red';
    }
    text = subject;
    if(deployMsg.deploymentOverview) {
      text = text + ': ' + deployMsg.deploymentOverview;
    }
    if(deployMsg.deploymentGroupName) {
      text = text + ' (' + deployMsg.deploymentGroupName + ')';
    }
    if(deployMsg.lifecycleEvents) {
      text = text + ' ' + deployMsg.lifecycleEvents;
    }
  }
  hipchat(text, color, callback);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment