Skip to content

Instantly share code, notes, and snippets.

@julianeon
Last active July 25, 2016 12:47
Show Gist options
  • Save julianeon/7490570 to your computer and use it in GitHub Desktop.
Save julianeon/7490570 to your computer and use it in GitHub Desktop.
Event state change emailer. It listens for webhook updates and sends email updates (as they come in) to the email address given in your service's name. For now, it looks for one email address in the service line, then sends notifications to that address.
//Node.js script to act as a listener for webhooks.
//
// Copyright (c) 2014, PagerDuty, Inc. <[email protected]>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of PagerDuty Inc nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
var nodemailer = require("nodemailer");
http = require('http');
fs = require('fs');
email_service="Gmail";
email_sender="[email protected]";
email_pwd="fake_password";
email_to_backup="[email protected]";
server = http.createServer( function(req, res) {
var smtpTransport=nodemailer.createTransport("SMTP",{
service: email_service,
auth: {
user: email_sender,
pass: email_pwd,
}
});
console.dir(req.param);
function email_goes_here(hash,backup) {
data=hash.data;
incident=data.incident;
service=incident.service;
var msg_arr=service.name.split(/EMAIL_TO:/);
var email_to=msg_arr[1];
if (typeof email_to==='undefined') {
email_to=service.name.match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/ig);
}
else {
email_to=email_to.split(/,/);
}
if (email_to===null){
email_to=backup;
}
return email_to;
}
function email_subject(hash){
data=hash.data;
incident=data.incident;
var strsubject="PagerDuty Alert: Incident " + incident.id + " - " + incident.status;
return strsubject;
}
function email_body(hash){
state_type=hash.type;
data=hash.data;
incident=data.incident;
service=incident.service;
escalator=incident.escalation_policy;
assigned_user=incident.assigned_to_user;
about_trigger=incident.trigger_summary_data;
var strbody= "Incident has been " + incident.status + ".\n";
strbody+= "ID: " + incident.id + "\n";
strbody+= "No.: " + incident.incident_number + "\n";
strbody+= "Created on: " + incident.created_on.replace(/[A-Z]/g," ") + "\n";
strbody+="\n";
strbody+= "By this service.\n"
strbody+= "Name: " + service.name + "\n";
strbody+= "ID: " + service.id + "\n";
strbody+="\n";
strbody+= "Using this escalation policy.\n";
strbody+= "Name: " + escalator.name + "\n";
strbody+= "ID: " + escalator.id + "\n";
strbody+="\n";
if (typeof assigned_user!=='undefined' && assigned_user!=null) {
strbody+= "Assigned to this user.\n"
strbody+= "Name: " + assigned_user.name + "\n";
strbody+= "Email: " + assigned_user.email + "\n";
strbody+= "ID: " + assigned_user.id + "\n";
strbody+="\n";
}
strbody+= "Triggered by this event.\n"
strbody+= "Subject: " + about_trigger.subject + "\n";
strbody+= "From: " + incident.trigger_type.replace(/_/," ") + "\n";
strbody+= "Last change.\n";
strbody+= "On: " + incident.last_status_change_on.replace(/[A-Z]/g, " ") + "\n";
strbody+= "Number of escalations: " + incident.number_of_escalations + "\n";
strbody+="\n";
strbody+=state_type+"\n";
return strbody;
}
function email_routing(to_e,from_e,sub,body) {
var maildetails = {
from: from_e,
to: to_e,
subject: sub,
text: body,
}
return maildetails;
}
if (req.method == 'POST') {
console.log("POST");
var body = '';
req.on('data', function (data) {
body += data;
req.on('end', function () {
var obj=JSON.parse(body);
var data=obj.messages[0];
var email_to=email_goes_here(data,email_to_backup);
var strbody=email_body(data);
var strsubject=email_subject(data);
for (key in email_to) {
var mailOptions = email_routing(email_to[key],email_sender,strsubject,strbody);
console.log(email_to);
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + strbody);
}
});
}
}); res.writeHead(200, {'Content-Type': 'text/html'}); res.end('post received');
});
}
else
{
var endmessage="This page is the index page for the node event listener. Doesn't really do anything, but lets you know it's here.";
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(endmessage);
}});
server.listen(process.env.PORT || 3000);
@garrettwilkin
Copy link

Couldn't get MIT license on there, eh?

Why not make this a proper repo?

Also, are you not using fs? https://gist.github.com/julianeon/7490570#file-heroku-listener-emailer-js-L30

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