Slack Bridge Integration
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Slack Bridge Integration provides a native connection between Matterbridge and the Slack platform. Its primary purpose is to facilitate real-time message relaying, file transfers, and event synchronization (such as topic changes and message deletions) between Slack channels and other supported chat protocols. This integration allows users on different platforms to communicate seamlessly within a unified gateway environment.
The bridge supports multiple connection methods, including modern Slack Bot tokens (Real-Time Messaging API) and legacy Webhooks. It utilizes the slack-go/slack library to manage WebSocket-based RTM connections for bidirectional communication or falls back to incoming and outgoing webhooks when tokens are unavailable.
Sources: bridge/slack/slack.go:1-10, README.md:104, README.md:195-212
Architecture and Connection Methods
The Slack bridge supports two distinct architectures for message handling based on the provided configuration: the RTM (Real-Time Messaging) method and the Webhook method.
Real-Time Messaging (RTM)
When a Bot Token (starting with xoxb) is provided, the bridge initializes a persistent WebSocket connection. This method is preferred as it handles both sending and receiving through a single managed connection and provides high-fidelity event support.
Webhook Fallback
In the absence of a token, the bridge utilizes a local webhook server to receive messages and an outgoing webhook URL to send them. This method is more limited and does not support advanced features like native file uploads or threaded message tracking as effectively as RTM.
The diagram above illustrates the decision logic for selecting the connection architecture.
Sources: bridge/slack/slack.go:94-135, bridge/slack/slack.go:197-246
Configuration Options
The Slack bridge is configured within the matterbridge.toml file. Key settings determine how the bridge identifies itself and handles message formatting.
| Key | Type | Description |
|---|---|---|
Token | String | Slack Bot token (recommended xoxb- prefix). |
WebhookBindAddress | String | Address for receiving incoming webhooks. |
WebhookURL | String | Slack URL for sending outgoing messages via webhooks. |
PrefixMessagesWithNick | Boolean | If true, prepends the sender's nickname to the message. |
SyncTopic | Boolean | Whether to synchronize channel topic changes. |
ShowUserTyping | Boolean | Whether to relay typing indicators to Slack. |
IconURL | String | Default icon URL for the bot avatar. |
Sources: bridge/slack/slack.go:56-72, README.md:214-225
Message Handling and Data Flow
Outgoing Messages (To Slack)
The bridge processes incoming messages from the gateway and converts them into Slack-compatible formats. This includes handling text, actions (/me), message edits, deletions, and file uploads.
This sequence shows the transformation of a generic gateway message into specific Slack API calls.
Sources: bridge/slack/slack.go:167-195, bridge/slack/slack.go:283-335
Handling Files and Attachments
Matterbridge supports native file uploads for Slack. When a message contains file information, the bridge uses the sc.UploadFile method. It also maintains a local LRU cache to track file IDs and avoid duplication or to correlate file messages.
- File Uploads: Uses
slack.FileUploadParametersto send data, filenames, and initial comments. - Slack Attachments: Supports native
slack.Attachmentstructures passed through theExtradata field.
Sources: bridge/slack/slack.go:338-376, bridge/slack/slack.go:406-431
Key Components and Functions
The implementation relies on several internal structures to manage state and Slack-specific logic:
BslackStruct: The main bridge instance containing the Slack client (sc), RTM client (rtm), and managers for users and channels.JoinChannel(): Validates that the bot is a member of the configured Slack channel. Unlike some protocols, Slack bots must be manually invited to channels.prepareMessageOptions(): Formats messages usingslack.MsgOptionand adds unique block IDs (e.g.,matterbridge_[uuid]) to identify messages created by the bridge.handleTopicOrPurpose(): Manages the synchronization of channel metadata.
Sources: bridge/slack/slack.go:23-42, bridge/slack/slack.go:139-165, bridge/slack/slack.go:378-404
Summary
The Slack Bridge Integration is a robust module within Matterbridge that leverages the Slack RTM and Webhook APIs to provide comprehensive chat synchronization. By supporting both modern bot tokens and legacy webhooks, it ensures compatibility across different Slack workspace configurations while maintaining features like thread support, file uploads, and nickname spoofing.
Sources: bridge/slack/slack.go:75-92, README.md:100-110