Lesson 1: Initialize an App Builder App Using a Template

If you don't have an App Builder app, please follow Create a New App Builder App from Template to create one. Make sure you have publish-eventin the template, and add I/O management APIin the console. Then run aio app deploy, and you should see this: publishevent

Here is the project set up at the Adobe Developer console: consoleproject

Event registration

Create event consumer

We will use the generic App Builder template to modify the code that creates an event consumer, naming the action consume-event. After deploying the event, you will be able to set up the event registration runtime actions you deployed.

Here is some sample code that shows how to test the webhook feature:

function main(params) {
  console.log('user action is processing event ' + params.event);
  var event = params.event;
  var id = event.id;
  var event_processed = "Event Received And Processed :: " + JSON.stringify(params.event)
    return {
        body: event_processed,
        statusCode: 200,
        headers: {
          'Content-Type': 'application/json'
        }
    };
  }

Alternatively, you could use this one to create a webhook to send to Slack:

/* this is a sample action sent a message to slack */
var request = require('request');

/* default slackwebhook and channel add yours here and replace the TODO below */
var slackWebhook = "Your webhook";
var slackChannel = "your channel";

async function main (params) {

  /* print event detail */
  console.log('in main + event detail: ', params.event);

  var returnObject = {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json'
    },
    body: ""
  };

  /* handle the challenge */
  if (params.challenge) {

    console.log('Returning challenge: ' + params.challenge);

    returnObject.body = new Buffer(JSON.stringify({
      "challenge": params.challenge
    })).toString('base64');

    return returnObject;

  } else {

    /* we need it to run asynchronously, so we are returning a Promise */
    return new Promise(function (resolve, reject) {

      var slackMessage = " Event received: " + JSON.stringify(params);

      var payload = {
        "channel": slackChannel,
        "username": "incoming-webhook",
        "text": slackMessage,
        "mrkdwn": true,
      };

      var options = {
        method: 'POST',
        url: slackWebhook,
        headers:
            { 'Content-type': 'application/json' },
        body: JSON.stringify(payload)
      };

      request(options, function (error, response, body) {
        if (error) {

          console.log("ERROR: fail to post " + response);

          reject(error);

        } else {

          console.log ("SUCCESS: posted to slack " + slackMessage);

          returnObject.body = new Buffer(JSON.stringify({
            "slackMessage": slackMessage
          })).toString('base64');

          resolve(returnObject);
        }

      });

    });

  }
}

exports.main = main

Note: An action used as event consumer does not need to be web: yes, and doesn't need require-adobe-auth: true in the manifest.yml file. Please make the necessary modifications to ensure your app's security.

Event runtime integration