Skip to content

Instantly share code, notes, and snippets.

@seratch
Created March 4, 2020 12:36
Show Gist options
  • Save seratch/4fecc19acefa1eaed07751da8d9ebbc7 to your computer and use it in GitHub Desktop.
Save seratch/4fecc19acefa1eaed07751da8d9ebbc7 to your computer and use it in GitHub Desktop.
Bolt JS authorize function example
const { App } = require('@slack/bolt');
const installations = [
{
teamId: 'T12345678', // Copy a message URL to get the value
botToken: 'xoxb-123-123-random', // Install the app from http://api.slack.com/apps
botId: 'B12345678', // Run https://api.slack.com/methods/users.info/test with bot user id
botUserId: 'U12345678', // To get this value, mention the bot user and inspect the message with Slack Developer Tools
},
];
const authorizeFn = async ({ teamId, enterpriseId }) => {
// Fetch team info from database
for (const team of installations) {
// Check for matching teamId and enterpriseId in the installations array
if ((team.teamId === teamId) && (team.enterpriseId === enterpriseId)) {
// This is a match. Use these installation credentials.
return {
// You could also set userToken instead
botToken: team.botToken,
botId: team.botId,
botUserId: team.botUserId
};
}
}
throw new Error('No matching authorizations');
};
const app = new App({
authorize: authorizeFn,
logLevel: 'debug',
signingSecret: process.env.SLACK_SIGNING_SECRET
});
app.command('/ping', async ({ command, ack, say }) => {
say(`${command.text}`);
ack();
});
app.use(args => {
const copiedArgs = JSON.parse(JSON.stringify(args));
copiedArgs.context.botToken = 'xoxb-***';
if (copiedArgs.context.userToken) {
copiedArgs.context.userToken = 'xoxp-***';
}
copiedArgs.client = {};
copiedArgs.logger = {};
args.logger.debug(
"Dumping request data for debugging...\n\n" +
JSON.stringify(copiedArgs, null, 2) +
"\n"
);
args.next();
});
(async () => {
await app.start(process.env.PORT || 3000);
console.log('⚡️ Bolt app is running!');
})();
{
"name": "bolt-oauth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@slack/bolt": "^1.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment