Skip to content

Instantly share code, notes, and snippets.

@aoberoi
Created October 7, 2018 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoberoi/a55da26a9f93eafd622d34a2af48f7f2 to your computer and use it in GitHub Desktop.
Save aoberoi/a55da26a9f93eafd622d34a2af48f7f2 to your computer and use it in GitHub Desktop.
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