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

simple-slack-bot

v0.0.5

Published

Awesome Simple Node.js Slack Bot library

Downloads

12

Readme

#Simple Slack Bot license SlugBay Badge

NPM NPM

This is Node.js library for easy operation with Slack's Real Time Messaging API.

Installation (module)

npm install simple-slack-bot

Events

  • slack.login - event fired, when Real Time Messaging API is started (via websocket).
  • slack.logout - event fired, when bot logout from Slack.
  • ws.message - event fired, when something happens in Slack. Description of all events here.
  • ws.open - websocket connection is open and ready to communicate.
  • ws.close - websocket connection is closed.
  • ws.error - an error occurred while connecting to Slack.
  • ws.ping - websocket connection is pinged.
  • ws.pong - websocket connection is ponged.

Methods

  • login() - Start a connection to Real Time Messaging API,
  • logout() - Logout from Slack and close websocket connection,
  • setCached(enabled) - Enable/Disable caching (default: true) to keep channels/groups/users updated,
  • getChannels(callback) - Returns a list of all channels in the team,
  • getGroups(callback) - Returns a list of all groups in the team,
  • getUsers(callback) - Returns a list of all users in the team,
  • getChannelByName(name, callback) - Gets channel by name,
  • getGroupByName(name, callback) - Gets group by name,
  • getUserByName(name, callback) - Gets user by name,
  • getChannelId(name, callback) - Gets channel ID by name,
  • getGroupId(name, callback) - Gets group ID by name,
  • getUserId(name, callback) - Gets user ID by name,
  • getChatId(name, callback) - Returns or opens and returns a direct message channel id,
  • postMessage(id, text, params, [callback]) (return: promise) - posts a message to channel | group | user by id,
  • updateMessage(channelId, timestamp, text, params, [callback]) - updates a message in a channel,
  • postTo(name, message [, params, callback]) - posts a message to channel | group | user by name,
  • postMessageToUser(name, message [, params, callback]) - posts a direct message by user name,
  • postMessageToGroup(name, message [, params, callback]) - posts a message to private group by name,
  • postMessageToChannel(name, message [, params, callback]) - posts a message to channel by name.

Usage

var SlackBot = require('simple-slack-bot');

// create a bot
var bot = new SlackBot(
    // Slack configuration
    {
        cached: true, // Enable/Disable caching (default: true)
        token: 'xoxb-012345678-ABC1DFG2HIJ3', // Add a bot https://my.slack.com/services/new/bot and put the token 
        name: 'My Bot'
    },
    // Websocket connection parameters
    {
        protocols: {
            ...
        },
        options: {
            ...
        }
    }
});

bot.on('slack.login', function() {
    
    // more information about additional params https://api.slack.com/methods/chat.postMessage
    var params = {
        icon_emoji: ':cat:'
    };
    
    // define channel, where bot exist. You can adjust it there https://my.slack.com/services 
    bot.postMessageToChannel('general', 'meow!', params, function (result) {

    });
    
    // define existing username instead of 'user_name'
    bot.postMessageToUser('user_name', 'meow!', params, function (result) {

    }); 
    
    // If you add a 'slackbot' property, 
    // you will post to another user's slackbot channel instead of a direct message
    bot.postMessageToUser('user_name', 'meow!', { 'slackbot': true, icon_emoji: ':cat:' }, function (result) {

    }); 
    
    // define private group instead of 'private_group', where bot exist
    bot.postMessageToGroup('private_group', 'meow!', params, function (result) {

    });

    // all ingoing events https://api.slack.com/rtm
    bot.on('ws.message', function(event) {
        
        console.log('message', event.data);
        console.log('flags', event.flags);
    });
});

bot.login()

Credits

Screenshot