Skip to content
31 changes: 1 addition & 30 deletions apps/sim/app/api/tools/slack/add-reaction/route.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import { type NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import { checkHybridAuth } from '@/lib/auth/hybrid'
import { generateRequestId } from '@/lib/core/utils/request'
import { createLogger } from '@/lib/logs/console/logger'

export const dynamic = 'force-dynamic'

const logger = createLogger('SlackAddReactionAPI')

const SlackAddReactionSchema = z.object({
accessToken: z.string().min(1, 'Access token is required'),
channel: z.string().min(1, 'Channel ID is required'),
channel: z.string().min(1, 'Channel is required'),
timestamp: z.string().min(1, 'Message timestamp is required'),
name: z.string().min(1, 'Emoji name is required'),
})

export async function POST(request: NextRequest) {
const requestId = generateRequestId()

try {
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })

if (!authResult.success) {
logger.warn(`[${requestId}] Unauthorized Slack add reaction attempt: ${authResult.error}`)
return NextResponse.json(
{
success: false,
Expand All @@ -32,22 +25,9 @@ export async function POST(request: NextRequest) {
)
}

logger.info(
`[${requestId}] Authenticated Slack add reaction request via ${authResult.authType}`,
{
userId: authResult.userId,
}
)

const body = await request.json()
const validatedData = SlackAddReactionSchema.parse(body)

logger.info(`[${requestId}] Adding Slack reaction`, {
channel: validatedData.channel,
timestamp: validatedData.timestamp,
emoji: validatedData.name,
})

const slackResponse = await fetch('https://slack.com/api/reactions.add', {
method: 'POST',
headers: {
Expand All @@ -64,7 +44,6 @@ export async function POST(request: NextRequest) {
const data = await slackResponse.json()

if (!data.ok) {
logger.error(`[${requestId}] Slack API error:`, data)
return NextResponse.json(
{
success: false,
Expand All @@ -74,12 +53,6 @@ export async function POST(request: NextRequest) {
)
}

logger.info(`[${requestId}] Reaction added successfully`, {
channel: validatedData.channel,
timestamp: validatedData.timestamp,
reaction: validatedData.name,
})

return NextResponse.json({
success: true,
output: {
Expand All @@ -93,7 +66,6 @@ export async function POST(request: NextRequest) {
})
} catch (error) {
if (error instanceof z.ZodError) {
logger.warn(`[${requestId}] Invalid request data`, { errors: error.errors })
return NextResponse.json(
{
success: false,
Expand All @@ -104,7 +76,6 @@ export async function POST(request: NextRequest) {
)
}

logger.error(`[${requestId}] Error adding Slack reaction:`, error)
return NextResponse.json(
{
success: false,
Expand Down
29 changes: 1 addition & 28 deletions apps/sim/app/api/tools/slack/delete-message/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import { type NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import { checkHybridAuth } from '@/lib/auth/hybrid'
import { generateRequestId } from '@/lib/core/utils/request'
import { createLogger } from '@/lib/logs/console/logger'

export const dynamic = 'force-dynamic'

const logger = createLogger('SlackDeleteMessageAPI')

const SlackDeleteMessageSchema = z.object({
accessToken: z.string().min(1, 'Access token is required'),
channel: z.string().min(1, 'Channel ID is required'),
channel: z.string().min(1, 'Channel is required'),
timestamp: z.string().min(1, 'Message timestamp is required'),
})

export async function POST(request: NextRequest) {
const requestId = generateRequestId()

try {
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })

if (!authResult.success) {
logger.warn(`[${requestId}] Unauthorized Slack delete message attempt: ${authResult.error}`)
return NextResponse.json(
{
success: false,
Expand All @@ -31,21 +24,9 @@ export async function POST(request: NextRequest) {
)
}

logger.info(
`[${requestId}] Authenticated Slack delete message request via ${authResult.authType}`,
{
userId: authResult.userId,
}
)

const body = await request.json()
const validatedData = SlackDeleteMessageSchema.parse(body)

logger.info(`[${requestId}] Deleting Slack message`, {
channel: validatedData.channel,
timestamp: validatedData.timestamp,
})

const slackResponse = await fetch('https://slack.com/api/chat.delete', {
method: 'POST',
headers: {
Expand All @@ -61,7 +42,6 @@ export async function POST(request: NextRequest) {
const data = await slackResponse.json()

if (!data.ok) {
logger.error(`[${requestId}] Slack API error:`, data)
return NextResponse.json(
{
success: false,
Expand All @@ -71,11 +51,6 @@ export async function POST(request: NextRequest) {
)
}

logger.info(`[${requestId}] Message deleted successfully`, {
channel: data.channel,
timestamp: data.ts,
})

return NextResponse.json({
success: true,
output: {
Expand All @@ -88,7 +63,6 @@ export async function POST(request: NextRequest) {
})
} catch (error) {
if (error instanceof z.ZodError) {
logger.warn(`[${requestId}] Invalid request data`, { errors: error.errors })
return NextResponse.json(
{
success: false,
Expand All @@ -99,7 +73,6 @@ export async function POST(request: NextRequest) {
)
}

logger.error(`[${requestId}] Error deleting Slack message:`, error)
return NextResponse.json(
{
success: false,
Expand Down
207 changes: 207 additions & 0 deletions apps/sim/app/api/tools/slack/read-messages/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import { type NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import { checkHybridAuth } from '@/lib/auth/hybrid'
import { generateRequestId } from '@/lib/core/utils/request'
import { createLogger } from '@/lib/logs/console/logger'
import { openDMChannel } from '../utils'

export const dynamic = 'force-dynamic'

const logger = createLogger('SlackReadMessagesAPI')

const SlackReadMessagesSchema = z
.object({
accessToken: z.string().min(1, 'Access token is required'),
channel: z.string().optional().nullable(),
userId: z.string().optional().nullable(),
limit: z.number().optional().nullable(),
oldest: z.string().optional().nullable(),
latest: z.string().optional().nullable(),
})
.refine((data) => data.channel || data.userId, {
message: 'Either channel or userId is required',
})

export async function POST(request: NextRequest) {
const requestId = generateRequestId()

try {
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })

if (!authResult.success) {
logger.warn(`[${requestId}] Unauthorized Slack read messages attempt: ${authResult.error}`)
return NextResponse.json(
{
success: false,
error: authResult.error || 'Authentication required',
},
{ status: 401 }
)
}

logger.info(
`[${requestId}] Authenticated Slack read messages request via ${authResult.authType}`,
{
userId: authResult.userId,
}
)

const body = await request.json()
const validatedData = SlackReadMessagesSchema.parse(body)

let channel = validatedData.channel
if (!channel && validatedData.userId) {
logger.info(`[${requestId}] Opening DM channel for user: ${validatedData.userId}`)
channel = await openDMChannel(
validatedData.accessToken,
validatedData.userId,
requestId,
logger
)
}

const url = new URL('https://slack.com/api/conversations.history')
url.searchParams.append('channel', channel!)
const limit = validatedData.limit ? Number(validatedData.limit) : 10
url.searchParams.append('limit', String(Math.min(limit, 15)))

if (validatedData.oldest) {
url.searchParams.append('oldest', validatedData.oldest)
}
if (validatedData.latest) {
url.searchParams.append('latest', validatedData.latest)
}

logger.info(`[${requestId}] Reading Slack messages`, {
channel,
limit,
})

const slackResponse = await fetch(url.toString(), {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${validatedData.accessToken}`,
},
})

const data = await slackResponse.json()

if (!data.ok) {
logger.error(`[${requestId}] Slack API error:`, data)

if (data.error === 'not_in_channel') {
return NextResponse.json(
{
success: false,
error:
'Bot is not in the channel. Please invite the Sim bot to your Slack channel by typing: /invite @Sim Studio',
},
{ status: 400 }
)
}
if (data.error === 'channel_not_found') {
return NextResponse.json(
{
success: false,
error: 'Channel not found. Please check the channel ID and try again.',
},
{ status: 400 }
)
}
if (data.error === 'missing_scope') {
return NextResponse.json(
{
success: false,
error:
'Missing required permissions. Please reconnect your Slack account with the necessary scopes (channels:history, groups:history, im:history).',
},
{ status: 400 }
)
}

return NextResponse.json(
{
success: false,
error: data.error || 'Failed to fetch messages',
},
{ status: 400 }
)
}

const messages = (data.messages || []).map((message: any) => ({
type: message.type || 'message',
ts: message.ts,
text: message.text || '',
user: message.user,
bot_id: message.bot_id,
username: message.username,
channel: message.channel,
team: message.team,
thread_ts: message.thread_ts,
parent_user_id: message.parent_user_id,
reply_count: message.reply_count,
reply_users_count: message.reply_users_count,
latest_reply: message.latest_reply,
subscribed: message.subscribed,
last_read: message.last_read,
unread_count: message.unread_count,
subtype: message.subtype,
reactions: message.reactions?.map((reaction: any) => ({
name: reaction.name,
count: reaction.count,
users: reaction.users || [],
})),
is_starred: message.is_starred,
pinned_to: message.pinned_to,
files: message.files?.map((file: any) => ({
id: file.id,
name: file.name,
mimetype: file.mimetype,
size: file.size,
url_private: file.url_private,
permalink: file.permalink,
mode: file.mode,
})),
attachments: message.attachments,
blocks: message.blocks,
edited: message.edited
? {
user: message.edited.user,
ts: message.edited.ts,
}
: undefined,
permalink: message.permalink,
}))

logger.info(`[${requestId}] Successfully read ${messages.length} messages`)

return NextResponse.json({
success: true,
output: {
messages,
},
})
} catch (error) {
if (error instanceof z.ZodError) {
logger.warn(`[${requestId}] Invalid request data`, { errors: error.errors })
return NextResponse.json(
{
success: false,
error: 'Invalid request data',
details: error.errors,
},
{ status: 400 }
)
}

logger.error(`[${requestId}] Error reading Slack messages:`, error)
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : 'Unknown error occurred',
},
{ status: 500 }
)
}
}
Loading
Loading