Top Related Projects
A framework to build Slack apps using JavaScript
A Ruby and command-line client for the Slack Web, Real Time Messaging and Event APIs.
Slack Developer Kit for Python
A framework to build Slack apps using Python
Quick Overview
The slackapi/node-slack-sdk is the official Slack SDK for Node.js. It provides a comprehensive set of tools and APIs for building Slack apps and integrations, allowing developers to interact with Slack's various features and functionalities programmatically.
Pros
- Comprehensive and official: Covers all aspects of Slack's API, ensuring reliability and up-to-date features
- Well-documented: Extensive documentation and examples for easy implementation
- Modular design: Allows developers to use only the parts of the SDK they need
- TypeScript support: Provides type definitions for improved development experience
Cons
- Learning curve: Due to its comprehensive nature, it may take time to fully understand and utilize all features
- Frequent updates: Keeping up with Slack's evolving platform may require regular updates to the SDK
- Performance overhead: For simple use cases, the full SDK might be overkill and introduce unnecessary overhead
Code Examples
- Sending a message to a channel:
const { WebClient } = require('@slack/web-api');
const web = new WebClient(process.env.SLACK_TOKEN);
(async () => {
try {
const result = await web.chat.postMessage({
channel: 'C1234567890',
text: 'Hello, world!'
});
console.log('Message sent: ', result.ts);
} catch (error) {
console.error(error);
}
})();
- Listening for events using the Events API:
const { createEventAdapter } = require('@slack/events-api');
const slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET);
slackEvents.on('message', (event) => {
console.log(`Received a message event: user ${event.user} in channel ${event.channel} says ${event.text}`);
});
(async () => {
const server = await slackEvents.start(3000);
console.log('Listening for events on port 3000');
})();
- Using the RTM API to listen for real-time events:
const { RTMClient } = require('@slack/rtm-api');
const rtm = new RTMClient(process.env.SLACK_TOKEN);
rtm.on('message', async (event) => {
console.log(`Message from ${event.user}: ${event.text}`);
});
(async () => {
await rtm.start();
})();
Getting Started
-
Install the SDK:
npm install @slack/web-api @slack/events-api @slack/rtm-api
-
Set up environment variables:
export SLACK_TOKEN=xoxb-your-bot-token export SLACK_SIGNING_SECRET=your-signing-secret
-
Create a new file (e.g.,
app.js
) and import the necessary modules:const { WebClient } = require('@slack/web-api'); const { createEventAdapter } = require('@slack/events-api'); const web = new WebClient(process.env.SLACK_TOKEN); const slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET); // Your app logic here
-
Run your app:
node app.js
Competitor Comparisons
A framework to build Slack apps using JavaScript
Pros of Bolt-js
- Simplified API with a more intuitive and concise syntax
- Built-in support for common Slack app features like modals and shortcuts
- Easier setup and configuration for Slack apps
Cons of Bolt-js
- Less flexibility for complex custom implementations
- Limited to Slack-specific functionality, whereas node-slack-sdk is more versatile
- Steeper learning curve for developers already familiar with node-slack-sdk
Code Comparison
node-slack-sdk:
const { WebClient } = require('@slack/web-api');
const web = new WebClient(token);
(async () => {
const result = await web.chat.postMessage({
channel: 'C1234567890',
text: 'Hello, world!'
});
})();
Bolt-js:
const { App } = require('@slack/bolt');
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
app.message('hello', async ({ message, say }) => {
await say(`Hey there <@${message.user}>!`);
});
The node-slack-sdk provides a lower-level API for interacting with Slack, offering more control but requiring more code. Bolt-js, on the other hand, provides a higher-level abstraction, making it easier to create Slack apps with less code. Bolt-js is ideal for quickly building Slack apps with common features, while node-slack-sdk is better suited for more complex or custom implementations.
A Ruby and command-line client for the Slack Web, Real Time Messaging and Event APIs.
Pros of slack-ruby-client
- Native Ruby implementation, ideal for Ruby-based projects
- Comprehensive support for Slack's Web API, RTM API, and Events API
- Active community and regular updates
Cons of slack-ruby-client
- Limited to Ruby ecosystem, less versatile for multi-language projects
- May have a steeper learning curve for developers not familiar with Ruby
Code Comparison
slack-ruby-client:
require 'slack-ruby-client'
Slack.configure do |config|
config.token = ENV['SLACK_API_TOKEN']
end
client = Slack::Web::Client.new
client.chat_postMessage(channel: '#general', text: 'Hello World!')
node-slack-sdk:
const { WebClient } = require('@slack/web-api');
const web = new WebClient(process.env.SLACK_TOKEN);
(async () => {
await web.chat.postMessage({ channel: '#general', text: 'Hello World!' });
})();
Both libraries provide similar functionality for interacting with Slack's API, but with syntax and patterns specific to their respective languages. The node-slack-sdk offers a more JavaScript-friendly approach with Promises and async/await support, while slack-ruby-client follows Ruby conventions and idioms.
Slack Developer Kit for Python
Pros of python-slack-sdk
- More comprehensive documentation and examples
- Better support for asynchronous programming with asyncio
- Stronger type hinting and static type checking
Cons of python-slack-sdk
- Slightly steeper learning curve for beginners
- Less extensive ecosystem of third-party libraries and tools
Code Comparison
python-slack-sdk:
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
client = WebClient(token="YOUR_TOKEN")
try:
response = client.chat_postMessage(channel="#random", text="Hello world!")
except SlackApiError as e:
print(f"Error: {e}")
node-slack-sdk:
const { WebClient } = require('@slack/web-api');
const client = new WebClient('YOUR_TOKEN');
(async () => {
try {
const result = await client.chat.postMessage({ channel: '#random', text: 'Hello world!' });
} catch (error) {
console.error(error);
}
})();
Both SDKs provide similar functionality for interacting with the Slack API, but python-slack-sdk offers more robust typing and documentation. The node-slack-sdk uses Promises for asynchronous operations, while python-slack-sdk leverages Python's asyncio for async programming. The Python version also includes built-in error handling with specific exception types, making it easier to handle different API errors.
A framework to build Slack apps using Python
Pros of Bolt-Python
- Simplified API for building Slack apps in Python
- Built-in support for common Slack app features like modals and shortcuts
- Easier to handle asynchronous operations with Python's async/await syntax
Cons of Bolt-Python
- Limited to Python ecosystem, while Node-Slack-SDK supports JavaScript/TypeScript
- May have a steeper learning curve for developers not familiar with Python
Code Comparison
Bolt-Python:
from slack_bolt import App
app = App(token="YOUR_BOT_TOKEN")
@app.message("hello")
def message_hello(message, say):
say(f"Hey there <@{message['user']}>!")
app.start(port=3000)
Node-Slack-SDK:
const { WebClient } = require('@slack/web-api');
const web = new WebClient('YOUR_BOT_TOKEN');
(async () => {
await web.chat.postMessage({ channel: 'C1234', text: 'Hello world!' });
})();
The Bolt-Python example shows a more declarative approach to handling Slack events, while the Node-Slack-SDK example demonstrates direct API calls. Bolt-Python's structure may be more intuitive for building complex Slack apps, but Node-Slack-SDK offers more flexibility for custom implementations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Node Slack SDK
Getting Started
Visit the documentation site for all the lovely details.
This SDK is a collection of single-purpose packages. The packages are aimed at making building Slack apps easy, performant, secure, and scalable. They can help with just about anything in the Slack platform, from dropping notifications in channels to fully interactive bots.
The Slack platform offers several APIs to build apps. Each Slack API delivers part of the capabilities from the platform, so that you can pick just those that fit for your needs. This SDK offers a corresponding package for each of Slack's APIs. They are small and powerful when used independently, and work seamlessly when used together, too.
Just starting out? The Getting Started tutorial will walk you through building your first Slack app using Node.js.
Slack API | What its for | NPM Package |
---|---|---|
Web API | Send data to or query data from Slack using any of over 220 methods. | @slack/web-api |
OAuth | Setup the authentication flow using V2 OAuth for Slack apps as well as V1 OAuth for classic Slack apps. | @slack/oauth |
Incoming Webhooks | Send notifications to a single channel which the user picks on installation. | @slack/webhook |
Socket Mode | Listen for incoming messages and a limited set of events happening in Slack, using WebSocket. | @slack/socket-mode |
Not sure about which APIs are right for your app? Read our blog post that explains the options. If you're still not sure, reach out for help and our community can guide you.
Deprecation Notice
@slack/events-api
and @slack/interactive-messages
officially reached EOL on May 31st, 2021. Development has fully stopped for these packages and all remaining open issues and pull requests have been closed.
At this time, we recommend migrating to Bolt for JavaScript, a framework that offers all of the functionality available in those packages (and more). To help with that process, we've provided some migration samples for those looking to convert their existing apps.
Installation
Use your favorite package manager to install any of the packages and save to your package.json
:
$ npm install @slack/web-api @slack/socket-mode
# Or, if you prefer yarn
$ yarn add @slack/web-api @slack/socket-mode
Usage
The following examples summarize the most common ways to use this package. There's also a Getting Started tutorial that's perfect for just starting out, and each package's documentation, linked in the table above.
Posting a message with Web API
Your app will interact with the Web API through the WebClient
object, which is an export from @slack/web-api
. You
typically instantiate a client with a token you received from Slack. The example below shows how to post a message into
a channel, DM, MPDM, or group. The WebClient
object makes it simple to call any of the over 130 Web API
methods.
const { WebClient } = require('@slack/web-api');
// An access token (from your Slack app or custom integration - xoxp, xoxb)
const token = process.env.SLACK_TOKEN;
const web = new WebClient(token);
// This argument can be a channel ID, a DM ID, a MPDM ID, or a group ID
const conversationId = 'C1232456';
(async () => {
// See: https://api.slack.com/methods/chat.postMessage
const res = await web.chat.postMessage({ channel: conversationId, text: 'Hello there' });
// `res` contains information about the posted message
console.log('Message sent: ', res.ts);
})();
Note: To use the example above, the token is required to have either the bot
, chat:user:write
, or
chat:bot:write
scopes.
Tip: Use the Block Kit Builder for a playground where you can prototype your message's look and feel.
Listening for an event with the Events API
Refer to Bolt for JavaScript document pages.
Responding to interactive messages
Refer to Bolt for JavaScript document pages.
Using Socket Mode
Refer to the module document page and Bolt for JavaScript document page.
Requirements
This package supports Node v14 and higher. It's highly recommended to use the latest LTS version of node, and the documentation is written using syntax and features from that version.
Getting Help
If you get stuck, we're here to help. The following are the best ways to get assistance working through your issue:
- Issue Tracker for questions, feature requests, bug reports and general discussion related to these packages. Try searching before you create a new issue.
- Email us in Slack developer support:
developers@slack.com
Top Related Projects
A framework to build Slack apps using JavaScript
A Ruby and command-line client for the Slack Web, Real Time Messaging and Event APIs.
Slack Developer Kit for Python
A framework to build Slack apps using Python
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot