1.0.11 • Published 6 years ago

slack-payload v1.0.11

Weekly downloads
18
License
MIT
Repository
github
Last release
6 years ago

Slack Payload Wrapper

A lightweight wrapper for Slack payloads to make working with events easier and consistent across schemas.

Why?

Slack has many events that POST to HTTPS endpoints, including Slash Commands, Events API, and Interactive Messages. Each payload schema is slightly different and adds additional if/then logic to your endpoint functions.

Let's use user_id as an example. If your endpoint needs this id, your logic might look something like this:

function getUserId(payload) {
  // Interactive Messages
  if (payload.user) return payload.user.id

  // Slash Commands
  if (payload.user_id) return payload.user_id

  // Events API
  if (payload.event && payload.event.user) return payload.event.user
  if (payload.event && payload.event.item) return payload.event.item.user
}

Sometimes even the payload itself requires work to use since it can be encoded then included in the payload field.

if (event.payload)
  payload = JSON.parse(event.payload)

Example

const Payload = require('slack-payload'),

app.post('/slack', (req, res) => {
  let payload = new Payload(req.body)

  // returns the message text no matter the event type
  let text = payload.text

  // check for the event type or values
  let isCommand = payload.is('slash_command')
  let isButtonClick = payload.is('message_button')
  let isButtonValue = payload.is('my_button_value')

  // perform regex on text messages
  let match = payload.match(/lunch/i)

  // determine if the event was caused by a bot
  let bot_id = payload.bot_id

})

API

PropertyDescription
textThe message text associated with the event
team_idThe team id the payload was sent from
channel_idThe channel id the payload was sent from
user_idThe user id that sent the payload (if available)
bot_idThe bot id that sent the payload (if available)
selectionThe selected interactive message option
actionThe selected interactive message action
typesAn array of all types associated with this payload (includes a wildcard)
FunctionParameterDescription
isevent type StringChecks if the payload matches an event type. Get a full list of events here
matchregexMatches the text with a regular expression

Install

npm i slack-payload
1.0.11

6 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago