/Slackbots

Primary LanguageJavaScriptMIT LicenseMIT

SlackBots.js

license Build Status npm

This is Node.js library for easy operation with Slack API.

It also exposes all opportunities of Slack's Real Time Messaging API.

Events

  • start - event fired, when Real Time Messaging API is started (via websocket),
  • message - event fired, when something happens in Slack. Description of all events here.

Methods

  • getChannels (return: promise) - returns a list of all channels in the team,
  • getUsers (return: promise) - returns a list of all users in the team,
  • getUser (return: promise) - gets user by name,
  • getChannel (return: promise) - gets channel by name,
  • getChatId (return: promise) - it returns or opens and returns a direct message channel ID,
  • postMessage - posts a message to channel by ID,
  • postMessageToChannel - posts a message to channel by name,
  • postMessageToUser - posts a direct message by user name.

Usage

var SlackBot = require('slackbots');

// create a bot
var bot = new SlackBot({
    token: 'xoxb-012345678-ABC1DFG2HIJ3', // Add a bot https://my.slack.com/services/new/bot and put the token 
    name: 'My Bot'
});

bot.on('start', function() {
    // more information about additional params https://api.slack.com/methods/chat.postMessage
    var params = {
        icon_emoji: ':cat:'
    };
    
    bot.postMessageToChannel('general', 'meow!', params);
    bot.postMessageToUser('username', 'meow!', params);
});

PROFIT!

/**
 * @param {object} data
 */
bot.on('message', function(data) {
    // all ingoing events https://api.slack.com/rtm
    console.log(data);
});