Bolt-js: Demonstrate using ExpressReceiver's router property to add custom HTTP routes

Created on 10 Jun 2020  路  6Comments  路  Source: slackapi/bolt-js

Description

Many developers have asked how they can add new HTTP routes to their Bolt app. These routes are usually unrelated to Slack, but used to handle webhooks from other APIs, serve an API, or to render webpages.

Since v2.1.0, this is a supported feature of ExpressReceiver via the added router property. That property exposes an Express Router on which more routes can be added.

This information is not well documented today, and it should be. Here is a starting code example that could be used:

const { App, ExpressReceiver } = require('@slack/bolt');

// Create a Bolt Receiver
const receiver = new ExpressReceiver({ signingSecret: '' });

// Create the Bolt App, using the receiver
const app = new App({ token: '', receiver });

// Slack interactions are methods on app
app.event('message', async ({ event, client }) => {
  // Do some slack-specific stuff here
  await client.chat.postMessage(...);
});

// Other web requests are methods on receiver.router
receiver.router.post('/secret-page', (req, res) => {
  // You're working with an express req and res now.
  res.send('yay!');
});

(async () => {
  await app.start(8080);
  console.log('app is running');
}());

Ref: https://github.com/slackapi/bolt-js/issues/283#issuecomment-600035307

Requirements (place an x in each of the [ ])

  • [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x] I've read and agree to the Code of Conduct.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.
docs

Most helpful comment

The doc is now live at https://slack.dev/bolt-js/concepts#custom-routes

All 6 comments

Hello @aoberoi,

This is exactly what I need but my new route is getting a 404 with @slack/bolt version 2.1.1.

In order auto-scale my application with GCP App Engine I need to leverage a (warmup request) route specifically at GET /_ah/warmup.

Here is the relevant code:

const config = require('config');
const { App, ExpressReceiver } = require('@slack/bolt');
const app = new App({
  token: config.get('slack.token'),
  signingSecret: config.get('slack.signingSecret')
});

// Create a Bolt Receiver for Google App Engine's Warmup Route
const receiver = new ExpressReceiver({ signingSecret: '' });
receiver.router.get('/_ah/warmup', (req, res) => {
  res.status(200).send("");
});

When I analyze with the debugger it appears as the route is successfully applied next to the /slack/events route but when I try to use it I get back a 404 NOT FOUND.

$ http GET :3000/_ah/warmup/
HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 150
Content-Security-Policy: default-src 'none'
Content-Type: text/html; charset=utf-8
Date: Thu, 11 Jun 2020 03:30:00 GMT
X-Content-Type-Options: nosniff
X-Powered-By: Express

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /_ah/warmup/</pre>
</body>
</html>

@szgaljic You need to give the receiver to App instance:

const app = new App({
  receiver,
  token: config.get('slack.token'),
});

I totally missed that step! It works now, thanks!

The doc is now live at https://slack.dev/bolt-js/concepts#custom-routes

When deploying through Lamda, should this new route show up as another accessible endpoint upon deploy?

should this new route show up as another accessible endpoint upon deploy?

@lukewing-okta
As long as you have an endpoint on the API Gateway side, there is no reason your custom endpoint path does not work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johnsonyick picture johnsonyick  路  4Comments

dschinkel picture dschinkel  路  4Comments

johnboxall picture johnboxall  路  4Comments

dginovker picture dginovker  路  4Comments

jacklein picture jacklein  路  4Comments