Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions apps/sim/lib/webhooks/providers/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,41 @@ describe('slackHandler formatInput - interactivity (block_actions)', () => {
expect(event.text).toBe('opt_b')
expect(event.user_name).toBe('bob')
})

it('maps a threaded button click to its thread and the clicking user team', async () => {
const { input } = await slackHandler.formatInput!(
ctx({
type: 'block_actions',
user: { id: 'U3', username: 'carol', team_id: 'T-external' },
team: { id: 'T-install' },
channel: { id: 'C42', name: 'incidents' },
container: { type: 'message', message_ts: '1700000002.000300' },
message: { ts: '1700000002.000300', thread_ts: '1700000000.000100', text: '' },
actions: [{ action_id: 'followup_0', type: 'button', value: 'Check the NAT gateway' }],
})
)
const event = eventOf(input)
expect(event.channel).toBe('C42')
expect(event.thread_ts).toBe('1700000000.000100')
expect(event.message_ts).toBe('1700000002.000300')
expect(event.team_id).toBe('T-install')
expect(event.user_team_id).toBe('T-external')
expect(event.action_id).toBe('followup_0')
expect(event.action_value).toBe('Check the NAT gateway')
})

it('falls back to the installation team when the user carries no team', async () => {
const { input } = await slackHandler.formatInput!(
ctx({
type: 'block_actions',
user: { id: 'U4', username: 'dave' },
team: { id: 'T-install' },
channel: { id: 'C42' },
actions: [{ action_id: 'followup_1', type: 'button', value: 'next' }],
})
)
expect(eventOf(input).user_team_id).toBe('T-install')
})
})

describe('slackHandler formatInput - block_suggestion', () => {
Expand Down
4 changes: 4 additions & 0 deletions apps/sim/lib/webhooks/providers/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ function formatSlackSlashCommand(b: Record<string, unknown>): SlackTriggerEvent
* `trigger_id` which are needed to respond to or follow up on the interaction.
* `text` prefers the source message text, falling back to the triggering
* action's value so a blocks-only message still surfaces something useful.
* `user_team_id` is the interacting user's own team (`user.team_id`), which a
* streamed reply needs as its recipient team; in Slack Connect it can differ
* from the installation team, and it is never used for routing.
*/
function formatSlackInteractive(b: Record<string, unknown>): SlackTriggerEvent {
const event = createSlackEvent()
Expand All @@ -262,6 +265,7 @@ function formatSlackInteractive(b: Record<string, unknown>): SlackTriggerEvent {

const team = b.team as Record<string, unknown> | undefined
event.team_id = asString(team?.id) || asString(user?.team_id)
event.user_team_id = asString(user?.team_id) || event.team_id

const container = b.container as Record<string, unknown> | undefined
const message = b.message as Record<string, unknown> | undefined
Expand Down
40 changes: 40 additions & 0 deletions apps/sim/lib/webhooks/slack-execution-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ vi.mock('@/lib/webhooks/slack-stream-sessions', () => ({
unregisterSlackStreamSession: mockUnregisterSlackStreamSession,
}))

import { slackHandler } from '@/lib/webhooks/providers/slack'
import type { SlackStreamChunk } from '@/lib/webhooks/slack-agent-api'
import { SlackDeliveryError } from '@/lib/webhooks/slack-delivery-error'
import { SlackExecutionStreamController } from '@/lib/webhooks/slack-execution-stream'
Expand Down Expand Up @@ -729,6 +730,45 @@ describe('SlackExecutionStreamController', () => {
controller.assertSucceeded()
})

it('streams a button click into the thread of the clicked message for the clicking user', async () => {
const { input } = await slackHandler.formatInput!({
webhook: {},
workflow: { id: 'workflow-1', userId: 'user-1' },
body: {
type: 'block_actions',
user: { id: 'U9', username: 'erin', team_id: 'T9' },
team: { id: 'T-install' },
channel: { id: 'C777' },
message: { ts: '1700000005.000500', thread_ts: '1700000000.000100', text: '' },
actions: [{ action_id: 'followup_0', type: 'button', value: 'Check the NAT gateway' }],
},
headers: {},
requestId: 'slack-click-test',
})

await createController(BASE_CONFIG, input as Record<string, unknown>)

const target = {
channel: 'C777',
threadTs: '1700000000.000100',
initiatorUserId: 'U9',
recipientUserId: 'U9',
recipientTeamId: 'T9',
}
expect(mockRegisterSlackStreamSession).toHaveBeenCalledWith('cred-1', target, {
executionId: 'execution-1',
workflowId: 'workflow-1',
userId: 'user-1',
workspaceId: 'workspace-1',
})
expect(mockSetSlackAgentSessionStatus).toHaveBeenCalledWith(
'xoxb-token',
target,
'processing',
undefined
)
})

it('rejects credentials that do not belong to the workflow workspace', async () => {
mockGetSlackBotCredential.mockResolvedValue({
botToken: 'xoxb-token',
Expand Down
11 changes: 10 additions & 1 deletion apps/sim/lib/webhooks/slack-stream-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ describe('Slack stream response config', () => {
).toBe('Running')
})

it.each(['reaction_added', 'assistant_thread_started'])(
it('accepts button clicks as a reply-capable streaming event', () => {
expect(
normalizeSlackStreamResponseConfig(
{ eventType: 'block_actions', streamResponse: true, streamOutputs: ['agent.content'] },
{ 'block-1': { id: 'block-1', name: 'Agent' } }
)?.outputConfigs
).toEqual([{ blockId: 'block-1', path: 'content' }])
})

it.each(['reaction_added', 'assistant_thread_started', 'view_submission'])(
'rejects unsupported streaming event %s and malformed output selectors',
(eventType) => {
expect(() =>
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/triggers/slack/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const slackOAuthTrigger: TriggerConfig = {
type: 'switch',
defaultValue: false,
description:
'Create a Slack agent session and stream selected workflow outputs into the conversation that started this run. Custom bots only.',
'Create a Slack agent session and stream selected workflow outputs into the conversation that started this run (for a button click, the thread of the clicked message). Custom bots only.',
required: false,
mode: 'trigger',
condition: { field: 'eventType', value: SLACK_STREAM_RESPONSE_EVENTS },
Expand Down
6 changes: 5 additions & 1 deletion apps/sim/triggers/slack/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export const SLACK_AGENT_SCOPES = [
'im:write',
] as const

export const SLACK_STREAM_RESPONSE_EVENTS = ['message', 'app_mention']
/**
* Trigger events whose run can stream a reply into the conversation that
* started it. A `block_actions` click streams into the clicked message's thread.
*/
export const SLACK_STREAM_RESPONSE_EVENTS = ['message', 'app_mention', 'block_actions']

/**
* Unified Slack trigger output shape, shared by the legacy bring-your-own-app
Expand Down
Loading