Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds friendlier config error messages to Greet and React example app #841

Merged
merged 4 commits into from Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/greet-and-react/.gitignore
@@ -1,3 +1,5 @@
# node / npm stuff
.env
/node_modules
/package-lock.json
/package-lock.json
/storage
4 changes: 2 additions & 2 deletions examples/greet-and-react/README.md
Expand Up @@ -17,12 +17,12 @@ changes
- Either clone this repo and run `npm install`
- Or visit [glitch.com/edit/#!/remix/slack-greet-and-react-example](https://glitch.com/edit/#!/remix/slack-greet-and-react-example)
2. Set the following environment variables (all available on the *Basic Information* page) to `.env` (see `.env.sample` if you're developing locally)
- `SLACK_CLIENT_ID`: You app's _Client ID_
- `SLACK_CLIENT_ID`: Your app's _Client ID_
- `SLACK_CLIENT_SECRET`: Your app's _Client Secret_
- `SLACK_SIGNING_SECRET`: Your app's _Signing Secret_
3. If you're running the app locally:
- Start the app (`node index.js`)
- In another window, start ngrok on the same port as your webserver (`ngrok http $PORT`)
- In another window, start [ngrok](https://ngrok.com/) on the same port as your webserver (`ngrok http $PORT`)

### Enable Events
1. Go back to the app settings and click on `Event Subscriptions` on the left side navigation
Expand Down
46 changes: 34 additions & 12 deletions examples/greet-and-react/index.js
@@ -1,4 +1,4 @@
// Load environment variables from `.env` file (optional)
// Load environment variables from `.env` file.
require('dotenv').config();

const { createEventAdapter } = require('@slack/events-api');
Expand All @@ -9,10 +9,15 @@ const SlackStrategy = require('@aoberoi/passport-slack').default.Strategy;
const http = require('http');
const express = require('express');

let slackEvents;
// *** Initialize event adapter using signing secret from environment variables ***
const slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET, {
includeBody: true
});
try {
slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET, {
includeBody: true
});
} catch (error) {
return logConfigurationError(['SLACK_SIGNING_SECRET']);
}

// Initialize a Local Storage object to store authorization info
// NOTE: This is an insecure method and thus for demo purposes only!
Expand All @@ -34,14 +39,20 @@ function getClientByTeamId(teamId) {
}

// Initialize Add to Slack (OAuth) helpers
passport.use(new SlackStrategy({
clientID: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
skipUserProfile: true,
}, (accessToken, scopes, team, extra, profiles, done) => {
botAuthorizationStorage.setItem(team.id, extra.bot.accessToken);
done(null, {});
}));
let strategy;
try {
strategy = new SlackStrategy({
clientID: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
skipUserProfile: true,
}, (accessToken, scopes, team, extra, profiles, done) => {
botAuthorizationStorage.setItem(team.id, extra.bot.accessToken);
done(null, {});
});
} catch (error) {
return logConfigurationError(['SLACK_CLIENT_ID', 'SLACK_CLIENT_SECRET']);
}
passport.use(strategy);

// Initialize an Express application
const app = express();
Expand Down Expand Up @@ -127,3 +138,14 @@ const port = process.env.PORT || 3000;
http.createServer(app).listen(port, () => {
console.log(`server listening on port ${port}`);
});

/**
* Logs a configuration error to console with given missing variable names.
* @param {Array} envVarNames
*/
function logConfigurationError(envVarNames) {
const description = envVarNames.length > 1 ?
`${envVarNames.join(', ')} environment variables` :
`${envVarNames[0]} environment variable`;
console.log(`***\nCould not start up the application. Have you set your ${description}?\n\nSee https://github.com/slackapi/node-slack-sdk/blob/master/examples/greet-and-react/README.md#run-locally-or-\n`);
}