GIG: Follow up notification based on response with bonus QR codes!

Welcome back! We get this request from time to time to notify someone else when a user responds to an event. Some examples might be asking for volunteers from a large group of people, then sending responders additional information through a subsequent notification. Well, let's see how we can do something like that using this fancy Integration Builder.

Chef loved the barcode idea for his loyalty program and wanted to expand it to the muffin reservation notifications from GIG 5. But barcodes are so 90s, any respectable chef uses QR codes. So, let's update our muffin reservation integration to fire off a new event to the person who responded and hook them up with their QR code to redeem their muffin. 

 


Ok, so when you think, "I need to take action when someone responds", you should immediately think, "I need a response webhook". Don't worry if you start dreaming about webhooks. That's normal. I do it all the time. This one time, webhooks ate my middle school bully and sent an HTTP DELETE to hell://game.over. There was much fire and brimstone and I awoke very satisfied.
Um, anyhooo, webhooks. Response Webhooks specifically. First, we need a Form to fire an event to. I called it Send Muffin QR Code. After generating a couple more properties and an email/push message template, I marked it as Web Service Only. Since we'll be sending images, email/push makes the most sense. But you could just add the link to the SMS message template. I'm not sure if anyone would appreciate a voice call that reads out a url though.... Unless he's a middle school bully. In which case, have at it 

I found a QR Code generator that has a super simple API for generating these QR codes. All you have to do is send it the text you want encoded and it takes care of the rest. Take a gander at the docs but they are pretty easy to use. Everything can be passed as an HTTP GET, which means all the data is encoded on the URL. So if I wanted to make a QR code of Task Force X FTW!, then I can just send a GET request to this URL:

https://api.qrserver.com/v1/create-qr-code?data=Task%20Force%20X%20FTW%21

Since this is a GET, you can actually paste that url into your browser to get this bad boy:

 

Ok, before we get into the nuts and bolts, let's just review the workflow:

  1. An event is generated and sends out notifications
  2. A user responds
  3. A response outbound integration fires that creates a new event
  4. The new event sends out notifications

The critical piece here is step 3. In the bad old days, you could only respond and that response would get logged and life moved on. Then came the integration builder and there was much rejoicing. 

Ok, now on to the nuts and bolts. This is the outbound integration script that will fire a new event: (Note, I'm not showing the default code that populates the callback.eventProperties here for clarity, but note that it should be there). 

Create a new Outbound Integration script. In the wizard select the Run a script action, then the Muffins Ready form and finally the Notification Responses trigger. Like so:

 

Click Save and Open Script Editor and paste in the following code.

 

/////// Pre-existing code to deal with eventProperties ////////
var callback = JSON.parse(request.body);
console.log('Executing outbound integration for xMatters event ID: ' + callback.eventIdentifier);

// Convert list of event properties to an eventProperties object
if (callback.eventProperties && Array.isArray(callback.eventProperties)) {
var eventProperties = callback.eventProperties;
callback.eventProperties = {};

for (var i = 0; i < eventProperties.length; i++) {
var eventProperty = eventProperties[i];
var key = Object.keys(eventProperty)[0];
callback.eventProperties[key] = eventProperty[key];
}
}

// Handle responses without annotations
if (callback.annotation == "null") {
callback.annotation = null;
}
//// END Pre-existing code to deal with eventProperties ///

// Don't send them a QR code if they didn't want one
if( callback.response != 'I want one!' ) {
console.log( 'Fine, you don\'t get one' );
return;
}

// Set the target and generate the QR Code
var target = callback.recipient;
var qrcode = 'https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=' + encodeURIComponent( 'put random value here' );

// Generate the new event
var payload = {
"properties": {
"Customer Name": callback.recipient,
"Flavor": callback.eventProperties['Flavor'],
"Fruits": callback.eventProperties['Fruits'].split(),
"Nuts?": callback.eventProperties['Nuts?'],
"Baker\u0027s Notes": callback.eventProperties["Baker's Notes"],
"QR Code URL": qrcode
},
"recipients": [{ "targetName": target, "devices": ["Work Email"] }]
};

// Prepare the HTTP request.
// Note that the 'path' here is the "Send Muffin QR Code" form, NOT
// the "Muffins Ready" form!!
var request = http.request({
'endpoint': 'xMatters',
'method': 'POST',
'path': '/reapi/2015-04-01/forms/b4a0b145-453c-4df6-92d9-2b4c086baefb/triggers',
'headers': {
'Content-Type': 'application/json'
}
});

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

 

This tells xMatters to call the Send QR Code outbound integration script, which will then inspect the response and generate a new event, targeting the Send Muffin QR Code form. 

So, after responding with "I want one!":

 

Moments later, I get a new notification with my QR code!

 

I've attached the whole communications plan. You should be able to download it, pop it into your favorite xMatters environment and send yourself (or your friends!) QR codes and Muffin updates. 

Enjoy!

Attachments
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.