slack-fieldify

Automagically create Slack fields from JavaScript objects.

Usage no npm install needed!

<script type="module">
  import slackFieldify from 'https://cdn.skypack.dev/slack-fieldify';
</script>

README

Slack Fieldify

http://brett.neese.rocks

A simple Node module that flattens a JavaScript object into a set of "fields" compatible with Slack's API; making it easier to make Slack notifications from aribtary JSON data.

Automagically turning this:

{ type: 'Ready',
  status: 'False',
  lastProbeTime: null,
  lastTransitionTime: '2016-04-25T16:49:53Z',
  reason: 'ContainersNotReady',
  message: 'containers with unready status: [sti-build]' }

Into this:

Screenshot

See the Slack docs for more info on fields. They're pretty neat.

Features:

  • Long and short "fields"
  • Custom prefixes for multidimensional arrays
  • Humanizes and properly cases value

Usage

slackFieldify(object, [short], [prefix])

  • @param {Object} inputObj
  • @param {boolean} [False] short? - Whether you want the fields to be long, for verbose data or short, for short data
  • @param {string} [] prefix? - Prepends things to the output. Mostly used inside the function.

Returns: {Object} fields - a Slack-ready set of "fields"

Example

slackFieldify = require('slack-fieldify')

/// In your Slack webhook....

attachments {     
    fields: slackFieldify(JSON.parse(jsonDataFromAPI), true);
}

N.b

Also works for arrays, or arrays of objects. If there's multiple objects in an array, it'll prepend the output with their index.

If there's only one object in an array, it won't.