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

slack-bot

v1.1.5

Published

Simplifies sending bot messages to slack.

Downloads

105

Readme

slack-bot

Simplifies sending bot messages to slack. Slack-bot utilizes incoming webhooks in Slack to send messages. You can add the incoming webhooks integration and get your unique webhook url at: https://YOUR_TEAMS_SLACK_URL/services/new/incoming-webhook.

Usage

npm install slack-bot (--save)

var slackBot = require('slack-bot')(YOUR_WEBHOOK_URL);

Functionality

Sending a message

To send a message, call the .send function:

slackBot.send(messageObject, callback);

The callback is optional and is passed three arguments:

  • An error object, if applicable
  • The response object
  • The response body

A simple callback example:

slackBot.send(messageObject, function(err, res, body) {
  if (err) return next(err);
  console.log(body);
});

The simplest message object:

slackBot.send({
  text: 'This message will appear in Slack!'
}, callback);

SlackBot defaults to the #general channel and to the username "Slack Bot" if those properties aren't passed in as in the example above, but can easily be set to whatever you like:

slackBot.send({
  text: 'OMG look at this thing',
  channel: '#hackers', // Can also be '@someone' for a direct message
  username: 'Alert Bot'
}, callback);

The other most commonly passed in property is to provide either an icon_emoji or an icon_url to replace the avatar of the bot sending the message:

slackBot.send({
  text: 'Put… the bunny… back… in the box.',
  channel: '#onetruegod',
  username: 'Nic Cage',
    
  icon_emoji: ':niccage:' // You can upload custom emojis in your team settings
  // OR
  icon_url: 'http://i.imgur.com/VVoeZ.gif'
}, callback);

The message object can also take additonal parameters. An overview can be found here: https://api.slack.com/incoming-webhooks

For some advanced message formatting information: https://api.slack.com/docs/formatting

How to send rich messages to slack (with good examples of messsages): https://api.slack.com/docs/attachments

Fancy Features

Multiple recipients

Slack-bot can also take an array of channels in the message object in order to send the same message to several people or several channels (or a mix of people and channels) all at once.

slackBot.send({
  text: 'Do not go outside, zombies are out there!',
  channel: ['#general', '@jon', '@charles'],
  username: 'Zombot',
  icon_emoji: ':zombie:'
}, callback);

In this case, the same message will be sent to the general channel, and to jon and charles as direct messages. When sending a message to multiple channels, the callback receives two arguments. The first argument is the error argument. If no errors occurred, the first argument will be null. If one or more errors occurred, the first argument will be an array of channel names where the errors occurred. The second argument is an object whose keys are each channel the message was sent to, and the value is an object with keys err, res, and body containing the values returned from that particular POST. If we were to send the above message to those three channels and no errors occurred, the object that would be returned to the callback would look like this:

{
  '#general' : {
    err: null,
    res: RESPONSE_OBJECT,
    body: 'ok'
  },
  '@jon': {
    err: null,
    res: RESPONSE_OBJECT,
    body: 'ok'
  },
  '@charles': {
    err: null,
    res: RESPONSE_OBJECT,
    body: 'ok'
  }
}

It is important to note that any errors that occur when sending to multiple channels will not stop the sending of the message to the other channels. That way if an error occurs half way through, it won't stop the message from going to the remaining channels.

The reason the error argument passed to the callback is an array of channel names is so you can easily access all the errors that occurred in the returned object by using the values in the array as the keys in the object:

slackBot.send(messageObjectToMultipleChannels, function(err, object) {
  if (err) {
    err.forEach(function(channel) {
      console.log(object[channel].err);
    });
  }
});