home / skills / linehaul-ai / linehaulai-claude-marketplace / slack-realtime-events

This skill helps you build real-time Slack apps by handling messages, mentions, interactions, and commands with Socket Mode or Events API.

npx playbooks add skill linehaul-ai/linehaulai-claude-marketplace --skill slack-realtime-events

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
5.7 KB
---
name: slack-realtime-events
description: Real-time event handling with Socket Mode and Events API. Use when building interactive Slack bots, handling message events, app mentions, reactions, button clicks, modal submissions, slash commands, or any event-driven Slack application functionality.
keywords: [slack, socket-mode, events-api, real-time, webhooks]
---

# Slack Real-time Events

## Socket Mode vs Events API

### Socket Mode (WebSocket)
**Use when**:
- App runs behind a firewall
- No public HTTPS endpoint available
- Development and testing
- Running on localhost

```go
import "github.com/slack-go/slack/socketmode"

client := socketmode.New(
    api,
    socketmode.OptionDebug(true),
)
```

See [socket-mode-setup.md](../../references/socket-mode-setup.md) for complete Socket Mode implementation.

### Events API (HTTP Webhooks)
**Use when**:
- Public HTTPS endpoint available
- Production cloud deployments
- Webhook-based architecture preferred

See [events-api-webhooks.md](../../references/events-api-webhooks.md) for HTTP webhook patterns.

## Socket Mode Basic Setup

```go
api := slack.New(
    os.Getenv("SLACK_BOT_TOKEN"),
    slack.OptionAppLevelToken(os.Getenv("SLACK_APP_TOKEN")),
)

client := socketmode.New(api)

go func() {
    for envelope := range client.Events {
        switch envelope.Type {
        case socketmode.EventTypeEventsAPI:
            client.Ack(*envelope.Request)
            // Handle event
        }
    }
}()

client.Run()
```

## Event Handling Patterns

### Message Events

```go
func handleMessageEvent(event *slackevents.MessageEvent, api *slack.Client) {
    // Ignore bot messages
    if event.BotID != "" {
        return
    }

    fmt.Printf("Message from %s in %s: %s\n", event.User, event.Channel, event.Text)

    // Respond
    api.PostMessage(event.Channel, slack.MsgOptionText("Got your message!", false))
}
```

### App Mention Events

```go
func handleAppMention(event *slackevents.AppMentionEvent, api *slack.Client) {
    text := strings.TrimSpace(strings.Replace(event.Text, fmt.Sprintf("<@%s>", botUserID), "", 1))

    response := processCommand(text)

    api.PostMessage(
        event.Channel,
        slack.MsgOptionText(response, false),
        slack.MsgOptionTS(event.TimeStamp), // Reply in thread
    )
}
```

### Reaction Events

```go
func handleReaction(event *slackevents.ReactionAddedEvent, api *slack.Client) {
    fmt.Printf("User %s added reaction :%s: to message %s\n",
        event.User, event.Reaction, event.Item.Timestamp)

    // React back
    api.AddReaction(event.Reaction, slack.ItemRef{
        Channel:   event.Item.Channel,
        Timestamp: event.Item.Timestamp,
    })
}
```

## Interactive Components

### Button Clicks

```go
func handleButtonClick(interaction slack.InteractionCallback, api *slack.Client) {
    action := interaction.ActionCallback.BlockActions[0]

    switch action.ActionID {
    case "approve_deployment":
        // Handle approval
        updateMessage(api, interaction.Channel.ID, interaction.Message.Timestamp, "Approved!")

    case "reject_deployment":
        // Handle rejection
        updateMessage(api, interaction.Channel.ID, interaction.Message.Timestamp, "Rejected!")
    }
}
```

### Modal Submissions

```go
func handleModalSubmission(interaction slack.InteractionCallback, api *slack.Client) {
    values := interaction.View.State.Values

    // Extract form data
    rating := values["rating_block"]["rating_select"].SelectedOption.Value
    comments := values["comments_block"]["comments_input"].Value

    // Process submission
    fmt.Printf("Feedback: %s stars - %s\n", rating, comments)

    // Acknowledge
    api.UpdateView(slack.View{}, interaction.View.ExternalID, "", interaction.View.ID)
}
```

See [interactive-components.md](../../references/interactive-components.md) for comprehensive interactive patterns.

## Slash Commands

Handle slash command invocations:

```go
func handleSlashCommand(command slack.SlashCommand, api *slack.Client) slack.Message {
    switch command.Command {
    case "/deploy":
        return slack.Message{
            Text: fmt.Sprintf("Deploying %s to %s...", command.Text, "production"),
        }

    case "/status":
        return slack.Message{
            Text: "All systems operational",
        }

    default:
        return slack.Message{
            Text: "Unknown command",
        }
    }
}
```

See [slash-commands.md](../../references/slash-commands.md) for command patterns and delayed responses.

## Event Types

Common event types to handle:

- **message** - New messages in channels
- **app_mention** - Bot mentioned in message
- **reaction_added/removed** - Message reactions
- **channel_created/renamed** - Channel lifecycle
- **team_join** - New workspace members
- **user_change** - User profile updates

See [event-types.md](../../references/event-types.md) for comprehensive event catalog.

## Connection Management

### Graceful Shutdown

```go
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
    sigChan := make(chan os.Signal, 1)
    signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
    <-sigChan
    cancel()
}()

client.RunContext(ctx)
```

### Error Handling

```go
for envelope := range client.Events {
    switch envelope.Type {
    case socketmode.EventTypeErrorBadMessage:
        fmt.Printf("Error: Bad message - %v\n", envelope.Request)

    case socketmode.EventTypeConnectionError:
        fmt.Printf("Connection error: %v\n", envelope.Request)
    }
}
```

## Common Pitfalls

- Not acknowledging events (causes timeouts)
- Blocking in event handlers (use goroutines for long operations)
- Missing bot vs user message filtering
- Not handling reconnections in Socket Mode
- Forgetting to verify request signatures in Events API

Overview

This skill implements real-time Slack event handling using Socket Mode and the Events API for Go-based apps. It provides patterns for handling messages, app mentions, reactions, interactive components, slash commands, and connection management. Use it to build responsive, event-driven Slack bots and integrations that run locally, behind firewalls, or in production cloud environments.

How this skill works

The skill wires a slack-go client to either Socket Mode (WebSocket) or the Events API (HTTP webhooks). It reads incoming envelopes or HTTP requests, acknowledges events, dispatches handlers for event types (message, app_mention, reaction, etc.), and responds via the Slack Web API. It includes patterns for interactive components (buttons, modals), slash commands, graceful shutdown, and error handling.

When to use it

  • Building interactive Slack bots that must respond in real time
  • Running a Slack app on localhost or behind a firewall (use Socket Mode)
  • Deploying to production with a public HTTPS endpoint (use Events API)
  • Handling button clicks, modal submissions, reactions, or slash commands
  • Creating event-driven automations that react to workspace activity

Best practices

  • Acknowledge incoming events immediately to avoid timeouts
  • Filter out bot-generated messages to prevent loops
  • Run long-running work in goroutines to avoid blocking handlers
  • Verify Events API request signatures in HTTP handlers
  • Implement reconnection and error handling for Socket Mode clients
  • Use graceful shutdown with context cancellation to stop cleanly

Example use cases

  • A helpdesk bot that replies to app mentions and opens modals for ticket details
  • CI/CD approval workflow that uses button actions to approve or reject deployments
  • Feedback collector that opens a modal, processes submission, and stores results
  • Monitoring bot that reacts to keywords and posts status updates in channels
  • Slash-command driven utilities like /deploy or /status with immediate or delayed responses

FAQ

When should I choose Socket Mode over the Events API?

Choose Socket Mode when your app cannot expose a public HTTPS endpoint (local development, firewalled environments) or when you prefer a single websocket connection. Use Events API when you have a reliable public endpoint and webhook-based architecture.

How do I avoid duplicate or looping responses?

Ignore events originating from your bot (check BotID), acknowledge events promptly, and design idempotent handlers for retries or repeated deliveries.