README
SlackBots.js
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
Params
token
(string) the slack tokenname
(string) the name of the botdisconnect
(boolean, defaults:false
) whether to open websocket connection to listen to incoming messages, set totrue
for one time use
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.error
- an error occurred while connecting to Slack
Methods
getChannels()
(return: promise) - returns a list of all channels in the teamgetGroups()
(return: promise) - returns a list of all groups in the teamgetUsers()
(return: promise) - returns a list of all users in the teamgetImChannels()
(return: promise) - returns a list of bots direct message channels in the teamgetChannel(name)
(return: promise) - gets channel by namegetGroup(name)
(return: promise) - gets group by namegetUser(name)
(return: promise) - gets user by namegetUserByEmail(name)
(return: promise) - gets user by namegetChannelId(name)
(return: promise) - gets channel ID by namegetGroupId(name)
(return: promise) - gets group ID by namegetUserId(name)
(return: promise) - gets user ID by namegetChatId(name)
(return: promise) - it returns or opens and returns a direct message channel IDpostEphemeral(id, user, text, params)
(return: promise) - posts an ephemeral message to channel and user by IDpostMessage(id, text, params)
(return: promise) - posts a message to channel | group | user by IDupdateMessage(channelId, timestamp, text, params)
(return: promise) - updates a message in a channelpostTo(name, message [, params, callback])
(return: promise) - posts a message to channel | group | user by namepostMessageToUser(name, message [, params, callback])
(return: promise) - posts a direct message by user namepostMessageToGroup(name, message [, params, callback])
(return: promise) - posts a message to private group by namepostMessageToChannel(name, message [, params, callback])
(return: promise) - posts a message to channel by nameopenIm(userId)
(return: promise) - opens a direct message channel with another member in the teamreply(incomingMessage, message [, params, callback])
(return: promise) - reply a message
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);
// 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:' });
// define private group instead of 'private_group', where bot exist
bot.postMessageToGroup('private_group', 'meow!', params);
});
PROFIT!
/**
* @param {object} message
*/
bot.on('message', function(message) {
// all ingoing events https://api.slack.com/rtm
console.log(message);
});
Reply incoming message:
/**
* @param {object} message
*/
bot.on('message', function(message) {
// all ingoing events https://api.slack.com/rtm
bot.reply(message, 'hi', function(data) {/* ... */});
bot.reply(message, 'hi', params, function(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) {
// ...
})