writing a slack command bot

Tom van Neerijnen
localhost.run
Published in
2 min readSep 4, 2020

--

I recently wrote a slack bot that responds to action commands (like /echo yolo ) but I ran into some trouble finding exactly how to do this, so I’m documenting it here for other peeps to find. The exact error I hit was /echo failed with the error "dispatch_failed" .

https://slack.dev/bolt-js/tutorial/getting-started Is a great place to start, but what wasn’t clear to me was how to listen for commands rather than events.

Turns out events covers everything, including commands. Write a simple bolt app:

const { App, LogLevel, ExpressReceiver } = require('@slack/bolt');// your url in slack must have the path /slack/events
// path is the same for all types (commands, messages, not just events)
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
logLevel: LogLevel.DEBUG,
signingSecret: process.env.SLACK_SIGNING_SECRET,
});
/* Add functionality here */
app.command('/echo', async ({ command, ack, say }) => {
// Acknowledge command request
await ack();
await say(`${command.text}`);
});
app.error((error) => {
// Check the details of the error to handle cases where you should retry sending a message or stop the app
console.error(error);
});
(async () => {
// Start the app
await app.start(process.env.PORT || 3000);
console.log('⚡️ Bolt app is running!');
})();

And set your webhook server’s url, including the /slack/events path, in your slack apps command request URL like so:

You can quickly develop slack bots like this one with my service https://localhost.run. It tunnels your local development environment onto an internet accessible domain name with a single command, and it uses SSH so no signup or client download is necessary. Try it with this app right now for free by running ssh -R80:127.0.0.1:3000 ssh.localhost.run

--

--