Skip to content

Commit

Permalink
feat: handle install and oauth event
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukaii committed Mar 2, 2023
1 parent 6e4cb39 commit a2222fa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions lib/slackApp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pages/api/slack/_app.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
18 changes: 18 additions & 0 deletions pages/api/slack/install.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 18 additions & 0 deletions pages/api/slack/oauth_redirect.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit a2222fa

Please sign in to comment.