From 7698953de24cbc3e009068d01f82401c688ec7a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Z=C3=B6ller?= <83727040+TheManWhoStaresAtCode@users.noreply.github.com> Date: Sat, 19 Jun 2021 02:35:52 +0200 Subject: [PATCH] add example how to use AWSLambdaReceiver (#815) (#940) * add example how to use AWSLambdaReceiver (#815) * replace existing example for AWS deployment with the one of the AwsLambdaReceiver (#815) * update the documentation to match with the updated example (#815) * Update the receiver name to be consistent through the do Co-authored-by: Kazuhiro Sera Co-authored-by: Michael Brooks --- docs/_deployments/aws-lambda.md | 40 +++++++++-------------- examples/deploy-aws-lambda/.gitignore | 2 +- examples/deploy-aws-lambda/README.md | 6 ++-- examples/deploy-aws-lambda/app.js | 24 +++++++------- examples/deploy-aws-lambda/package.json | 7 ++-- examples/deploy-aws-lambda/serverless.yml | 4 +-- 6 files changed, 37 insertions(+), 46 deletions(-) diff --git a/docs/_deployments/aws-lambda.md b/docs/_deployments/aws-lambda.md index daf52ccdc..2abb3674b 100644 --- a/docs/_deployments/aws-lambda.md +++ b/docs/_deployments/aws-lambda.md @@ -110,38 +110,29 @@ 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 }); ``` @@ -149,9 +140,10 @@ Finally, at the bottom of your app, update the [source code that starts the HTTP ```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]. diff --git a/examples/deploy-aws-lambda/.gitignore b/examples/deploy-aws-lambda/.gitignore index 417d6dfd5..b05baa05f 100644 --- a/examples/deploy-aws-lambda/.gitignore +++ b/examples/deploy-aws-lambda/.gitignore @@ -6,4 +6,4 @@ package-lock.json .DS_Store # Serverless -.serverless/ \ No newline at end of file +.serverless/ diff --git a/examples/deploy-aws-lambda/README.md b/examples/deploy-aws-lambda/README.md index 327f44082..25863c0a8 100644 --- a/examples/deploy-aws-lambda/README.md +++ b/examples/deploy-aws-lambda/README.md @@ -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]. @@ -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 \ No newline at end of file +[slack-app-settings]: https://api.slack.com/apps diff --git a/examples/deploy-aws-lambda/app.js b/examples/deploy-aws-lambda/app.js index 19b4cbff6..87e7fd972 100644 --- a/examples/deploy-aws-lambda/app.js +++ b/examples/deploy-aws-lambda/app.js @@ -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" @@ -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); +} diff --git a/examples/deploy-aws-lambda/package.json b/examples/deploy-aws-lambda/package.json index 5836be386..15645cdab 100644 --- a/examples/deploy-aws-lambda/package.json +++ b/examples/deploy-aws-lambda/package.json @@ -1,7 +1,7 @@ { - "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", @@ -9,8 +9,7 @@ }, "license": "MIT", "dependencies": { - "@slack/bolt": "^3.2.0", - "@vendia/serverless-express": "^4.3.2" + "@slack/bolt": "^3.2.0" }, "devDependencies": { "serverless": "^2.13.0", diff --git a/examples/deploy-aws-lambda/serverless.yml b/examples/deploy-aws-lambda/serverless.yml index da1402a12..b6cfed268 100644 --- a/examples/deploy-aws-lambda/serverless.yml +++ b/examples/deploy-aws-lambda/serverless.yml @@ -1,4 +1,4 @@ -service: serverless-bolt-js +service: serverless-aws-lambda-receiver-bolt-js frameworkVersion: '2' provider: name: aws @@ -14,4 +14,4 @@ functions: path: slack/events method: post plugins: - - serverless-offline \ No newline at end of file + - serverless-offline