forked from beginner-corp/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.tmpl
145 lines (102 loc) · 4.72 KB
/
readme.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<h1 align="center">
<img width="333px" src=https://s3-us-west-1.amazonaws.com/bugbot/slack-js.svg alt=slack title=slack width=178>
</h1>
<p align="center" style="font-size: 1.2rem;">
<a href=https://www.npmjs.com/package/slack><img alt=npm src=https://badge.fury.io/js/slack.svg></a>
<a href=https://codeship.com/projects/121411><img alt=codeship src=https://img.shields.io/codeship/3fd641e0-81f4-0133-c733-22940a7a47c6.svg></a>
<a href=><img alt="coverage 93.85%" src=https://img.shields.io/badge/coverage-93.85%25-brightgreen.svg></a>
</p>
### A [Slack Web API](https://api.slack.com/methods) client :seedling::raised_hands::two_hearts:
- Written in modern JavaScript; tested for Node and the browser
- Complete support for the [Slack Web API](https://api.slack.com/methods)
- 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 :star2::package:
```
npm i slack
```
# Usage :sparkles::rocket:
`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.
```javascript
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.
```javascript
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:
```javascript
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. :hearts::beer:
### Error Handling
Some methods (like [`slack.dialog.open`](https://api.slack.com/methods/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.
```javascript
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.
```javascript
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.
```javascript
function login(authInfo, callback) {
callback('username', 'password')
}
const slack = new Slack({useElectronNet:true, login})
```
[Read more about `electron.net` from the source!](https://github.com/electron/electron/blob/master/docs/api/net.md)
### Test Setup :lock::key::point_left:
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](https://api.slack.com/web). 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](https://api.slack.com/docs/token-types#bot).
## Testing :green_heart::green_heart::green_heart:
:point_right: In Node:
```
npm test
```
:point_right: Or the browser:
```
npm run btest
```
# Slack Web API :tada::honeybee::triangular_flag_on_post:
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.
{{#methods}}
- {{{.}}}
{{/methods}}
# Contributing
The code for the client is generated by scraping the [Slack Web API documentation](https://api.slack.com/methods).
Regenerate from the latest Slack documentation by running :runner::
```
npm run generate
```
Portions of this README are generated as well; to make edits, update `readme.tmpl`
and run the same command :cloud::umbrella::sunny::sunflower:.