Skip to content

Instantly share code, notes, and snippets.

@junkangli
Last active October 26, 2018 09:50
Show Gist options
  • Save junkangli/bf00918620bf98dbd220fb717e236e94 to your computer and use it in GitHub Desktop.
Save junkangli/bf00918620bf98dbd220fb717e236e94 to your computer and use it in GitHub Desktop.
This is an AWS Lambda function that handles an Azure DevOps Services event and sends a notification to a room on a hosted HipChat server. It is meant to integrate with AWS API Gateway and subscribe to an Azure DevOps Services project Web Hook.
const http = require('https');
var hipchatToken = process.env['hipchatToken']
var hipchatRoom = '96';
exports.handler = async (event, context, callback) => {
const body = JSON.parse(event.body);
const message = body.message;
var hipchatMessage = JSON.stringify({
message: message.html,
notify: true,
message_format: 'html',
});
console.log('hipchat message: ' + hipchatMessage);
var httpOptions = {
host: 'collab.singtel-labs.com',
port: 443,
method: 'POST',
path: '/v2/room/' + hipchatRoom + '/notification?auth_token=' + hipchatToken,
headers: {
'Content-Type': 'application/json',
}
};
var req = http.request(httpOptions, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log('BODY: ' + chunk);
});
res.on('end', function() {
console.log(res.statusCode);
if (res.statusCode === 204) {
console.log('success');
callback(null, 'message delivered to hipchat');
} else {
console.log('failed');
const error = new Error('hipchat API returned an error');
callback(error);
}
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
const error = new Error('failed to deliver message to hipchat');
callback(error);
});
req.write(hipchatMessage);
req.end();
const response = {
statusCode: 200,
body: JSON.stringify('success')
};
callback(null, response);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment