Socket
Book a DemoInstallSign in
Socket

slack-payload

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

slack-payload

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

latest
Source
npmnpm
Version
1.0.11
Version published
Weekly downloads
214
1.9%
Maintainers
1
Weekly downloads
 
Created
Source

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 [String]Checks 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

Keywords

slack

FAQs

Package last updated on 01 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts