Node Slack SDK

repository·main·Indexed 25 days ago

https://github.com/slackapi/node-slack-sdk

A collection of modular, single-purpose packages for building Slack apps, providing direct access to the Web API, OAuth, Webhooks, and Socket Mode. Includes specialized packages such as @slack/oauth for managing installation flows, @slack/rtm-api for Real Time Messaging, @slack/cli-test and @slack/cli-hooks for Slack CLI automation and integration, and @slack/logger for standardized logging.

Tokens
302.4K
Snippets
785
Records
2.1K
Agent score
82%

What's inside node-slack-sdk

  1. Overview of @slack/oauth

    main

    The @slack/oauth package simplifies the OAuth flow for Slack apps. It supports both V2 OAuth for modern Slack Apps and V1 OAuth for legacy Classic Slack apps. It is designed for apps installed in multiple workspaces (e.g., Slack Marketplace or Enterprise Grid) and handles:

    • URL generation for installation
    • State verification
    • Authorization code exchange for access tokens
    • Providing an interface to plug in custom databases for storing installation data.
  2. Overview of Slack API packages

    main

    The Node Slack SDK provides specialized packages for different Slack APIs. Use the package that matches your specific use case:

    • Web API: Use @slack/web-api to send data to or query data from Slack using over 200 methods.
    • OAuth: Use @slack/oauth to set up the V2 OAuth authentication flow for Slack apps.
    • Incoming Webhooks: Use @slack/webhook to send notifications to a specific channel.
    • Socket Mode: Use @slack/socket-mode to listen for incoming messages and events via WebSocket.

    Note: @slack/events-api and @slack/interactive-messages have reached End of Life (EOL). It is recommended to migrate to Bolt for JavaScript for these functionalities.

  3. Use the Slack Real Time Messaging (RTM) API

    main

    The @slack/rtm-api package provides a client for interacting with Slack's legacy Real Time Messaging API via a persistent Websocket connection.

    Important Compatibility Note: The RTM API is not available for modern granular-permissions apps. If you are building a new app, Slack recommends using Bolt for JavaScript. If you have an existing RTM app, do not update its scopes, as converting it to a granular-permissions app will cause it to stop working with the RTM API.

  4. Available Slack API packages

    main

    The SDK provides dedicated packages for different Slack APIs. Choose the package that matches your integration needs:

    Slack APIUse CaseNPM Package
    Web APISend data to or query data from Slack using over 270 methods.@slack/web-api
    OAuthSet up V2 OAuth (for Slack apps) or V1 OAuth (for classic apps) authentication flows.@slack/oauth
    Incoming WebhooksSend notifications to a single channel selected by the user during installation.@slack/webhook
    Socket ModeListen for incoming messages and a limited set of events using WebSockets.@slack/socket-mode
  5. Use ChatStreamer to stream messages

    main

    The ChatStreamer class allows you to create a continuous chat stream, enabling you to append content incrementally to a single message. This is useful for long-running or generative text responses.

    To use it, you can either instantiate it directly via new ChatStreamer(...) or use the convenience method client.chatStream(...) provided by the WebClient.

    Workflow:

    1. Initialize: Create the streamer with channel, thread_ts, recipient_team_id, and recipient_user_id.
    2. Append: Call .append() multiple times to add content (e.g., markdown_text).
    3. Stop: Call .stop() to finalize the message and end the stream.
    const streamer = client.chatStream({
      channel: "C0123456789",
      thread_ts: "1700000001.123456",
      recipient_team_id: "T0123456789",
      recipient_user_id: "U0123456789",
    });
    
    await streamer.append({
      markdown_text: "**hello wo",
    });
    
    await streamer.append({
      markdown_text: "rld!**",
    });
    
    await streamer.stop();
  6. Migrate @slack/web-api from v7 to v8

    main

    The v8 release of @slack/web-api replaces axios with the native Fetch API (globalThis.fetch).

    Key Requirements & Changes:

    • Node.js Version: Requires Node.js 20 or later.
    • Dependencies: Drops axios, form-data, is-electron, and is-stream.
    • Transport Configuration: Replaces agent, tls, requestInterceptor, and adapter options with a single fetch option.
    • Error Handling: Errors are now proper Error subclasses (e.g., WebAPIPlatformError, WebAPIRequestError) instead of interfaces. It is recommended to use instanceof for error checking.
    npm i @slack/web-api
  7. Migrate @slack/socket-mode from v1 to v2

    main

    When upgrading from @slack/socket-mode v1.x to v2.x, you must address breaking changes regarding lifecycle events and client properties.

    Replace Lifecycle Events

    • Instead of authenticated: Use connected to detect when the client has established a connection and received a hello message (events will start flowing after this), or use connecting if you need to be notified before the connection is established.
    • Instead of unable_to_socket_mode_start: Use the error event, or reconnecting if you have client reconnections enabled.

    Replace Client Properties

    • The properties connected and authenticated have been removed.
    • Use isActive(): Call this method on the client to determine if the WebSocket connection powering the client is healthy.
  8. Understand the Node SDK support schedule

    main

    The Node Slack SDK follows a support schedule tied to Node.js LTS (Long Term Support) version maintenance cycles. You are supported at least until the maintenance for your current Node.js LTS version ends, which typically occurs every 12 months.

    To ensure a smooth transition between major versions, the maintainers follow these commitments:

    1. Announcement: If support for a major version is ending, it will be announced at least 90 days before the next Node.js LTS version maintenance ends.
    2. New Version Availability: A new major version will be released within 30 days of the end-of-support announcement to allow for migration.
    3. Soft-Maintenance Period: After a major version's maintenance ends, there is a 30-day 'soft-maintenance' period dedicated to critical fixes and low-impact contributions. After this period, the version reaches end-of-life (EOL).

    Migration Planning Tip: If a newer major version of the Node SDK is available, plan your migration early. Some updates may only be released for the newest major versions.