Giter Club home page Giter Club logo

slack-bot-api's Introduction

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.
  • error - an error occurred while connecting to Slack

Methods

  • getChannels() (return: promise) - returns a list of all channels in the team,
  • getGroups() (return: promise) - returns a list of all groups in the team,
  • getUsers() (return: promise) - returns a list of all users in the team,
  • getImChannels() (return: promise) - returns a list of bots direct message channels in the team,
  • getChannel(name) (return: promise) - gets channel by name,
  • getGroup(name) (return: promise) - gets group by name,
  • getUser(name) (return: promise) - gets user by name,
  • getUserByEmail(name) (return: promise) - gets user by name,
  • getChannelId(name) (return: promise) - gets channel ID by name,
  • getGroupId(name) (return: promise) - gets group ID by name,
  • getUserId(name) (return: promise) - gets user ID by name,
  • getChatId(name) (return: promise) - it returns or opens and returns a direct message channel ID,
  • postEphemeral(id, user, text, params) (return: promise) - posts an ephemeral message to channel and user by ID,
  • postMessage(id, text, params) (return: promise) - posts a message to channel | group | user by ID,
  • updateMessage(channelId, timestamp, text, params) (return: promise) - updates a message in a channel,
  • postTo(name, message [, params, callback]) (return: promise) - posts a message to channel | group | user by name,
  • postMessageToUser(name, message [, params, callback]) (return: promise) - posts a direct message by user name,
  • postMessageToGroup(name, message [, params, callback]) (return: promise) - posts a message to private group by name,
  • postMessageToChannel(name, message [, params, callback]) (return: promise) - posts a message to channel by name.
  • openIm(userId) (return: promise) - opens a direct message channel with another member in the team

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} 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) {
    // ...
})

slack-bot-api's People

Contributors

mishk0 avatar oscar-b avatar jesuslg123 avatar 199911 avatar kixorz avatar xludx avatar maxart2501 avatar kchung avatar thesisb avatar dotcypress avatar tomorth avatar aethernet avatar kellan avatar v3xx3d avatar

Watchers

j03 k1ng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.