Git Product home page Git Product logo

slack-api's Introduction

slack-api

A javascript wrapper for the Slack API.

Installation

Installation is easy, like any package on npm, you can add it to your project by typing the following in the terminal in your project directory:

npm install --save slack-api

Usage

slack-api uses the same structure as the Slack Web API, and follows Node convention for calling methods.

'use strict';

var Slack = require('slack-api');

Slack.api.test({}, function (error, data) {
  console.log(data);
});

API

slack-api shares the same methods as the Slack Web API, but with a few additions.

You can find the full Slack Web API documentation on their site.

If slack-api is missing a method let us know, or better yet, update it.

oauth.getUrl(options, [callback=noop])

This method generates the url used for step 1 of the Slack OAuth flow.

Arguments

  1. options (Object) - options hash should have the following properties:
client_id    - issued when you created your application (required)
redirect_uri - URL to redirect back to (see below) (optional)
scope        - permissions to request (see below) (optional)
state        - unique string to be passed back upon completion
team         - Slack team ID to restrict to (optional)
  1. [callback=noop] (Function) - Function to be called upon completion. If none is provided, this method will simply execute silently.

Returns

This method invokes the callback argument function in the standard node.js style (callback(error, data)), where data is the url (String) that the user should be redirected to to start the OAuth process for slack.

oauth.access(options, [state], [callback=noop])

This method allows you to exchange a temporary OAuth code for an API access token. This is used as part of the OAuth authentication flow.

This method will optionally perform the state check for you, should you provide it.

Arguments

  1. options (Object) - options hash should have the following properties:
client_id     - issued when you created your application (required)
client_secret	- issued when you created your application (required)
code	        - the code param returned via the OAuth callback (required)
redirect_uri	- this must match the originally submitted URI (if one was sent)
  1. [state] (String) - unique string passed to the original authorization call (optional)
  2. [callback=noop] (Function) - Function to be called upon completion. If none is provided, this method will simply execute silently.

Returns

This method invokes the callback argument function in the standard node.js style (callback(error, data)).

promisify()

This method returns a promisified version of the Slack API library.

'use strict';

var Slack = require('slack-api').promisify();

Slack.api.test({}).then(function (data) {
  console.log(data);
}).catch(Slack.errors.SlackError, function (error) {
  console.log('Slack did not like what you did: ' + error.message);
}).catch(Slack.errors.CommunicationError, function (error) {
  console.error('Error communicating with Slack. ' + error.message);
}).catch(Slack.errors.SlackServiceError, function (error) {
  console.error('Error communicating with Slack. ' + error.message);
  //To get error details
  console.error(error.errorDetails);
});

Errors

The slack-api comes with some custom errors, and their constructors are included under the errors property.

  • CommunicationError - This error is thrown if the https request fails e.g. because of a network problem.
  • SlackError - If Slack returns an error, this error will be passed to the callback function as the first argument for error-handling.
  • SlackServiceError - If Slack returns a non-200 status code, this error will be passed to the callback function as the first argument for error-handling. The error details are provided as a property on the error called errorDetails. SlackServiceError is different from SlackError because the Slack API returns a 200 status code for the kinds of errors identified by SlackError.

Development and testing

If you want to contribute, simply clone this repo, and run

npm install

Tests are written for mocha, and can be run with

npm test

slack-api's People

Contributors

ustice avatar nraomobiquityinc avatar metaodi avatar athakral avatar benaychh avatar ceejbot avatar filipekc avatar fmgoncalves avatar r4j4h avatar ainoya avatar

Watchers

L3DLP avatar James Cloos 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.