Created
November 30, 2016 19:48
-
-
Save serkanh/ca7822c2bf292509d3541236de45aa1d to your computer and use it in GitHub Desktop.
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
console.log('Loading function'); | |
var AWS = require('aws-sdk'); | |
AWS.config.region = 'us-east-1'; | |
// Hipchat room notification auth token, as per: https://www.hipchat.com/docs/apiv2/auth | |
// Make sure to create it per token | |
var authToken = 'API-TOKEN-PER-ROOM'; | |
var critical = 'true'; | |
// Hipchat server and room name | |
var host = 'jumpstart.hipchat.com'; | |
//API ID OF THE ROOM | |
var room = 'API-ID'; | |
var https = require('https'); | |
var sqs = new AWS.SQS(); | |
exports.handler = (event, context) => { | |
var params = { | |
AttributeNames: [ | |
"ApproximateNumberOfMessages" | |
], | |
QueueUrl: "QUEUE-URL" | |
}; | |
sqs.getQueueAttributes(params, function(err, data) { | |
if (err) { | |
console.log(err, err.stack);// an error occurred | |
}else{ | |
console.log(data.Attributes.ApproximateNumberOfMessages); // successful response | |
Notify(data.Attributes.ApproximateNumberOfMessages,function(status) { | |
console.log(status); | |
}); | |
//context.done(); // SUCCESS | |
} | |
}); | |
}; | |
function Notify(body,completedCallback) { | |
var messageBody ='Number of Messages in ContentPublishing-qa-Rejected:'+body; | |
// The notification message to send | |
var message = { | |
message: messageBody, | |
color: 'red', | |
notify: critical, | |
message_format: 'text' | |
}; | |
var messageString = JSON.stringify(message); | |
// Options and headers for the HTTPS request | |
var options = { | |
host: host, | |
port: 443, | |
path: '/v2/room/' + room + '/notification', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': messageString.length, | |
'Authorization': 'Bearer '+authToken | |
} | |
}; | |
// Setup the HTTPS request | |
var req = https.request(options, function (res) { | |
// Collect response data as it comes back. | |
var responseString = ''; | |
res.on('data', function (data) { | |
responseString += data; | |
}); | |
// Log the response received from Hipchat: if it's empty it's usually 200-OK. | |
res.on('end', function () { | |
if (!responseString) { | |
console.log('Hipchat Response: 200 OK'); | |
completedCallback('API request sent successfully.'); | |
} else { | |
console.log('Hipchat Response: ' + JSON.stringify(responseString)); | |
completedCallback('API request sent almost successfully.'); | |
} | |
}); | |
}); | |
// Handler for HTTPS request errors. | |
req.on('error', function (e) { | |
console.error('HTTPS error: ' + e.message); | |
completedCallback('API request completed with error(s).'); | |
}); | |
// Send the HTTPS request to the Hipchat API. | |
// Log the message we are sending. | |
console.log('Hipchat API call: ' + messageString); | |
req.write(messageString); | |
req.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment