node package manager
Stop wasting time. Easily manage code sharing in your team. Create a free org »

slack

slack

npm codeship coverage 96.67%

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 two dependencies: util.promisify and 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 now supported. For an instance of Slack all methods come prebound with the token parameter applied.

const token = process.env.SLACKBOT_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.SLACKBOT_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. It is definitely worth examining what style is more concise, expressive and deterministic. It is also worth noticing how these properties can change between runtime versions. ♥️🍺

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)
})

Test Setup 🔒🔑👈

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

SLACK_TOKEN=xxxx
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.

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 ☁️☔️☀️🌻.