Implementation:Langfuse Langfuse SlackService
| Knowledge Sources | |
|---|---|
| Domains | Integrations, Slack, Notifications |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
The SlackService is a singleton service class that manages Slack OAuth integration, channel listing, and message sending for Langfuse projects using the official Slack SDK libraries.
Description
The SlackService class handles the complete Slack integration lifecycle for Langfuse projects. It is implemented as a singleton (via getInstance()) and wraps the Slack SDK's InstallProvider for OAuth flow management and WebClient for API operations.
Key components and behaviors:
- OAuth flow management - Uses
@slack/oauthInstallProviderwith a custominstallationStorethat persists Slack installations to PostgreSQL via theslackIntegrationPrisma model. Each project maps to one Slack workspace integration. - Encrypted token storage - Bot tokens are encrypted via
encrypt()before storage and decrypted viadecrypt()on retrieval, ensuring sensitive credentials are not stored in plaintext. - Metadata-based project mapping - Installation metadata carries the
projectId, parsed and validated byparseSlackInstallationMetadata. ThefetchInstallationcallback supports lookup by bothteamIdandprojectId. - Channel listing -
getChannelsrecursively fetches all public, non-archived channels using cursor-based pagination, respecting theSLACK_FETCH_LIMITenvironment variable. - Message sending -
sendMessageposts Block Kit messages to a specified channel with link unfurling disabled. - Client management -
getWebClientForProjectauthorizes and returns aWebClientinstance for a given project. - Client validation -
validateClienttests whether aWebClientis still valid viaauth.test(). - Integration deletion -
deleteIntegrationremoves the Slack integration for a project.
The requested OAuth scopes are: channels:read, chat:write, chat:write.public.
Usage
Use this service when you need to:
- Initiate or handle the Slack OAuth installation flow for a project.
- Retrieve the list of Slack channels a bot can post to.
- Send notification messages (e.g., evaluation alerts) to a Slack channel.
- Validate or refresh a Slack connection for a project.
- Remove a Slack integration from a project.
Code Reference
Source Location
- Repository: Langfuse
- File: packages/shared/src/server/services/SlackService.ts
- Lines: 1-427
Signature
export interface SlackChannel {
id: string;
name: string;
isPrivate: boolean;
isMember: boolean;
}
export interface SlackMessageParams {
client: WebClient;
channelId: string;
blocks: any[];
text?: string;
}
export interface SlackMessageResponse {
messageTs: string;
channel: string;
}
export function parseSlackInstallationMetadata(
metadata: unknown,
): SlackInstallationMetadata;
export class SlackService {
static getInstance(): SlackService;
static resetInstance(): void;
getInstaller(): InstallProvider;
async deleteIntegration(projectId: string): Promise<void>;
async getWebClientForProject(projectId: string): Promise<WebClient>;
async getChannels(client: WebClient): Promise<SlackChannel[]>;
async sendMessage(params: SlackMessageParams): Promise<SlackMessageResponse>;
async validateClient(client: WebClient): Promise<boolean>;
}
Import
import { SlackService, SlackChannel, SlackMessageParams } from "@langfuse/shared/src/server/services/SlackService";
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| projectId | string | Yes | The project ID for retrieving or deleting Slack integrations |
| client | WebClient | Yes (for channels/messages) | An authorized Slack WebClient instance |
| channelId | string | Yes (for sending) | The Slack channel ID to send a message to |
| blocks | any[] | Yes (for sending) | Slack Block Kit blocks composing the message body |
| text | string | No | Fallback plain text for the message (defaults to "Langfuse Notification") |
| metadata | unknown | Yes (for parsing) | Raw installation metadata string containing projectId JSON |
Outputs
| Name | Type | Description |
|---|---|---|
| SlackService | SlackService | Singleton instance of the service |
| WebClient | WebClient | Authorized Slack Web API client for a specific project |
| SlackChannel[] | array | List of accessible Slack channels with id, name, privacy, and membership status |
| SlackMessageResponse | object | Contains messageTs (Slack timestamp ID) and channel for the sent message |
| boolean | boolean | Client validation result (true if auth.test succeeds) |
Usage Examples
import { SlackService } from "@langfuse/shared/src/server/services/SlackService";
// Get the singleton instance
const slackService = SlackService.getInstance();
// Get a WebClient for a project
const client = await slackService.getWebClientForProject("proj-123");
// List available channels
const channels = await slackService.getChannels(client);
// Send a notification message
const response = await slackService.sendMessage({
client,
channelId: "C01234567",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "Evaluation *accuracy* completed with score 0.95",
},
},
],
text: "Evaluation accuracy completed with score 0.95",
});
// Validate the client connection
const isValid = await slackService.validateClient(client);
// Delete integration for a project
await slackService.deleteIntegration("proj-123");