# slack-payload

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

Latest version **1.0.11** (published 2018-07-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install slack-payload
pnpm add slack-payload
yarn add slack-payload
bun add slack-payload
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.11 |
| Published | 2018-07-01 |
| First published | 2017-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | John Agan |
| Maintainers | johnagan |
| Keywords | slack, payload, json, bot |

## Links

- npm: https://www.npmjs.com/package/slack-payload
- Repository: https://github.com/johnagan/slack-payload
- Issues: https://github.com/johnagan/slack-payload/issues
- npm.io page: https://npm.io/package/slack-payload

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.11 (latest) — 2018-07-01
- 1.0.10 — 2017-07-18
- 1.0.9 — 2017-05-07
- 1.0.8 — 2017-05-07
- 1.0.7 — 2017-05-06
- 1.0.6 — 2017-03-04
- 1.0.5 — 2017-02-20
- 1.0.4 — 2017-02-19
- 1.0.3 — 2017-02-19
- 1.0.2 — 2017-02-19
- 1.0.1 — 2017-02-19
- 1.0.0 — 2017-02-19

## README

# 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](https://api.slack.com/slash-commands), [Events API](https://api.slack.com/events-api), and [Interactive Messages](https://api.slack.com/docs/message-buttons). 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:

```js
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.
```js
if (event.payload)
  payload = JSON.parse(event.payload)
```

## Example
```js
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

Property | Description
---|---
`text` | The message text associated with the event
`team_id` | The team id the payload was sent from
`channel_id` | The channel id the payload was sent from
`user_id` | The user id that sent the payload (if available)
`bot_id` | The bot id that sent the payload (if available)
`selection` | The selected interactive message option
`action` | The selected interactive message action
`types` | An array of all types associated with this payload (includes a wildcard)

Function | Parameter | Description
---|---|---
`is` | event type [String] | Checks if the payload matches an event type. Get a full list of events [here](index.js#L197)
`match` | regex | Matches the text with a regular expression

## Install
```
npm i slack-payload
```

---
_Source: https://npm.io/package/slack-payload · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
