|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { type NextRequest, NextResponse } from 'next/server' |
| 3 | +import { z } from 'zod' |
| 4 | +import { checkInternalAuth } from '@/lib/auth/hybrid' |
| 5 | +import { generateRequestId } from '@/lib/core/utils/request' |
| 6 | + |
| 7 | +export const dynamic = 'force-dynamic' |
| 8 | + |
| 9 | +const logger = createLogger('SlackSendEphemeralAPI') |
| 10 | + |
| 11 | +const SlackSendEphemeralSchema = z.object({ |
| 12 | + accessToken: z.string().min(1, 'Access token is required'), |
| 13 | + channel: z.string().min(1, 'Channel ID is required'), |
| 14 | + user: z.string().min(1, 'User ID is required'), |
| 15 | + text: z.string().min(1, 'Message text is required'), |
| 16 | + thread_ts: z.string().optional().nullable(), |
| 17 | + blocks: z.array(z.record(z.unknown())).optional().nullable(), |
| 18 | +}) |
| 19 | + |
| 20 | +export async function POST(request: NextRequest) { |
| 21 | + const requestId = generateRequestId() |
| 22 | + |
| 23 | + try { |
| 24 | + const authResult = await checkInternalAuth(request, { requireWorkflowId: false }) |
| 25 | + |
| 26 | + if (!authResult.success) { |
| 27 | + logger.warn(`[${requestId}] Unauthorized Slack ephemeral send attempt: ${authResult.error}`) |
| 28 | + return NextResponse.json( |
| 29 | + { |
| 30 | + success: false, |
| 31 | + error: authResult.error || 'Authentication required', |
| 32 | + }, |
| 33 | + { status: 401 } |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + logger.info( |
| 38 | + `[${requestId}] Authenticated Slack ephemeral send request via ${authResult.authType}`, |
| 39 | + { userId: authResult.userId } |
| 40 | + ) |
| 41 | + |
| 42 | + const body = await request.json() |
| 43 | + const validatedData = SlackSendEphemeralSchema.parse(body) |
| 44 | + |
| 45 | + logger.info(`[${requestId}] Sending ephemeral message`, { |
| 46 | + channel: validatedData.channel, |
| 47 | + user: validatedData.user, |
| 48 | + threadTs: validatedData.thread_ts ?? undefined, |
| 49 | + }) |
| 50 | + |
| 51 | + const response = await fetch('https://slack.com/api/chat.postEphemeral', { |
| 52 | + method: 'POST', |
| 53 | + headers: { |
| 54 | + 'Content-Type': 'application/json', |
| 55 | + Authorization: `Bearer ${validatedData.accessToken}`, |
| 56 | + }, |
| 57 | + body: JSON.stringify({ |
| 58 | + channel: validatedData.channel, |
| 59 | + user: validatedData.user, |
| 60 | + text: validatedData.text, |
| 61 | + ...(validatedData.thread_ts && { thread_ts: validatedData.thread_ts }), |
| 62 | + ...(validatedData.blocks && |
| 63 | + validatedData.blocks.length > 0 && { blocks: validatedData.blocks }), |
| 64 | + }), |
| 65 | + }) |
| 66 | + |
| 67 | + const data = await response.json() |
| 68 | + |
| 69 | + if (!data.ok) { |
| 70 | + logger.error(`[${requestId}] Slack API error:`, data.error) |
| 71 | + return NextResponse.json( |
| 72 | + { success: false, error: data.error || 'Failed to send ephemeral message' }, |
| 73 | + { status: 400 } |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + logger.info(`[${requestId}] Ephemeral message sent successfully`) |
| 78 | + |
| 79 | + return NextResponse.json({ |
| 80 | + success: true, |
| 81 | + output: { |
| 82 | + messageTs: data.message_ts, |
| 83 | + channel: validatedData.channel, |
| 84 | + }, |
| 85 | + }) |
| 86 | + } catch (error) { |
| 87 | + logger.error(`[${requestId}] Error sending ephemeral message:`, error) |
| 88 | + return NextResponse.json( |
| 89 | + { |
| 90 | + success: false, |
| 91 | + error: error instanceof Error ? error.message : 'Unknown error occurred', |
| 92 | + }, |
| 93 | + { status: 500 } |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
0 commit comments