Code Monkey home page Code Monkey logo

slack's Introduction

slack

npm codeship coverage 93.85%

A Slack Web API client ๐ŸŒฑ๐Ÿ™Œ๐Ÿ’•

  • Written in modern JavaScript; tested for Node and the browser
  • Complete support for the Slack Web API
  • Perfect symmetry: JS method signatures match Web API docs
  • Choose your async adventure: all methods accept either a Node style errback or return a Promise
  • Opt into an OO style class instance that applies token to all methods
  • Well tested, CI, and Apache2 licensed
  • Only one dependency: tiny-json-http
  • Tiny: 7kb browserified/minified

Install ๐ŸŒŸ๐Ÿ“ฆ

npm i slack

Usage โœจ๐Ÿš€

slack mirrors the published API docs exactly because its generated from those docs! The default interface are stateless functions and has remain unchanged since 1.0.0 and that will continue to be the case.

var slack = require('slack')

// logs {args:{hello:'world'}}
slack.api.test({hello:'world'}, console.log)

// :new: opt into promises
slack.api.test({nice:1}).then(console.log).catch(console.log)

Due to popular demand an OO style is supported. For an instance of Slack all methods come prebound with the token parameter applied.

const token = process.env.SLACK_BOT_TOKEN
const Slack = require('slack')
const bot = new Slack({token})

// logs {args:{hyper:'card'}}
bot.api.test({hyper:'card'}).then(console.log)

Using async/await in Node 8.x:

let token = process.env.SLACK_BOT_TOKEN
let Slack = require('slack')
let bot = new Slack({token})

;(async function main() {
  // logs {args:{hyper:'card'}}
  var result = await bot.api.test({hyper:'card'})
  console.log(result)
})()

Choose whichever style works best for your project deployment needs and team preference. โ™ฅ๏ธ๐Ÿบ

Error Handling

Some methods (like slack.dialog.open) provide additional context for errors through a response_metadata object. This will be exposed as a messages properties on the errors that are thrown.

slack.dialog.open(options).catch(err => {
  console.log(err.messages)
})

Specialized Electron Support

Electron ships its own HTTP module called electron.net which can have better performance and offers more extensive HTTP proxy handling. You can opt into Electron support by passing useElectronNet:true to the Slack constructor.

import {app, BrowserWindow, net} from 'electron'
import Slack from 'slack'

const slack = new Slack({useElectronNet:true})

You can setup an HTTP authentication proxy logic by passing login to the constructor.

function login(authInfo, callback) {
  callback('username', 'password')
}

const slack = new Slack({useElectronNet:true, login})

Read more about electron.net from the source!

Test Setup ๐Ÿ”’๐Ÿ”‘๐Ÿ‘ˆ

Clone this repo and create a file called .env in the root with the following:

SLACK_BOT_TOKEN=xxxx
SLACK_CLIENT_ID=xxxx
SLACK_CLIENT_SECRET=xxxx

You can get a SLACK_TOKEN for testing here. You need to register an app for a SLACK_CLIENT_ID and SLACK_CLIENT_SECRET. The tests require the app to have the channels:history scope. You can read about bot tokens here.

Testing ๐Ÿ’š๐Ÿ’š๐Ÿ’š

๐Ÿ‘‰ In Node:

npm test

๐Ÿ‘‰ Or the browser:

npm run btest

Slack Web API ๐ŸŽ‰๐Ÿ๐Ÿšฉ

The entire Slack Web API is supported. All method signatures accept a params object and either a Node style callback (an errback) or, if absent, it will return a Promise. Required params are documented inline below.

Contributing

The code for the client is generated by scraping the Slack Web API documentation. Regenerate from the latest Slack documentation by running ๐Ÿƒ:

npm run generate

Portions of this README are generated as well; to make edits, update readme.tmpl and run the same command โ˜๏ธโ˜”โ˜€๏ธ๐ŸŒป.

slack's People

Contributors

adhambadr avatar brianleroux avatar delasteve avatar fearphage avatar html5cat avatar jimkang avatar llzes avatar matthewmueller avatar mbrevoort avatar natew avatar nvolungis avatar omariosouto avatar ryanblock avatar santoshsahoo avatar shockey avatar tkmcc avatar

Watchers

 avatar  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.