Using/building a "webhook server" for Post /message

Had this use case come up today in some example scenarios:

I whipped together a hapi/node server + the twillio example to send “approve/deny” URLs to the user.

Just posting for anyone that comes across this use case later.

Node Code

'use strict';

const Hapi = require('hapi');
const Boom = require('boom');
const loki = require('lokijs');
const Wreck = require('wreck');
const uuidV4 = require('uuid/v4');

var db = new loki('loki.json')
var decisions = db.addCollection('decisions')

const server = new Hapi.Server();
server.connection({ port: 3000, host: 'localhost' });


// Create a Decision UUID.
server.route({
  method: 'GET',
  path: '/decisions',
  handler: function (request, reply) {
    var createdDecision = decisions.insert({"decisionid" : uuidV4() });
    reply({"decisionid" : createdDecision.decisionid});
}}) // End of Route

// Create a Decision Outcome (Approved/Denied)
server.route({
  method: 'GET',
  path: '/makedecision/{decisionid}',
  handler: function (request, reply) {

  var decisionid = request.params.decisionid;
  var decision = request.query.decision;

  // Check if the decision already exists.
  if (decisions.find({'decisionid': { '$eq' : decisionid }}).length == 0 ){
    return reply(Boom.badRequest('Incorrect decision id'););
  }

  if (decision != "approved" && decision != "denied"){
    return reply(Boom.badRequest('Invalid decision query param')); 
  }

  // Builds the data needed by the Camunda /messages endpoint
  var camundaURL = "http://192.168.99.100:8080/engine-rest/message"
  var camundaPayload = {
      "messageName" : "decision",
      "resultEnabled" : true,
      "correlationKeys" : {
        "decisionID" : {"value" : String(decisionid), "type": "String"}
      },
      "processVariables" : {
        "decisionOutcome" : {"value" : decision, "type" : "String"}
      }
    };

  //POST /Message to Camunda
  Wreck.post(camundaURL, { "payload" : camundaPayload, "json" : true }, (err, res, payload) => {
        reply({"response" : payload });
  });

}}) // End of route

server.start((err) => {

    if (err) {
        throw err;
    }
    console.log(`Server running at: ${server.info.uri}`);
});

BPMN:
uniqueURL-template.bpmn (17.1 KB)

Notes:

  1. very little is error caught or bullet proof. This was just a proof of concept to demonstrate a quick example.
  2. Consider this a starting point to extend from.
  3. Decision UUIDs are not persisted.
  4. Decision UUIDs are stored in-memory db (lokijs)
1 Like