From a2222fab08677e5f49a64248373f2bfe4f0d1fe7 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Thu, 2 Mar 2023 22:32:54 +0800 Subject: [PATCH] feat: handle install and oauth event --- .env.example | 2 ++ lib/config.ts | 2 ++ lib/slackApp/index.ts | 7 ++++--- pages/api/slack/_app.ts | 3 ++- pages/api/slack/install.ts | 18 ++++++++++++++++++ pages/api/slack/oauth_redirect.ts | 18 ++++++++++++++++++ 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 pages/api/slack/install.ts create mode 100644 pages/api/slack/oauth_redirect.ts diff --git a/.env.example b/.env.example index a7f02b9..3cc0fc5 100644 --- a/.env.example +++ b/.env.example @@ -6,6 +6,8 @@ SLACK_DEV_MODE=false SLACK_STATE_SECRET=ZpfmpL2LWpMnbnsP4RGpLAY2dFirKNiv2dC/LtNGv8g= SLACK_CLIENT_ID= SLACK_CLIENT_SECRET= +SLACK_REDIRECT_URI=https://localhost:3000/api/slack/oauth_redirect +SLACK_SLASH_COMMAND=/zuzugo TARGET_URL= ZUZUGO_API_SECRET= diff --git a/lib/config.ts b/lib/config.ts index 5b03930..b7a726b 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -45,6 +45,8 @@ export const config = { slackClientId: process.env.SLACK_CLIENT_ID, slackClientSecret: process.env.SLACK_CLIENT_SECRET, slackStateSecret: process.env.SLACK_STATE_SECRET, + slackRedirectUri: process.env.SLACK_REDIRECT_URI, + slackSlashCommand: process.env.SLACK_SLASH_COMMAND, apiSecret, tokenLine, diff --git a/lib/slackApp/index.ts b/lib/slackApp/index.ts index c9ab42f..9d6fee4 100644 --- a/lib/slackApp/index.ts +++ b/lib/slackApp/index.ts @@ -15,7 +15,7 @@ const installationStore = new PrismaInstallationStore({ }); export function setupSlackApp(setupApp: (app: App) => void) { - const isDevMode = config.enableSocketModeForDev; + const isDevMode = config.slackDevMode; if (isDevMode) { console.log( @@ -25,15 +25,16 @@ export function setupSlackApp(setupApp: (app: App) => void) { const baseAppOptions: AppOptions = { logLevel: LogLevel.DEBUG, - token: config.slackBotToken, signingSecret: config.slackSigningSecret, clientId: config.slackClientId, clientSecret: config.slackClientSecret, - scopes: ["commands", "chat:write"], + stateSecret: config.slackStateSecret, + scopes: ["commands", "chat:write", "incoming-webhook"], installerOptions: { directInstall: true, }, installationStore, + redirectUri: config.slackRedirectUri, }; // before start diff --git a/pages/api/slack/_app.ts b/pages/api/slack/_app.ts index 03852b8..77e6739 100644 --- a/pages/api/slack/_app.ts +++ b/pages/api/slack/_app.ts @@ -1,8 +1,9 @@ +import { config } from "@/lib/config"; import { setupSlackApp } from "@/lib/slackApp"; export { appRunner } from "@/lib/slackApp"; setupSlackApp((app) => { - app.command("/zuzugo", async ({ ack, command, say, client }) => { + app.command(config.slackSlashCommand || "/zuzugo", async ({ ack, command, say, client }) => { console.log(command.text); await client.chat.postMessage({ diff --git a/pages/api/slack/install.ts b/pages/api/slack/install.ts new file mode 100644 index 0000000..689fab3 --- /dev/null +++ b/pages/api/slack/install.ts @@ -0,0 +1,18 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import { appRunner } from "./_app"; + +import { config as applicationConfig } from "@/lib/config"; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method !== "GET") { + res.status(405).json({ error: "Sorry! This endpoint does not accept your requests." }); + return; + } + + if (applicationConfig.slackDevMode) { + return res.status(200).json({ ok: true }); + } else { + await appRunner?.handleInstallPath(req, res); + } +} diff --git a/pages/api/slack/oauth_redirect.ts b/pages/api/slack/oauth_redirect.ts new file mode 100644 index 0000000..c9f994d --- /dev/null +++ b/pages/api/slack/oauth_redirect.ts @@ -0,0 +1,18 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import { appRunner } from "./_app"; + +import { config } from "@/lib/config"; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method !== "GET") { + res.status(405).json({ error: "Sorry! This endpoint does not accept your requests." }); + return; + } + + if (config.slackDevMode) { + return res.status(200).json({ ok: true }); + } else { + await appRunner?.handleCallback(req, res); + } +}