()
| 29 | * Returns a router to be mounted at /api/slack with sub-routers for each bot |
| 30 | */ |
| 31 | export function createSlackRouter(): { aaobotRouter: Router; addieRouter: Router } { |
| 32 | const aaobotRouter = Router(); |
| 33 | // Create wrapper router for Addie that handles URL verification first |
| 34 | const addieRouter = Router(); |
| 35 | |
| 36 | // ========================================================================= |
| 37 | // MAIN AAO BOT ROUTES (mounted at /api/slack/aaobot) |
| 38 | // ========================================================================= |
| 39 | |
| 40 | // POST /api/slack/aaobot/commands - Handle Slack slash commands |
| 41 | aaobotRouter.post( |
| 42 | '/commands', |
| 43 | slackUrlencodedParser(), |
| 44 | createSlackSignatureVerifier(process.env.SLACK_SIGNING_SECRET, 'AAO Bot'), |
| 45 | async (req, res) => { |
| 46 | try { |
| 47 | const command = req.body; |
| 48 | |
| 49 | // Validate it's our command |
| 50 | if (command.command !== '/aao') { |
| 51 | logger.warn({ command: command.command }, 'Unknown slash command'); |
| 52 | return res.status(400).json({ error: 'Unknown command' }); |
| 53 | } |
| 54 | |
| 55 | // Handle the command |
| 56 | const response = await handleSlashCommand(command); |
| 57 | |
| 58 | // Slack expects a 200 response within 3 seconds |
| 59 | res.json(response); |
| 60 | } catch (error) { |
| 61 | logger.error({ err: error }, 'Slack command error'); |
| 62 | res.json({ |
| 63 | response_type: 'ephemeral', |
| 64 | text: 'Sorry, there was an error processing your command. Please try again later.', |
| 65 | }); |
| 66 | } |
| 67 | } |
| 68 | ); |
| 69 | |
| 70 | // POST /api/slack/aaobot/events - Handle Slack Events API for main AAO bot |
| 71 | aaobotRouter.post( |
| 72 | '/events', |
| 73 | slackJsonParser(), |
| 74 | async (req, res) => { |
| 75 | try { |
| 76 | // Handle URL verification challenge (before signature verification) |
| 77 | if (handleUrlVerification(req, res)) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Verify the request is from Slack |
| 82 | if (isSlackSigningConfigured()) { |
| 83 | const verifier = createSlackSignatureVerifier( |
| 84 | process.env.SLACK_SIGNING_SECRET, |
| 85 | 'AAO Bot' |
| 86 | ); |
| 87 | // Run verification manually since we already parsed the body |
| 88 | const result = await new Promise<boolean>((resolve) => { |
no test coverage detected