Skip to content

Instantly share code, notes, and snippets.

@seratch
Created March 31, 2022 05:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seratch/2b97e752645e83322a1066a9c24e2a20 to your computer and use it in GitHub Desktop.
Save seratch/2b97e752645e83322a1066a9c24e2a20 to your computer and use it in GitHub Desktop.
@slack/bolt meets Fastify
import Fastify from 'fastify';
import { App, FileInstallationStore, LogLevel } from '@slack/bolt';
import { FileStateStore } from '@slack/oauth';
import { FastifyReceiver } from 'slack-bolt-fastify';
const fastify = Fastify({ logger: true });
fastify.get('/', async (_, res) => {
res.redirect('/slack/install');
});
const receiver = new FastifyReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET!,
clientId: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
scopes: ['commands', 'chat:write', 'app_mentions:read'],
installationStore: new FileInstallationStore(),
installerOptions: {
directInstall: true,
stateStore: new FileStateStore({}),
},
fastify,
});
const app = new App({
logLevel: LogLevel.DEBUG,
receiver,
});
app.event('app_mention', async ({ event, say }) => {
await say({
text: `<@${event.user}> Hi there :wave:`,
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `<@${event.user}> Hi there :wave:`,
},
},
],
});
});
app.command('/my-command', async ({ ack }) => {
await ack('Hi there!');
});
app.shortcut('my-global-shortcut', async ({ ack, body, client }) => {
await ack();
await client.views.open({
trigger_id: body.trigger_id,
view: {
type: 'modal',
callback_id: 'my-modal',
title: {
type: 'plain_text',
text: 'My App',
},
submit: {
type: 'plain_text',
text: 'Submit',
},
blocks: [
{
type: 'input',
block_id: 'b',
element: {
type: 'plain_text_input',
action_id: 'a',
},
label: {
type: 'plain_text',
text: 'Comment',
},
},
],
},
});
});
app.view('my-modal', async ({ view, ack, logger }) => {
logger.info(view.state.values);
await ack();
});
(async () => {
await app.start();
// eslint-disable-next-line no-console
console.log('⚡️ Bolt app is running!');
})();
{
"name": "fastify-example-app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "npm run build && npx node app.js",
"build": "tsc -p ."
},
"keywords": [],
"author": "Kazuhiro Sera (@seratch)",
"license": "MIT",
"dependencies": {
"@slack/bolt": "^3.11.0",
"fastify": "^3.27.4",
"slack-bolt-fastify": "^0.3.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
}
}
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es6", "dom"],
"outDir": "lib",
"rootDir": "src",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment