Node Library for the Slack APIs
Read the full documentation for all the lovely details.
This module is a wrapper around the Slack RTM and Web APIs.
It will help you build on the Slack platform, from dropping notifications in channels to developing fully interactive bots. It provides the low level functionality you need to build reliable apps and projects on top of Slack's APIs. It:
- handles reconnection logic and request retries
- provides reasonable defaults for events and logging
- defines a basic model layer and data-store for caching Slack RTM API responses
This module does not attempt to provide application level support, e.g. regex matching and filtering of the conversation stream.
Most Slack apps are interested in posting messages into Slack channels, and generally working with our Web API. Read on
to learn how to use
node-slack-sdk to accomplish these tasks. Bots, on the other hand, are a bit more complex,
so we have them covered in Building Bots.
Once you have a working Node.js project, you can install the Slack Developer Kit as a dependency via npm:
$ npm install @slack/client --save
All of these examples assume that you have set up a Slack app or custom integration, and understand the basic mechanics of working with the Slack Platform.
Posting a message with Incoming Webhooks
Incoming webhooks are an easy way to get notifications posted into Slack with a minimum of setup. You'll need to either have a custom incoming webhook set up, or an app with an incoming webhook added to it.
var IncomingWebhook = IncomingWebhook;var url = processenvSLACK_WEBHOOK_URL || '';var webhook = url;webhook;
Posting a message with Web API
You'll need a Web API token to call any of the Slack Web API methods. For custom integrations, you'll get this from the token generator, and for apps it will come as the final part of the OAuth dance.
Your app will interact with the Web API through the
WebClient object, which requires an access token to operate.
var WebClient = WebClient;var token = processenvSLACK_API_TOKEN || '';var web = token;webchat;
Posting a message with the Real-Time Messaging API
Starting a bot up requires a bot token (bot tokens start with
which can be had either creating a custom bot or by creating an app with a
bot user, at the end of the OAuth dance. If you aren't sure path is right for you,
have a look at the Bot Users documentation.
var RtmClient = RtmClient;var CLIENT_EVENTS = CLIENT_EVENTS;var bot_token = processenvSLACK_BOT_TOKEN || '';var rtm = bot_token;let channel;// The client will emit an RTM.AUTHENTICATED event on successful connection, with the `rtm.start` payloadrtm;// you need to wait for the client to fully connect before you can send messagesrtm;rtmstart;