Created
October 7, 2018 19:10
-
-
Save aoberoi/a55da26a9f93eafd622d34a2af48f7f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const express = require('express'); | |
const app = express(); | |
const bodyParser = require('body-parser'); | |
const { WebClient } = require('@slack/client'); | |
const { createMessageAdapter } = require('@slack/interactive-messages'); | |
const slack = new WebClient(process.env.SLACK_ACCESS_TOKEN); | |
const slackInteractions = createMessageAdapter(process.env.SLACK_SIGNING_SECRET); | |
const orderAction = 'coffee_order'; | |
// handle slash command | |
app.post('/slack/command', bodyParser.urlencoded({ extended: false }), (req, res) => { | |
// should verify signature, but skipping for demo purposes | |
console.log('received slash command'); | |
res.send(''); | |
// asynchronously open a dialog | |
process.nextTick(() => { | |
slack.dialog.open({ | |
trigger_id: req.body.trigger_id, | |
dialog: { | |
title: 'Create coffee order', | |
callback_id: orderAction, | |
elements: [{ | |
type: 'text', | |
name: 'details', | |
label: 'Details', | |
}], | |
state: 'hello' | |
}, | |
submit_label: 'Order', | |
}); | |
}); | |
}); | |
// handle dialog submission | |
slackInteractions.action(orderAction, (payload, respond) => { | |
console.log('received dialog submission'); | |
console.log(payload); | |
// asynchronously send a success message | |
process.nextTick(() => { | |
respond({ | |
text: 'Order recieved', | |
}); | |
}); | |
}); | |
app.use('/slack/actions', slackInteractions.expressMiddleware()); | |
const server = http.createServer(app); | |
server.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment