Skip to content

Commit

Permalink
add example how to use AWSLambdaReceiver (slackapi#815) (slackapi#940)
Browse files Browse the repository at this point in the history
* add example how to use AWSLambdaReceiver (slackapi#815)
* replace existing example for AWS deployment with the one of the AwsLambdaReceiver (slackapi#815)
* update the documentation to match with the updated example (slackapi#815)
* Update the receiver name to be consistent through the do

Co-authored-by: Kazuhiro Sera <[email protected]>
Co-authored-by: Michael Brooks <[email protected]>
  • Loading branch information
3 people authored Jun 19, 2021
1 parent 6f0d5a2 commit 7698953
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 46 deletions.
40 changes: 16 additions & 24 deletions docs/_deployments/aws-lambda.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,48 +110,40 @@ Now that you have an app, let's prepare it for AWS Lambda and the Serverless Fra

By default, Bolt listens for HTTP requests. In this section, we'll customize your Bolt app's [`receiver`](https://slack.dev/bolt-js/concepts#receiver) to respond to Lambda function events instead.

First, install the [Serverless Express](https://github.com/vendia/serverless-express) module to transform Express HTTP requests to Lambda function events:

```bash
npm install --save @vendia/serverless-express
```

> 💡 This guide requires version `4.x.x` or later.
Next, update the [source code that imports your modules](https://github.com/slackapi/bolt-js-getting-started-app/blob/main/app.js#L1) in `app.js` to require Bolt's Express receiver and the AWS Serverless Express module:
First, update the [source code that imports your modules](https://github.com/slackapi/bolt-js-getting-started-app/blob/main/app.js#L1) in `app.js` to require Bolt's AwsLambdaReceiver:

```javascript
const { App, ExpressReceiver } = require('@slack/bolt');
const serverlessExpress = require('@vendia/serverless-express');
const { App, AwsLambdaReceiver } = require('@slack/bolt');
```

Then update the [source code that initializes your Bolt app](https://github.com/slackapi/bolt-js-getting-started-app/blob/main/app.js#L3-L7) to create a custom receiver using AWS Serverless Express:
Then update the [source code that initializes your Bolt app](https://github.com/slackapi/bolt-js-getting-started-app/blob/main/app.js#L3-L7) to create a custom receiver using AwsLambdaReceiver:

```javascript
// Initialize your custom receiver
const expressReceiver = new ExpressReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
// The `processBeforeResponse` option is required for all FaaS environments.
// It allows Bolt methods (e.g. `app.message`) to handle a Slack request
// before the Bolt framework responds to the request (e.g. `ack()`). This is
// important because FaaS immediately terminate handlers after the response.
processBeforeResponse: true
const awsLambdaReceiver = new AwsLambdaReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
});

// Initializes your app with your bot token and the AWS Lambda ready receiver
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
receiver: expressReceiver
token: process.env.SLACK_BOT_TOKEN,
receiver: awsLambdaReceiver,
// The `processBeforeResponse` option is required for all FaaS environments.
// It allows Bolt methods (e.g. `app.message`) to handle a Slack request
// before the Bolt framework responds to the request (e.g. `ack()`). This is
// important because FaaS immediately terminate handlers after the response.
processBeforeResponse: true
});
```

Finally, at the bottom of your app, update the [source code that starts the HTTP server](https://github.com/slackapi/bolt-js-getting-started-app/blob/main/app.js#L40-L45) to now respond to an AWS Lambda function event:

```javascript
// Handle the Lambda function event
module.exports.handler = serverlessExpress({
app: expressReceiver.app
});
module.exports.handler = async (event, context, callback) => {
const handler = await app.start();
return handler(event, context, callback);
}
```

When you're done, your app should look similar to the ⚡️[Deploying to AWS Lambda app][deploy-aws-lambda-app/app.js].
Expand Down
2 changes: 1 addition & 1 deletion examples/deploy-aws-lambda/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ package-lock.json
.DS_Store

# Serverless
.serverless/
.serverless/
6 changes: 3 additions & 3 deletions examples/deploy-aws-lambda/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Deploying to AWS Lambda ⚡️ Bolt for JavaScript

> Slack app example for deploying to AWS Lambda using the Serverless Framework with Bolt for JavaScript
> Slack app example for using the AwsLambdaReceiver of Bolt for JavaScript
## Overview

This is an example app that updates the [Getting Started ⚡️ Bolt for JavaScript app][bolt-app] to be deployed to [AWS Lambda][aws-lambda] using the [Serverless Framework][serverless-framework].
This is an example app that updates the [Getting Started ⚡️ Bolt for JavaScript app][bolt-app] to use the AwsLambdaReceiver and be deployed to [AWS Lambda][aws-lambda] using the [Serverless Framework][serverless-framework].
You can learn how to build this example app by following our 📚 [Deploying to AWS Lambda guide][bolt-guide-aws-lambda].

Before you begin, you may want to follow our [Getting Started guide][bolt-guide] to learn how to build your first Slack app using the [Bolt for JavaScript framework][bolt-website].
Expand Down Expand Up @@ -140,4 +140,4 @@ Follow the steps to [test your app](#6-test-your-slack-app).
[bolt-website]: https://slack.dev/bolt-js/
[ngrok-install]: https://api.slack.com/tutorials/tunneling-with-ngrok
[serverless-framework]: https://serverless.com/
[slack-app-settings]: https://api.slack.com/apps
[slack-app-settings]: https://api.slack.com/apps
24 changes: 12 additions & 12 deletions examples/deploy-aws-lambda/app.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
const { App, ExpressReceiver } = require('@slack/bolt');
const serverlessExpress = require('@vendia/serverless-express');
const { App, AwsLambdaReceiver } = require('@slack/bolt');

// Initialize your custom receiver
const expressReceiver = new ExpressReceiver({
const awsLambdaReceiver = new AwsLambdaReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
// The `processBeforeResponse` option is required for all FaaS environments.
// It allows Bolt methods (e.g. `app.message`) to handle a Slack request
// before the Bolt framework responds to the request (e.g. `ack()`). This is
// important because FaaS immediately terminate handlers after the response.
processBeforeResponse: true
});

// Initializes your app with your bot token and the AWS Lambda ready receiver
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
receiver: expressReceiver
receiver: awsLambdaReceiver,
// The `processBeforeResponse` option is required for all FaaS environments.
// It allows Bolt methods (e.g. `app.message`) to handle a Slack request
// before the Bolt framework responds to the request (e.g. `ack()`). This is
// important because FaaS immediately terminate handlers after the response.
processBeforeResponse: true
});

// Listens to incoming messages that contain "hello"
Expand Down Expand Up @@ -57,6 +56,7 @@ app.message('goodbye', async ({ message, say }) => {
});

// Handle the Lambda function event
module.exports.handler = serverlessExpress({
app: expressReceiver.app
});
module.exports.handler = async (event, context, callback) => {
const handler = await app.start();
return handler(event, context, callback);
}
7 changes: 3 additions & 4 deletions examples/deploy-aws-lambda/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"name": "bolt-js-aws-lambda",
"name": "bolt-js-aws-lambda-receiver",
"version": "1.0.0",
"description": "Deploying to AWS Lambda ⚡️ Bolt for JavaScript",
"description": "Deploying to AWS Lambda ⚡️ Bolt for JavaScript using the AwsLambdaReceiver",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "MIT",
"dependencies": {
"@slack/bolt": "^3.2.0",
"@vendia/serverless-express": "^4.3.2"
"@slack/bolt": "^3.2.0"
},
"devDependencies": {
"serverless": "^2.13.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/deploy-aws-lambda/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
service: serverless-bolt-js
service: serverless-aws-lambda-receiver-bolt-js
frameworkVersion: '2'
provider:
name: aws
Expand All @@ -14,4 +14,4 @@ functions:
path: slack/events
method: post
plugins:
- serverless-offline
- serverless-offline

0 comments on commit 7698953

Please sign in to comment.