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

slock

v0.0.7

Published

promise-based and functional reactive bindings to Slack's API

Downloads

18

Readme

slock logo

Slock is not affiliated with or endorsed by Slack in any way

Info | Badges -----|------- Version | github release npm version License | npm license Popularity | npm downloads Testing | build status test coverage Quality | dependency status dev dependency status Help/Community | Join the chat at https://gitter.im/declandewet/slock

Slock

Note this module is in extremely early development. There are no tests yet, documentation is missing and versioning might be a little different. So don't use this in production at the moment - but feel free to play with it!

Slock is a Node.js module for working with the various Slack APIs, built atop functional reactive programming principles.

The benefit of using functional reactive programming for such a library means that events can be filtered, mapped, reduceed, and so on - just like arrays!

Functional Reactive code can also be very expressive while still remaining terse, and this makes maintainability an absolute dream.

Installation

Slock can be installed by executing the following command in a terminal:

npm install slock --save

Usage

Slock tries to be as inexplicit as possible, and as such doesn't require much of a learning curve (even if functional reactive programming, observables and EventStreams are not quite your strong point).

Every event and method correlates directly to the events and methods listed on the Slack API documentation site, so if those docs expect a payload, just call the relevant method with the expected payload as a regular JS object.

Let's get started building a realtime bot that echoes out what users say.

First thing's first - we have to connect to the Slack API by passing in our bot's API token.

// import the module
var Slack = require('slock');

// give it our token
var slack = new Slack('insert api token here');

That was pretty simple and straightforward - but now we need to connect to the RTM API. Simple enough:

slack.connect();

Boom. How's about we post a message to Slack letting users know we've connected?

slack.connect()
  .onValue(function(response) {
    slack.chat.postMessage({
      text: "Hello! I'm now active on your team.",
      channel: "#general"
    });
  });

Now, for the logic that echoes out what users say...

slack.events.message
  .filter(function(message) {
    return ~message.text.indexOf('bot echo');
  })
  .onValue(function(message) {
    var echo = message.text.split('bot echo ')[1];
    slack.chat.postMessage({
      text: echo,
      channel: message.channel
    })
  });

Putting it all together, we have something that looks like this:

// import the module
var Slack = require('slock');

// give it our token
var slack = new Slack('insert api token here');

slack.events.message
  .filter(function(message) {
    return ~message.text.indexOf('bot echo');
  })
  .onValue(function(message) {
    var echo = message.text.split('bot echo ')[1];
    slack.chat.postMessage({
      text: echo,
      channel: message.channel
    })
  });

slack.connect()
  .onValue(function(response) {
    slack.chat.postMessage({
      text: "Hello! I'm now active on your team.",
      channel: "#general"
    });
  });

As you can see, the methods are fairly straightforward.

Any time you want to get access to a Slack RTM event, just call slack.events.<event type>. Here's a list of all of them.

Similarly, if you want to use a particular Slack Web API method, just call slack.<name of method group>.<name of method>, so if you wanted to post a message, it'd be slack.chat.postMessage. If you want to add a reaction, call slack.reactions.add. Here's a list of all the methods you can call.