Skip to content
Joey Wang
Menu

Search

Sending Pub/Sub Notifications to Slack With a Webhook

A small Node.js Cloud Function that decodes a Pub/Sub message, filters it by type, and forwards it to Slack through an incoming webhook.

· 1 min read

#javascript#gcp#slack#automation

A Cloud Function that takes a Pub/Sub message and forwards it to Slack through an incoming webhook, filtering by an optional list of type URLs so only the notifications worth seeing get through.

const { IncomingWebhook } = require('@slack/webhook');
const url = process.env.SLACK_WEBHOOK_URL;

const webhook = new IncomingWebhook(url);

// Optionally filter what notification types to forward to Slack.
// If empty, all types will be allowed.
const allowedTypeURLs = [];

// slackNotifier is the main function called by Cloud Functions
module.exports.slackNotifier = (pubSubEvent, context) => {
  const data = decode(pubSubEvent.data);

  // Send message to Slack.
  if (isAllowedType(pubSubEvent.attributes)) {
    const message = createSlackMessage(data, pubSubEvent.attributes);
    webhook.send(message);
  }
};

// decode decodes a pubsub event message from base64.
const decode = (data) => {
  return Buffer.from(data, 'base64').toString();
}

// isAllowedType can be used to filter out messages that don't match the
// allowed type URLs. If allowedTypeURLs is empty, it allows all types.
const isAllowedType = (attributes) => {
  if (allowedTypeURLs.length == 0) {
    return true;
  }
  for (var x in allowedTypeURLs) {
    if (attributes['type_url'] == allowedTypeURLs[x]) {
      return true;
    }
  }
  return false;
}

// createSlackMessage creates a message from a data object.
const createSlackMessage = (data, attributes) => {
  // Write the message data and attributes.
  text = `${data}`
  for (var key in attributes) {
    if (attributes.hasOwnProperty(key)) {
      text = text + `\n\t\`${key}: ${attributes[key]}\``
    }
  }
  const message = {
    text: text,
    mrkdwn: true,
  };
  return message;
}