Skip to content

kellan/slack-bot-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Installation

npm install slackbots

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,
  • open - websocket connection is open and ready to communicate,
  • close - websocket connection is closed.

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 | group | user by ID,
  • postTo(name, message [, params, callback]) - posts a message to channel | group | user by name,
  • postMessageToChannel(name, message [, params, callback]) - posts a message to channel 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.

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:'
    };
    
    // define channel, where bot exist. You can adjust it there https://my.slack.com/services 
    bot.postMessageToChannel('general', 'meow!', params);
    
    // define existing username instead of 'user_name'
    bot.postMessageToUser('user_name', 'meow!', params); 
    
    // define private group instead of 'private_group', where bot exist
    bot.postMessageToGroup('private_group', 'meow!', params); 
});

PROFIT!

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

###Response Handler The simplest way for handling response is callback function, which is specified as a last argument:

bot.postMessageToUser('user1', 'hi', function(data) {/* ... */});
bot.postMessageToUser('user1', 'hi', params, function(data) {/* ... */});

But also you can use promises.

Error:

bot.postMessageToUser('user1', 'hi').fail(function(data) {
    //data = { ok: false, error: 'user_not_found' }
})

Success:

bot.postMessageToUser('user', 'hi').then(function(data) {
    // ...
})

Error and Success:

bot.postMessageToUser('user', 'hi').always(function(data) {
    // ...
})

About

Simple way to control your Slack Bot

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%