Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/johnagan/slack-payload
A lightweight wrapper for Slack payloads to make working with events easier and consistent across schemas.
https://github.com/johnagan/slack-payload
json payload slack
Last synced: about 7 hours ago
JSON representation
A lightweight wrapper for Slack payloads to make working with events easier and consistent across schemas.
- Host: GitHub
- URL: https://github.com/johnagan/slack-payload
- Owner: johnagan
- License: mit
- Created: 2017-02-19T00:52:57.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-01T16:40:41.000Z (over 6 years ago)
- Last Synced: 2024-10-31T16:58:24.561Z (17 days ago)
- Topics: json, payload, slack
- Language: JavaScript
- Homepage:
- Size: 18.6 KB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
```