Created
May 1, 2016 23:01
-
-
Save marcelog/3f463cd98d5ce41528eb58bfd7d7bcb5 to your computer and use it in GitHub Desktop.
Send HipChat Notifications with SNS and Lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the version that works with HipChat API v2