Tutorial Step 7 - Sending Notifications
Congratulations! You've reached the end of the tutorial. This last step actually has nothing to do with Cloud Manager or Adobe I/O. In this step, we're going to take the data retrieved and send a message to either Microsoft Teams or Slack. Or you could do both if you are feeling adventurous.
Setting up a Notification Webhook
For both Microsoft Teams and Slack, notifications can be sent via an Incoming Webhook integration. The details are slightly different, but both follow the same basic concept -- when you want to post a message to a channel, you can make an HTTP POST request to a special URL generated specifically for this purpose.
Documentation to create a webhook URL for Microsoft Teams can be found here
Documentation to create a webhook URL for Slack can be found here.
Notifying Slack
Sending a Slack notification can be done with just a simple JSON object containing a text
property. Let's create a new function which sends such an object to the webhook. To make the webhook easy to chnage, add a new variable to your .env
file named SLACK_WEBHOOK
and create this function:
Copied to your clipboard1function notifySlack (message) {2 fetch(process.env.SLACK_WEBHOOK, {3 'method': 'POST',4 'headers': { 'Content-Type': 'application/json' },5 'body': JSON.stringify({6 'text': message7 })8 })9}
And the invoke this function instead of logging:
Copied to your clipboard1if (STARTED === event['@type'] &&2 EXECUTION === event['xdmEventEnvelope:objectType']) {3 console.log('received execution start event')45 const executionUrl = event['activitystreams:object']['@id']67 getExecution(executionUrl).then(execution => {8 notifySlack(`Execution for ${execution.program.name} started`)9 })10}
This will produce a Slack message which looks like this:
Of course, feel free to make the message more complex if you want.
Notifying Microsoft Teams
Sending a Microsoft Teams notification can be as simple as Slack -- just a single text
property. But Teams also supports some slightly fancier formatting options -- notifications can have a title and banner color, among other options. As with the Slack webhook, you should put the webhook URL in your .env
file in a variable named TEAMS_WEBHOOK
.
Copied to your clipboard1function notifyTeams (message) {2 fetch(process.env.TEAMS_WEBHOOK, {3 'method': 'POST',4 'headers': { 'Content-Type': 'application/json' },5 'body': JSON.stringify({6 '@context': 'https://schema.org/extensions',7 '@type': 'MessageCard',8 'themeColor': '0072C6',9 'title': 'Update from Cloud Manager',10 'text': message11 })12 })13}
There are a number of other options for the payload sent to Microsoft. Please read the documentation for more information.
And then invoke this function:
Copied to your clipboard1if (STARTED === event['@type'] &&2 EXECUTION === event['xdmEventEnvelope:objectType']) {3 console.log('received execution start event')45 const executionUrl = event['activitystreams:object']['@id']67 getExecution(executionUrl).then(execution => {8 notifyTeams(`Execution for ${execution.program.name} started`)9 })10}
This will produce a Microsoft Teams message which looks like this: