0

Sample Parse Email IB Outbound Webhook

Context
Email initiation is a simple and easy way to generate events and on some systems may be the only way to get stuff out. However, with simplicity often comes limited power. The email can only be chunked into subject, html body, plain text body and from, which can make it hard to display the information in an SMS or through a voice call ( think of a robot voice speaking the contents of an email...) and makes subscriptions rather useless. Fortunately, there is help. When you add the integration builder to the mix, you can pass the body of the incoming email through a script that can parse out the needed items and populate actual values in the event properties. This way you can then craft easy to read SMS, simple voice calls and unleash the power of subscriptions.

The workflow for this is to fire the event into the email initiation address, xMatters pulls out the subject, body, and from fields and generates an event. No one is notified with this event, but the event status callback triggers an outbound script that takes care of parsing the body of the email. Before the script finishes, it fires a new event into a new form using a web service endpoint. This new event now has all the details parsed out and ready to go.

To set this up, create a form and make it email initated. Map the subject, body and from to properties that make sense. Then, create a new Outbound Integration script and paste in the following code. Then, in the callbacks of the email initiation form, set this new outbound script as the event status callback.

Note: Modifications to this script will be needed depending on the format of your incoming email. Generally it might be easier to parse a plain text email rather than an html based email.

Code

var callback = JSON.parse( request.body );
console.log( 'Executing outbound integration for xMatters event ID: ' + callback.eventIdentifier );
console.log( 'Response Info -' + request.body );

var alldata = JSON.stringify( callback );
var i = alldata.indexOf( "Body" );
var body = alldata.substring( i + 6 ) + '"';
console.log( body );
var data = body;

var item = "";
var endchar = "";
var value = "";
var json = "";
var payload = "";

 

dataparse( data, "Monitor", ":" );
dataparse( data, "Group", "\n" );
dataparse( data, "Status", "\n" );
dataparse( data, "Sample #", "\n" );
dataparse( data, "Time", "\n" );
dataparse( data, "status", "\n" );
dataparse( data, "Thresholds warning", "\n" );
dataparse( data, "Total errors ( errors )", "\n" );

console.log( "Json - " + json );
var json2 = JSON.parse( '{"properties":{' + json + "}}" );
json2.properties.Sender = "SiteScope";
json2.properties.Subject = "SiteScope Alert";
json2.properties.ErrorField = json["Total errors ( errors )"];

// Prepare the HTTP request
var request = http.request({
'endpoint': 'xMatters',
'method': 'POST',
'path': '/reapi/2015-04-01/forms/35ce56a8-0ac1-4494-96cf-a4690c0d776c/triggers',
'headers': {
'Content-Type': 'application/json'
}
});

console.log( payload );
// Submit the request and capture the response
var response = request.write( json2 );

// Write the response to the activity stream
console.log( response );

 


function dataparse ( data, item, endchar ) {

var ji = data.indexOf( item );
//console.log( ji );
var jdata = data.substr( item.length + 2 + ji );
//console.log( jdata );
var jend = jdata.indexOf( endchar );
//console.log( jend );
value = data.substr( ji + item.length + 2, jend );
if ( json == "" ) {
json = '"' + item + '":' + '"' + value.trim() + '"';
console.log( "N" );
}else{
json = json + "," + '"' + item + '":' + '"' + value.trim() + '"';
}

console.log( item + " = " + value.trim());
return ( value, item );
}

 

0 comments

Please sign in to leave a comment.