# messaging-api-slack

> Messaging API client for Slack

Latest version **1.1.0** (published 2021-10-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install messaging-api-slack
pnpm add messaging-api-slack
yarn add messaging-api-slack
bun add messaging-api-slack
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2021-10-04 |
| First published | 2017-07-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 8 |
| Unpacked size | 243.8 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1937 |
| Maintainers | chentsulin, kpman, tw0517tw |
| Keywords | bot, chatbot, messaging-apis, slack |

## Links

- npm: https://www.npmjs.com/package/messaging-api-slack
- Repository: https://github.com/Yoctol/messaging-apis
- Homepage: https://github.com/Yoctol/messaging-apis#readme
- Issues: https://github.com/Yoctol/messaging-apis/issues
- npm.io page: https://npm.io/package/messaging-api-slack

## Dependencies (8)

- [axios](https://npm.io/package/axios.md) ^0.21.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [warning](https://npm.io/package/warning.md) ^4.0.3
- [axios-error](https://npm.io/package/axios-error.md) ^1.0.4
- [ts-invariant](https://npm.io/package/ts-invariant.md) ^0.4.4
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.156
- [@types/warning](https://npm.io/package/@types/warning.md) ^3.0.0
- [messaging-api-common](https://npm.io/package/messaging-api-common.md) ^1.0.4

## Recent versions

- 1.1.0 (latest) — 2021-10-04
- 1.0.0-beta.33 (next) — 2020-07-22
- 1.0.6 — 2021-09-02
- 1.0.4 — 2021-01-11
- 1.0.3 — 2020-10-20
- 1.0.1 — 2020-09-21
- 1.0.0 — 2020-09-07
- 1.0.0-beta.29 — 2020-06-29
- 1.0.0-beta.27 — 2020-06-23
- 1.0.0-beta.26 — 2020-06-16
- 1.0.0-beta.25 — 2020-06-11
- 1.0.0-beta.24 — 2020-06-11
- 1.0.0-beta.23 — 2020-05-18
- 0.7.18 — 2020-04-08
- 0.7.17 — 2020-04-08
- … 63 more at https://npm.io/package/messaging-api-slack/versions

## README

# messaging-api-slack

> Messaging API client for Slack

<img src="https://cdn-images-1.medium.com/max/1200/1*TiKyhAN2gx4PpbOsiBhYcw.png" alt="Slack" width="150" />

## Table of Contents

- [Installation](#installation)
- [OAuth Client](#oauth-client)
  - [Usage](#usage)
  - [API Reference](#api-reference)
- [Webhook Client](#webhook-client)
  - [Usage](#usage-1)
  - [API Reference](#api-reference-1)
- [Debug Tips](#debug-tips)
- [Testing](#testing)

## Installation

```sh
npm i --save messaging-api-slack
```

or

```sh
yarn add messaging-api-slack
```

<br />

## OAuth Client

### Usage

Get your bot user OAuth access token by setup OAuth & Permissions function to your app or check the [Using OAuth 2.0](https://api.slack.com/docs/oauth) document.

```js
const { SlackOAuthClient } = require('messaging-api-slack');

// get access token by setup OAuth & Permissions function to your app.
// https://api.slack.com/docs/oauth
const client = new SlackOAuthClient({
  accessToken: 'xoxb-000000000000-xxxxxxxxxxxxxxxxxxxxxxxx',
});
```

#### Error Handling

`messaging-api-slack` uses [axios](https://github.com/axios/axios) as HTTP client. We use [axios-error](https://github.com/Yoctol/messaging-apis/tree/master/packages/axios-error) package to wrap API error instances for better formatting error messages. Directly `console.log` on the error instance will return formatted message. If you'd like to get the axios `request`, `response`, or `config`, you can still get them via those keys on the error instance.

```js
client.callMethod(method, body).catch((error) => {
  console.log(error); // formatted error message
  console.log(error.stack); // error stack trace
  console.log(error.config); // axios request config
  console.log(error.request); // HTTP request
  console.log(error.response); // HTTP response
});
```

<br />

### API Reference

All methods return a Promise.

<br />

#### Call available methods

## `callMethod(method, body)` - [Official docs](https://api.slack.com/methods)

Calling any API methods which follow [slack calling conventions](https://api.slack.com/web#basics).

| Param  | Type     | Description                                         |
| ------ | -------- | --------------------------------------------------- |
| method | `String` | One of [API Methods](https://api.slack.com/methods) |
| body   | `Object` | Body that the method needs.                         |

Example:

```js
client.callMethod('chat.postMessage', { channel: 'C8763', text: 'Hello!' });
```

<br />

#### Chat API

- [chat.postMessage](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#chat)
- [chat.postEphemeral](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#chat)

<br />

#### Users API

- [getUserList](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#getuserlist)
- [getAllUserList](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#getalluserlist)
- [getUserInfo](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#getuserinfo)

<br />

#### Channels API

- [getChannelInfo](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#getchannelinfo)

<br />

#### Conversations API

- [getConversationInfo](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#getconversationinfo)
- [getConversationMembers](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#getconversationmembers)
- [getAllConversationMembers](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#getallconversationmembers)
- [getConversationList](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#getconversationlist)
- [getAllConversationList](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackoauthclient-1.html#getallconversationlist)

<br />

## Webhook Client

## Usage

Get your webhook url by adding a [Incoming Webhooks](https://api.slack.com/incoming-webhooks) integration to your team or setup Incoming Webhooks function to your app.

```js
const { SlackWebhookClient } = require('messaging-api-slack');

// get webhook URL by adding a Incoming Webhook integration to your team.
// https://my.slack.com/services/new/incoming-webhook/
const client = new SlackWebhookClient({
  url: 'https://hooks.slack.com/services/XXXXXXXX/YYYYYYYY/zzzzzZZZZZ',
});
```

<br />

## API Reference

All methods return a Promise.

<br />

### Send API - [Official docs](https://api.slack.com/docs/messages)

- [sendRawBody](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackwebhookclient-1.html#sendrawbody)
- [sendText](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackwebhookclient-1.html#sendtext)
- [sendAttachments](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackwebhookclient-1.html#sendattachments)
- [sendAttachment](https://yoctol.github.io/messaging-apis/latest/classes/messaging_api_slack.slackwebhookclient-1.html#sendattachment)

<br />

## Debug Tips

### Log Requests Details

To enable default request debugger, use following `DEBUG` env variable:

```sh
DEBUG=messaging-api:request
```

If you want to use a custom request logging function, just provide your own `onRequest`:

```js
// for SlackOAuthClient
const client = new SlackOAuthClient({
  accessToken: ACCESS_TOKEN,
  onRequest: ({ method, url, headers, body }) => {
    /* */
  },
});

// for SlackWebhookClient
const client = new SlackWebhookClient({
  url: URL,
  onRequest: ({ method, url, headers, body }) => {
    /* */
  },
});
```

## Testing

### Point Requests to Your Dummy Server

To avoid sending requests to real Slack server, specify the `origin` option when constructing your client:

```js
const { SlackOAuthClient } = require('messaging-api-slack');

const client = new SlackOAuthClient({
  accessToken: ACCESS_TOKEN,
  origin: 'https://mydummytestserver.com',
});
```

> Warning: Don't do this on your production server.

---
_Source: https://npm.io/package/messaging-api-slack · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
