Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support some Slack conversations API #185

Merged
merged 1 commit into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 130 additions & 1 deletion packages/messaging-api-slack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Calling any API methods which follow [slack calling conventions](https://api.sla

Param | Type | Description
------ | -------- | -----------
method | `String` | One of <code>`chat.postMessage` &#124; `channels.info` &#124; `channels.list` &#124; `users.info` &#124; `users.list`</code>
method | `String` | One of [API Methods](https://api.slack.com/methods)
body | `Object` | Body that the method needs.

Example:
Expand Down Expand Up @@ -170,6 +170,8 @@ client.getChannelList().then(res => {
});
```

<br />

## `getChannelInfo(channelId)` - [Official docs](https://api.slack.com/methods/channels.info)

Gets information about a channel.
Expand All @@ -192,6 +194,133 @@ client.getChannelInfo(channelId).then(res => {

<br />

#### Conversasions API

## `getConversationInfo(channelId)` - [Official docs](https://api.slack.com/methods/conversations.info)

Retrieve information about a conversation.

Param | Type | Description
--------- | -------- | -----------
channelId | `String` | Channel to get info on.

Example:
```js
client.getConversationInfo(channelId).then(res => {
console.log(res);
// {
// id: 'C8763',
// name: 'fun',
// ...
// }
});
```

<br />

## `getConversationMembers(channelId, options)` - [Official docs](https://api.slack.com/methods/conversations.members)

Retrieve members of a conversation.

Param | Type | Description
--------- | -------- | -----------
channelId | `String` | Channel to get info on.
options | `Object` | Optional arguments.

Example:
```js
client.getConversationMembers(channelId, { cursor: 'xxx' })
client.getConversationMembers(channelId).then(res => {
console.log(res);
// {
// members: ['U061F7AUR', 'U0C0NS9HN'],
// next: 'cursor',
// }
});
```

<br />

## `getAllConversationMembers(channelId)` - [Official docs](https://api.slack.com/methods/conversations.members)

Recursively retrieve members of a conversation using cursor.

Param | Type | Description
--------- | -------- | -----------
channelId | `String` | Channel to get info on.

Example:
```js
client.getAllConversationMembers(channelId).then(res => {
console.log(res);
// ['U061F7AUR', 'U0C0NS9HN', ...]
});
```

<br />

## `getConversationList(options)` - [Official docs](https://api.slack.com/methods/conversations.list)

Lists all channels in a Slack team.

Param | Type | Description
--------- | -------- | -----------
options | `Object` | Optional arguments.

Example:
```js
client.getConversationList({ cursor: 'xxx' })
client.getConversationList().then(res => {
console.log(res);
// {
// channels: [
// {
// id: 'C012AB3CD',
// name: 'general',
// ...
// },
// {
// id: 'C012AB3C5',
// name: 'random',
// ...
// },
// ],
// next: 'cursor',
// }
});
```

<br />

## `getAllConversationList(options)` - [Official docs](https://api.slack.com/methods/conversations.list)

Recursively lists all channels in a Slack team using cursor.

Param | Type | Description
--------- | -------- | -----------
options | `Object` | Optional arguments.

Example:
```js
client.getAllConversationList().then(res => {
console.log(res);
// [
// {
// id: 'C012AB3CD',
// name: 'general',
// ...
// },
// {
// id: 'C012AB3C5',
// name: 'random',
// ...
// },
// ],
});
```

<br />

## Webhook Client

## Usage
Expand Down
108 changes: 106 additions & 2 deletions packages/messaging-api-slack/src/SlackOAuthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ type GetInfoOptions = {
include_locale?: boolean,
};

type WithCursorOptions = {
cursor?: string,
};

type ConversationListOptions = {
cursor?: string,
exclude_archived?: boolean,
limit?: number,
types?: string,
};

export default class SlackOAuthClient {
static connect = (token: Token): SlackOAuthClient =>
new SlackOAuthClient(token);
Expand Down Expand Up @@ -98,7 +109,7 @@ export default class SlackOAuthClient {
};

/**
* Gets information about a channel.
* Gets information about a public channel.
*
* https://api.slack.com/methods/channels.info
*/
Expand All @@ -111,14 +122,107 @@ export default class SlackOAuthClient {
);

/**
* Lists all channels in a Slack team.
* Lists all public channels in a Slack team.
*
* https://api.slack.com/methods/channels.list
* FIXME: [breaking] support cursor, exclude_archived, exclude_members, limit
*/
getChannelList = (): Promise<Array<SlackChannel>> =>
this.callMethod('channels.list').then(data => data.channels);

/**
* Retrieve information about a conversation.
*
* https://api.slack.com/methods/conversations.info
*/
getConversationInfo = (
channelId: string,
options: GetInfoOptions = {}
): Promise<SlackChannel> =>
this.callMethod('conversations.info', {
channel: channelId,
...options,
}).then(data => data.channel);

/**
* Retrieve members of a conversation.
*
* https://api.slack.com/methods/conversations.members
*/
getConversationMembers = (
channelId: string,
options: WithCursorOptions = {}
): Promise<{
members: Array<string>,
next: ?string,
}> =>
this.callMethod('conversations.members', {
channel: channelId,
...options,
}).then(data => ({
members: data.members,
next: data.response_metadata && data.response_metadata.next_cursor,
}));

getAllConversationMembers = async (
channelId: string
): Promise<Array<string>> => {
let allMembers = [];
let continuationCursor;

do {
const {
members,
next,
// eslint-disable-next-line no-await-in-loop
} = await this.getConversationMembers(channelId, {
cursor: continuationCursor,
});
allMembers = allMembers.concat(members);
continuationCursor = next;
} while (continuationCursor);

return allMembers;
};

/**
* Lists all channels in a Slack team.
*
* https://api.slack.com/methods/conversations.list
*/
getConversationList = (
options: ConversationListOptions = {}
): Promise<{
channels: Array<SlackChannel>,
next: ?string,
}> =>
this.callMethod('conversations.list', options).then(data => ({
channels: data.channels,
next: data.response_metadata && data.response_metadata.next_cursor,
}));

getAllConversationList = async (
options: ConversationListOptions = {}
): Promise<Array<SlackChannel>> => {
let allChannels = [];
let continuationCursor;

do {
const {
channels,
next,
// eslint-disable-next-line no-await-in-loop
} = await this.getConversationList({
...options,
cursor: continuationCursor,
});
allChannels = allChannels.concat(channels);
continuationCursor = next;
} while (continuationCursor);

return allChannels;
};

/**
* Sends a message to a channel.
*
Expand Down
Loading