npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@felixrieseberg/slack-client

v3.14.11

Published

A library for creating a Slack client

Downloads

14

Readme

Node Library for the Slack APIs

Build Status codecov npm (scoped)

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.

Installation

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

Some Examples

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 = require('@slack/client').IncomingWebhook;

var url = process.env.SLACK_WEBHOOK_URL || '';

var webhook = new IncomingWebhook(url);

webhook.send('Hello there', function(err, header, statusCode, body) {
  if (err) {
    console.log('Error:', err);
  } else {
    console.log('Received', statusCode, 'from Slack');
  }
});

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 = require('@slack/client').WebClient;

var token = process.env.SLACK_API_TOKEN || '';

var web = new WebClient(token);
web.chat.postMessage('C1232456', 'Hello there', function(err, res) {
  if (err) {
    console.log('Error:', err);
  } else {
    console.log('Message sent: ', res);
  }
});

Posting a message with the Real-Time Messaging API

Starting a bot up requires a bot token (bot tokens start with xoxb-), 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 = require('@slack/client').RtmClient;
var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;

var bot_token = process.env.SLACK_BOT_TOKEN || '';

var rtm = new RtmClient(bot_token);

let channel;

// The client will emit an RTM.AUTHENTICATED event on successful connection, with the `rtm.start` payload
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
  for (const c of rtmStartData.channels) {
	  if (c.is_member && c.name ==='general') { channel = c.id }
  }
  console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
});

// you need to wait for the client to fully connect before you can send messages
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function () {
  rtm.sendMessage("Hello!", channel);
});

rtm.start();