Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a generic event receiver #83

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ slapp.route('handleDoitConfirmation', (msg, state) => {

- [Slapp.use()](#slappusefnfunction)
- [Slapp.attachToExpress()](#slappattachtoexpressappobjectoptsobject)
- [Slapp.receiveEvent()](#slappreceiveeventeventobject)
- [Slapp.receiveCommand()](#slappreceivecommandcommandobject)
- [Slapp.receiveAction()](#slappreceiveactionactionobject)
- [Slapp.receiveOptions()](#slappreceiveoptionsoptionobject)
- [Slapp.route()](#slapproutefnkeystringfnfunction)
- [Slapp.getRoute()](#slappgetroutefnkeystring)
- [Slapp.match()](#slappmatchfnfunction)
Expand Down Expand Up @@ -454,6 +458,34 @@ slapp.route('handleDoitConfirmation', (msg, state) => {
})
```

## Slapp.receiveEvent(event:Object)

Receive a new event from slack, this is useful if you are not using express to handle events from slack

#### Parameters
- `event` string - JSON event payload from slack

## Slapp.receiveCommand(command:Object)

Receive a new slash command from slack, this is useful if you are not using express to handle commands from slack

#### Parameters
- `command` string - JSON slash command payload from slack

## Slapp.receiveAction(action:Object)

Receive a new interactive button action from slack, this is useful if you are not using express to handle actions from slack

#### Parameters
- `action` string - JSON button action payload from slack

## Slapp.receiveOptions(option:Object)

Receive a new interactive menu load from slack, this is useful if you are not using express to handle options from slack

#### Parameters
- `option` string - JSON option payload from slack

## Slapp.route(fnKey:string, fn:function)

Register a new function route
Expand Down
30 changes: 30 additions & 0 deletions src/receiver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,35 @@ module.exports = class Receiver extends EventEmitter {
msg.attachResponse(response, timeout)
}

receiveHandler (slappData, callback) {
let req = { slapp: slappData }

this.context(req, null, err => {
if (err) {
return callback(err)
}

let message = req.slapp
let msg = new Message(message.type, message.body, message.meta)

this.emit('message', msg)
})
}

receiveEvent (event, callback) {
this.receiveHandler(ParseEvent.slappData(event), callback)
}

receiveCommand (command, callback) {
this.receiveHandler(ParseCommand.slappData(command), callback)
}

receiveAction (action, callback) {
this.receiveHandler(ParseAction.slappData(action), callback)
}

receiveOptions (option, callback) {
this.receiveHandler(ParseOptions.slappData(option), callback)
}
}

35 changes: 21 additions & 14 deletions src/receiver/middleware/parse-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const bodyParser = require('body-parser')

module.exports = () => {
let parse

module.exports = parse = () => {
return [
bodyParser.urlencoded({extended: true}),
bodyParser.text({type: '*/*'}),
Expand All @@ -19,21 +21,26 @@ module.exports = () => {
return res.send('Error parsing payload')
}

req.slapp = {
type: 'action',
body: body,
meta: {
verify_token: body.token,
user_id: body.user.id,
channel_id: body.channel.id,
team_id: body.team.id
},
// Message actions may be responded to directly within 3000ms
response: res,
responseTimeout: 2500
}
req.slapp = parse.slappData(body)

// Message actions may be responded to directly within 3000ms
req.slapp.response = res
req.slapp.responseTimeout = 2500

next()
}
]
}

parse.slappData = body => {
return {
type: 'action',
body: body,
meta: {
verify_token: body.token,
user_id: body.user && body.user.id,
channel_id: body.channel && body.channel.id,
team_id: body.team && body.team.id
}
}
}
35 changes: 21 additions & 14 deletions src/receiver/middleware/parse-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@

const bodyParser = require('body-parser')

module.exports = () => {
let parse

module.exports = parse = () => {
return [
bodyParser.urlencoded({extended: true}),
function parseCommand (req, res, next) {
let body = req.body

req.slapp = {
type: 'command',
body: body,
meta: {
verify_token: body.token,
user_id: body.user_id,
channel_id: body.channel_id,
team_id: body.team_id
},
// Slash Commands requests may be responded to directly within 3000ms
response: res,
responseTimeout: 2500
}
req.slapp = parse.slappData(body)

// Message actions may be responded to directly within 3000ms
req.slapp.response = res
req.slapp.responseTimeout = 2500

next()
}
]
}

parse.slappData = body => {
return {
type: 'command',
body: body,
meta: {
verify_token: body.token,
user_id: body.user_id,
channel_id: body.channel_id,
team_id: body.team_id
}
}
}
37 changes: 22 additions & 15 deletions src/receiver/middleware/parse-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const bodyParser = require('body-parser')

module.exports = () => {
let parse

module.exports = parse = () => {
return [
bodyParser.json(),
function handleChallenge (req, res, next) {
Expand All @@ -18,22 +20,27 @@ module.exports = () => {
},
function parseEvent (req, res, next) {
let body = req.body || {}
let event = body.event || {}
let channelId = event.channel || (event.item && event.item.channel)

req.slapp = {
type: 'event',
body: body,
meta: {
verify_token: body.token,
user_id: event.user,
bot_id: event.bot_id,
channel_id: channelId,
team_id: body.team_id
}
}

req.slapp = parse.slappData(body)

next()
}
]
}

parse.slappData = (body) => {
let event = body.event || {}
let channelId = event.channel || (event.item && event.item.channel)

return {
type: 'event',
body: body,
meta: {
verify_token: body.token,
user_id: event.user,
bot_id: event.bot_id,
channel_id: channelId,
team_id: body.team_id
}
}
}
35 changes: 21 additions & 14 deletions src/receiver/middleware/parse-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const bodyParser = require('body-parser')

module.exports = () => {
let parse

module.exports = parse = () => {
return [
bodyParser.urlencoded({extended: true}),
function parseOptions (req, res, next) {
Expand All @@ -18,21 +20,26 @@ module.exports = () => {
return next(new Error('Error parsing payload'))
}

req.slapp = {
type: 'options',
body: body,
meta: {
verify_token: body.token,
user_id: body.user && body.user.id,
channel_id: body.channel && body.channel.id,
team_id: body.team && body.team.id
},
// Options must be handled very quickly within ???
response: res,
responseTimeout: 3000
}
req.slapp = parse.slappData(body)

// Message actions may be responded to directly within 3000ms
req.slapp.response = res
req.slapp.responseTimeout = 3000

next()
}
]
}

parse.slappData = body => {
return {
type: 'options',
body: body,
meta: {
verify_token: body.token,
user_id: body.user && body.user.id,
channel_id: body.channel && body.channel.id,
team_id: body.team && body.team.id
}
}
}
52 changes: 52 additions & 0 deletions src/slapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,58 @@ class Slapp extends EventEmitter {
return this.receiver.attachToExpress(app, opts)
}

/**
* Receive a new event from slack, this is useful if you are not using express to handle events from slack
*
* ##### Parameters
* - `event` string - JSON event payload from slack
*
* @param {Object} event - event payload from slack
*/

receiveEvent (event) {
return this.receiver.receiveEvent(event)
}

/**
* Receive a new slash command from slack, this is useful if you are not using express to handle commands from slack
*
* ##### Parameters
* - `command` string - JSON slash command payload from slack
*
* @param {Object} command - slash command payload from slack
*/

receiveCommand (command) {
return this.receiver.receiveCommand(command)
}

/**
* Receive a new interactive button action from slack, this is useful if you are not using express to handle actions from slack
*
* ##### Parameters
* - `action` string - JSON button action payload from slack
*
* @param {Object} action - button action payload from slack
*/

receiveAction (action) {
return this.receiver.receiveAction(action)
}

/**
* Receive a new interactive menu load from slack, this is useful if you are not using express to handle options from slack
*
* ##### Parameters
* - `option` string - JSON option payload from slack
*
* @param {Object} option - option payload from slack
*/

receiveOptions (option) {
return this.receiver.receiveOptions(option)
}

/**
* Register a new function route
*
Expand Down
Loading