Skip to content

Commit

Permalink
major docs content rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
aoberoi committed Dec 22, 2017
1 parent 257d8e6 commit 3fbd77c
Show file tree
Hide file tree
Showing 14 changed files with 1,066 additions and 594 deletions.
190 changes: 116 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**.
5 changes: 3 additions & 2 deletions docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ collections:

# Build settings
markdown: kramdown
gems:
- jemoji
plugins:
- jemoji
- jekyll-redirect-from
2 changes: 1 addition & 1 deletion docs/_includes/page_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<span class="vert_divider"></span>
</a>

<a href="https://api.slack.com/" id="header_logo" class="api hide_on_mobile" style="float:left; display: inline-block;">
<a href="https://api.slack.com/" id="header_logo" class="api hide_on_mobile" style="float:left;">
<img alt="Slack API" src="https://a.slack-edge.com/ae57/img/slack_api_logo.png" style="width: 225px; padding-right: 25px; border-right: 1px solid #DDD;"/>
</a>
<span style="display: inline-block; padding-left: 20px; margin-top: 20px; font-weight: bold;">
Expand Down
7 changes: 4 additions & 3 deletions docs/_includes/side_nav.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<div id="api_sections">

{% comment %}https://github.com/pages-themes/primer/issues/15{% endcomment %}
{% assign sorted_pages = site.pages | where_exp:"page", "page.name != 'style.scss'" | where_exp: "page", "page.layout != 'redirect'" | sort:"order" %}

{% assign sorted_pages = site.pages | sort:"order" %}
{% for node in sorted_pages %}
{% if node.hidden != true %}
<h5><a href="{{ node.url | prepend: site.baseurl }}" class="{% if page.url == node.url %}current{% endif %}">{% if node.title %}{{ node.title }}{% else %}{{ site.title }}{% endif %}</a></h5>
<ul>
{% if node.headings %}
{% if node.headings %}
{% for heading in node.headings %}
{% assign anchor = heading.title | downcase | replace: " ", "-" | remove: ":" | remove: "," | remove: "'" | remove: "." | remove: "?" | remove: "!" %}
<li><a href="{{ node.url | prepend: site.baseurl }}#{{ anchor }}" class="sidebar_menu_list_item">{{ heading.title }}</a></li>
Expand All @@ -23,4 +24,4 @@ <h5>SDK Reference</h5>
</ul>


</div>
</div>
71 changes: 0 additions & 71 deletions docs/_pages/FAQ.md

This file was deleted.

8 changes: 4 additions & 4 deletions docs/_pages/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ order: 9999

{{ site.description }}

{{ site.product_name }} is proudly maintained with :sparkling_heart: by the Slack Developer Tools team
{{ site.product_name }} is proudly maintained with :sparkling_heart: by the Slack Platform team

* [License](https://github.com/{{ site.github_username }}/{{ site.repo_name }}/blob/master/LICENSE)
* [Code of Conduct](https://github.com/{{ site.github_username }}/{{ site.repo_name }}/blob/master/CODE_OF_CONDUCT.md)
* [Contributing](https://github.com/{{ site.github_username }}/{{ site.repo_name }}/blob/master/CONTRIBUTING.md)
* [Contributor License Agreement](https://docs.google.com/a/slack-corp.com/forms/d/e/1FAIpQLSfzjVoCM7ohBnjWf7eDYQxzti1EPpinsIJQA5RAUBwJKRUQHg/viewform)
* [Code of Conduct](https://slackhq.github.io/code-of-conduct)
* [Contributing](https://github.com/{{ site.github_username }}/{{ site.repo_name }}/blob/master/.github/CONTRIBUTING.md)
* [Contributor License Agreement](https://cla-assistant.io/slackapi/node-slack-sdk)
Loading

0 comments on commit 3fbd77c

Please sign in to comment.