Skip to content

Instantly share code, notes, and snippets.

@aoberoi
Created June 9, 2020 07:14
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/f5256da70ec4193c415979580bc15861 to your computer and use it in GitHub Desktop.
Save aoberoi/f5256da70ec4193c415979580bc15861 to your computer and use it in GitHub Desktop.
const { createServer } = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const { createEventAdapter } = require('@slack/events-api');
const { createMessageAdapter } = require('@slack/interactive-messages');
const slackSigningSecret = process.env.SLACK_SIGNING_SECRET;
const port = process.env.PORT || 3000;
const slackEvents = createEventAdapter(slackSigningSecret);
const slackInteractions = createMessageAdapter(slackSigningSecret);
// Create an express application
const app = express();
// Plug the adapter in as a middleware
app.use('/slack/events', slackEvents.requestListener());
// Plug the adapter in as a middleware
app.use('/slack/actions', slackInteractions.requestListener());
// Example: If you're using a body parser, always put it after the event adapter in the middleware stack
app.use(bodyParser());
// Initialize a server for the express app - you can skip this and the rest if you prefer to use app.listen()
const server = createServer(app);
server.listen(port, () => {
// Log a message when the server is ready
console.log(`Listening for events on ${server.address().port}`);
});
slackEvents.on('message', () => {
});
// Example of handling static select (a type of block action)
slackInteractions.action({ type: 'static_select' }, (payload, respond) => {
// Logs the contents of the action to the console
console.log('payload', payload);
// Send an additional message to the whole channel
doWork()
.then(() => {
respond({ text: 'Thanks for your submission.' });
})
.catch((error) => {
respond({ text: 'Sorry, there\'s been an error. Try again later.' });
});
// If you'd like to replace the original message, use `chat.update`.
// Not returning any value.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment