Logo

Slack Message

OpenMeter Notifications enables you to send Slack messages to your team about your customer's consumption. This is done via our webhook provider, Svix's Slack integration and webhook transformations.

Setting up the Slack integration

To set up the Slack integration, visit the Svix dashboard from OpenMeter Cloud UI. On the Svix dashboard, click Add Endpoint -> Transformation template -> Report Usage to Slack.

On the endpoint creation page, connect your Slack account and pick a Slack channel. Then, choose the entitlements.balance.threshold event type.

Webhook Transformation

With the Slack integration set up, you can now create a webhook transformation to customize the Slack message. The transformation code is written in JavaScript and is executed on the Svix platform. See the example below for a transformation code that you can use to send a Slack message when a customer's consumption exceeds a threshold.

The Slack message looks as: OpenMeter Architecture

And the transformation code producing it is:

function handler(webhook) {
  const data = webhook.payload.data;
  const subject = data.subject.displayName || data.subject.key;
  const feature = data.feature.name;
  const threshold =
    data.threshold.type === 'PERCENT'
      ? `${data.threshold.value}%`
      : webhook.payload.threshold.value;
 
  const from = data.entitlement.currentUsagePeriod.from;
  const fromUnix = Math.round(new Date(from).getTime() / 1000);
  const fromStr = `<!date^${fromUnix}^{date} at {time}|${from}>`;
  const to = new Date(data.entitlement.currentUsagePeriod.to);
  const toUnix = Math.round(new Date(to).getTime() / 1000);
  const toStr = `<!date^${toUnix}^{date} at {time}|${to}>`;
  webhook.payload = {
    text: `\`${subject}\` entitlement for \`${feature}\` is \`${threshold}\` for period \`${fromStr}\` - \`${toStr}\`.`,
  };
  return webhook;
}

To learn more about webhook transformations, please read Svix's documentation.