forked from slackapi/node-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes slackapi#428, slackapi#239, slackapi#305, slackapi#310, slackapi#364, slackapi#366, slackapi#413, slackapi#411, slackapi#423
- Loading branch information
Showing
14 changed files
with
1,066 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,111 +4,153 @@ | |
[![codecov](https://codecov.io/gh/slackapi/node-slack-sdk/branch/master/graph/badge.svg)](https://codecov.io/gh/slackapi/node-slack-sdk) | ||
[![npm (scoped)](https://img.shields.io/npm/v/@slack/client.svg)](https://www.npmjs.com/package/@slack/client) | ||
|
||
Read the [full documentation](https://slackapi.github.io/node-slack-sdk) for all the lovely details. | ||
Visit the [full documentation](https://slackapi.github.io/node-slack-sdk) for all the lovely details. | ||
|
||
This module is a wrapper around the Slack [RTM](https://api.slack.com/rtm) and [Web](https://api.slack.com/web) APIs. | ||
So you want to build a Slack app with Node.js? We've got you covered. {{ site.product_name }} is aimed at making | ||
building Slack apps ridiculously easy. This package will help you build on all aspects of the Slack platform, | ||
from dropping notifications in channels to fully interactive bots. | ||
|
||
It will help you build on the Slack platform, from dropping notifications in channels to developing fully interactive bots. It provides the low level functionality you need to build reliable apps and projects on top of Slack's APIs. | ||
It: | ||
## Requirements | ||
|
||
- handles reconnection logic and request retries | ||
- provides reasonable defaults for events and logging | ||
- defines a basic model layer and data-store for caching Slack RTM API responses | ||
This package supports node starting from major **version 0.12 and later**. It's highly recommended | ||
to use [the latest LTS version of node](https://github.com/nodejs/Release#release-schedule), and the | ||
documentation is written using syntax and features from that version. | ||
|
||
This module does not attempt to provide application level support, _e.g._ regex matching and filtering of the | ||
conversation stream. | ||
## Installation | ||
|
||
Most Slack apps are interested in posting messages into Slack channels, and generally working with our [Web API](https://api.slack.com/web). Read on | ||
to learn how to use `node-slack-sdk` to accomplish these tasks. Bots, on the other hand, are a bit more complex, | ||
so we have them covered in [Building Bots](https://slackapi.github.io/node-slack-sdk/bots). | ||
Use npm to install the package and save it to your `package.json`: | ||
|
||
# Installation | ||
Once you have a working Node.js project, you can install the Slack Developer Kit as a dependency via npm: | ||
|
||
```sh | ||
$ npm install @slack/client --save | ||
```shell | ||
$ npm install @slack/client | ||
``` | ||
|
||
# Some Examples | ||
|
||
All of these examples assume that you have set up a Slack [app](https://api.slack.com/slack-apps) or | ||
[custom integration](https://api.slack.com/custom-integrations), and understand the basic mechanics of working with the | ||
Slack Platform. | ||
## Features | ||
|
||
## Posting a message with Incoming Webhooks | ||
The Slack platform offers several APIs to build apps. Each API delivers part of the capabilities | ||
from the platform, with a range of complexity and functionality, so that you can pick the one that | ||
fits for your app. | ||
|
||
[Incoming webhooks](https://api.slack.com/incoming-webhooks) are an easy way to get notifications posted into Slack with | ||
a minimum of setup. You'll need to either have a custom incoming webhook set up, or an app with an incoming webhook | ||
added to it. | ||
| Slack API | Outgoing | Incoming | NPM Package | Documentation | | ||
|--------------|:--------:|:--------:|---------------------|-------------------| | ||
| Web API | ⬆️ | ⬜️ | `@slack/client` | [Guide](https://slackapi.github.io/node-slack-sdk/web_client) | | ||
| RTM API | ⬆️ | ⬇️ | `@slack/client` | [Guide](https://slackapi.github.io/node-slack-sdk/rtm_client) | | ||
| Incoming Webhooks | ⬆️ | ⬜️ | `@slack/client` | [Guide](https://slackapi.github.io/node-slack-sdk/incoming_webhook) | | ||
| Events API | ⬜️ | ⬇️ | `@slack/events-api` | [README](https://github.com/slackapi/node-slack-events-api) | | ||
| Interactive Messages | ⬜️ | ⬇️ | `@slack/interactive-messages` | [README](https://github.com/slackapi/node-slack-interactive-messages) | | ||
|
||
```js | ||
var IncomingWebhook = require('@slack/client').IncomingWebhook; | ||
**Just starting out?** We suggest starting at the | ||
[Getting Started guide](https://slackapi.github.io/node-slack-sdk/getting_started.md) which will walk you | ||
through building your first Slack app using Node.js. | ||
|
||
var url = process.env.SLACK_WEBHOOK_URL || ''; | ||
**Not sure about which APIs are right for your app?** Read our | ||
[helpful blog post](https://medium.com/slack-developer-blog/getting-started-with-slacks-apis-f930c73fc889) | ||
that explains and compares the options. If you're still not sure, | ||
[reach out for help](#getting-help) and our community can guide you. | ||
|
||
var webhook = new IncomingWebhook(url); | ||
## Examples | ||
|
||
webhook.send('Hello there', function(err, header, statusCode, body) { | ||
if (err) { | ||
console.log('Error:', err); | ||
} else { | ||
console.log('Received', statusCode, 'from Slack'); | ||
} | ||
}); | ||
``` | ||
### Posting a message with Web API | ||
|
||
## Posting a message with Web API | ||
Your app will interact with the Web API through the `WebClient` object, which a top level export | ||
from this package. At a minimum, you need to instantiate it with a token. The example below shows | ||
how to post a message into a channel, DM, MPDM, or group. This will require either the | ||
`chat:user:write` or `chat:bot:write` scopes. | ||
|
||
You'll need a Web API token to call any of the Slack Web API methods. For custom integrations, you'll get this | ||
[from the token generator](https://api.slack.com/docs/oauth-test-tokens), and for apps it will come as the final part | ||
of the [OAuth dance](https://api.slack.com/docs/oauth). | ||
```javascript | ||
const { WebClient } = require('@slack/client'); | ||
|
||
Your app will interact with the Web API through the `WebClient` object, which requires an access token to operate. | ||
// An access token (from your Slack app or custom integration - xoxp, xoxb, or xoxa) | ||
const token = process.env.SLACK_TOKEN; | ||
|
||
```js | ||
var WebClient = require('@slack/client').WebClient; | ||
const web = new WebClient(token); | ||
|
||
var token = process.env.SLACK_API_TOKEN || ''; | ||
// The first argument can be a channel ID, a DM ID, a MPDM ID, or a group ID | ||
const channelId = 'C1232456'; | ||
|
||
var web = new WebClient(token); | ||
web.chat.postMessage('C1232456', 'Hello there', function(err, res) { | ||
if (err) { | ||
console.log('Error:', err); | ||
} else { | ||
console.log('Message sent: ', res); | ||
} | ||
}); | ||
// See: https://api.slack.com/methods/chat.postMessage | ||
web.chat.postMessage(channelId, 'Hello there') | ||
.then((res) => { | ||
// `res` contains information about the posted message | ||
console.log('Message sent: ', res.ts); | ||
}) | ||
.catch(console.error); | ||
``` | ||
|
||
## Posting a message with the Real-Time Messaging API | ||
The `WebClient` object makes it simple to call any of the | ||
[**over 130 Web API methods**](https://api.slack.com/methods). See the | ||
[guide](http://slackapi.github.io/node-slack-sdk/web_api) for details. | ||
|
||
### Posting a message with the Real-Time Messaging API | ||
|
||
Starting a bot up requires a bot token (bot tokens start with `xoxb-`), | ||
which can be had either creating a [custom bot](https://my.slack.com/apps/A0F7YS25R-bots) or by creating an app with a | ||
bot user, at the end of the [OAuth dance](https://api.slack.com/docs/oauth). If you aren't sure path is right for you, | ||
have a look at the [Bot Users documentation](https://api.slack.com/bot-users). | ||
Your app will interact with the RTM API through the `RtmClient` object, which is a top level | ||
export from this package. At a minimum, you need to instantiate it with a token, usually a | ||
bot token. | ||
|
||
```js | ||
var RtmClient = require('@slack/client').RtmClient; | ||
var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS; | ||
```javascript | ||
const { RtmClient, CLIENT_EVENTS } = require('@slack/client'); | ||
|
||
var bot_token = process.env.SLACK_BOT_TOKEN || ''; | ||
// An access token (from your Slack app or custom integration - usually xoxb) | ||
const token = process.env.SLACK_TOKEN; | ||
|
||
var rtm = new RtmClient(bot_token); | ||
// Cache of data | ||
const appData = {}; | ||
|
||
let channel; | ||
// Initialize the RTM client with the recommended settings. Using the defaults for these | ||
// settings is deprecated. | ||
const rtm = new RtmClient(token, { | ||
dataStore: false, | ||
useRtmConnect: true, | ||
}); | ||
|
||
// The client will emit an RTM.AUTHENTICATED event on successful connection, with the `rtm.start` payload | ||
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => { | ||
for (const c of rtmStartData.channels) { | ||
if (c.is_member && c.name ==='general') { channel = c.id } | ||
} | ||
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`); | ||
// The client will emit an RTM.AUTHENTICATED event on when the connection data is avaiable | ||
// (before the connection is open) | ||
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (connectData) => { | ||
// Cache the data necessary for this app in memory | ||
appData.selfId = connectData.self.id; | ||
console.log(`Logged in as ${appData.selfId} of team ${connectData.team.id}`); | ||
}); | ||
|
||
// you need to wait for the client to fully connect before you can send messages | ||
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function () { | ||
rtm.sendMessage("Hello!", channel); | ||
// The client will emit an RTM.RTM_CONNECTION_OPEN the connection is ready for | ||
// sending and recieving messages | ||
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPEN, () => { | ||
console.log(`Ready`); | ||
}); | ||
|
||
// Start the connecting process | ||
rtm.start(); | ||
``` | ||
|
||
The `RtmClient` object makes it simple to listen for [events](https://api.slack.com/rtm#events) from | ||
and send simple messages to a workspace. See the | ||
[guide](http://slackapi.github.io/node-slack-sdk/rtm_api) for details. | ||
|
||
## Posting a message with Incoming Webhooks | ||
|
||
[Incoming webhooks](https://api.slack.com/incoming-webhooks) are an easy way to send notifications | ||
to a Slack channel with a minimum of setup. You'll need a webhook URL from a Slack app or a custom | ||
integration to use the `IncomingWebhook` class. | ||
|
||
```javascript | ||
const { IncomingWebhook } = require('@slack/client'); | ||
const url = process.env.SLACK_WEBHOOK_URL; | ||
const webhook = new IncomingWebhook(url); | ||
|
||
// Send simple text to the webhook channel | ||
webhook.send('Hello there', function(err, res) { | ||
if (err) { | ||
console.log('Error:', err); | ||
} else { | ||
console.log('Message sent: ', res); | ||
} | ||
}); | ||
``` | ||
|
||
## Getting Help | ||
|
||
If you get stuck, we're here to help. The following are the best ways to get assistance working through your issue: | ||
|
||
* [Issue Tracker](http://github.com/slackapi/node-slack-sdk/issues) for questions, feature | ||
requests, bug reports and general discussion related to this package. | ||
* [Email us](mailto:[email protected]) in Slack developer support: `[email protected]` | ||
* [Bot Developers Hangout](https://community.botkit.ai/): a Slack community for developers | ||
building all types of bots. You can find the maintainers and users of this package in **#sdk-node-slack-sdk**. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,6 @@ collections: | |
|
||
# Build settings | ||
markdown: kramdown | ||
gems: | ||
- jemoji | ||
plugins: | ||
- jemoji | ||
- jekyll-redirect-from |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.