messaging-api-slack
Advanced tools
Comparing version 0.2.6 to 0.3.0
@@ -5,4 +5,3 @@ 'use strict';var _SlackWebhookClient = require('./SlackWebhookClient');var _SlackWebhookClient2 = _interopRequireDefault(_SlackWebhookClient); | ||
module.exports = { | ||
SlackClient: _SlackWebhookClient2.default, | ||
SlackOAuthClient: _SlackOAuthClient2.default, | ||
SlackWebhookClient: _SlackWebhookClient2.default }; |
@@ -87,2 +87,3 @@ 'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;};var _querystring = require('querystring');var _querystring2 = _interopRequireDefault(_querystring); | ||
// https://api.slack.com/methods/users.info | ||
@@ -98,3 +99,3 @@ | ||
}exports.default = SlackOAuthClient;SlackOAuthClient.connect = token => new SlackOAuthClient(token);var _initialiseProps = function _initialiseProps() {var _this = this;this.getHTTPClient = () => this._http;this.callMethod = (method, body = {}) => {body.token = this._token; // eslint-disable-line no-param-reassign | ||
return this._http.post(method, _querystring2.default.stringify(body)).then(res => {if (!res.data.ok) {throw new Error(res.data.error);}return res.data;});};this.postMessage = (channel, text, options = {}) => this.callMethod('chat.postMessage', _extends({ channel, text }, options));this.getUserList = cursor => this.callMethod('users.list', { cursor }).then(data => data.members);this.getAllUserList = _asyncToGenerator(function* () {let allUsers = [];let continuationCursor;do {var _ref2 = yield _this.callMethod('users.list', { cursor: continuationCursor });const users = _ref2.members;var _ref2$response_metada = _ref2.response_metadata;_ref2$response_metada = _ref2$response_metada === undefined ? {} : _ref2$response_metada;const next = _ref2$response_metada.next_cursor;allUsers = allUsers.concat(users);continuationCursor = next;} while (continuationCursor);return allUsers;});this.getUserInfo = userId => this.callMethod('users.info', { user: userId }).then(data => data.user);this.getChannelList = () => this.callMethod('channels.list').then(data => data.channels);this.getChannelInfo = channelId => this.callMethod('channels.info', { channel: channelId }).then( | ||
return this._http.post(method, _querystring2.default.stringify(body)).then(res => {if (!res.data.ok) {throw new Error(res.data.error);}return res.data;});};this.postMessage = (channel, text, options = {}) => this.callMethod('chat.postMessage', _extends({ channel, text }, options));this.getUserList = cursor => this.callMethod('users.list', { cursor }).then(data => ({ members: data.members, next: data.response_metadata && data.response_metadata.next_cursor }));this.getAllUserList = _asyncToGenerator(function* () {let allUsers = [];let continuationCursor;do {var _ref2 = yield _this.getUserList(continuationCursor);const users = _ref2.members,next = _ref2.next;allUsers = allUsers.concat(users);continuationCursor = next;} while (continuationCursor);return allUsers;});this.getUserInfo = userId => this.callMethod('users.info', { user: userId }).then(data => data.user);this.getChannelList = () => this.callMethod('channels.list').then(data => data.channels);this.getChannelInfo = channelId => this.callMethod('channels.info', { channel: channelId }).then( | ||
data => data.channel);}; |
{ | ||
"name": "messaging-api-slack", | ||
"description": "Messaging API client for Slack", | ||
"version": "0.2.6", | ||
"version": "0.3.0", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">=6" |
@@ -27,2 +27,4 @@ # messaging-api-slack | ||
<br /> | ||
## OAuth Client | ||
@@ -44,2 +46,4 @@ | ||
<br /> | ||
### API Reference | ||
@@ -49,2 +53,4 @@ | ||
<br /> | ||
#### Call available methods | ||
@@ -54,2 +60,4 @@ | ||
Calling any API methods which follow [slack calling conventions](https://api.slack.com/web#basics). | ||
###### method | ||
@@ -68,2 +76,4 @@ | ||
<br /> | ||
#### Chat API | ||
@@ -73,2 +83,4 @@ | ||
Sends a message to a channel. | ||
###### channel | ||
@@ -86,2 +98,6 @@ | ||
###### options.as_user | ||
Type: `Boolean` | ||
```js | ||
@@ -92,2 +108,4 @@ client.postMessage('C8763', 'Hello!'); | ||
<br /> | ||
#### Users API | ||
@@ -97,2 +115,4 @@ | ||
Lists all users in a Slack team. | ||
###### cursor | ||
@@ -103,13 +123,20 @@ | ||
```js | ||
client.getUserList(CURSOR).then(res => { | ||
client.getUserList(cursor).then(res => { | ||
console.log(res); | ||
// [ | ||
// { ... }, | ||
// { ... }, | ||
// ] | ||
// { | ||
// members: [ | ||
// { ... }, | ||
// { ... }, | ||
// ], | ||
// next: 'abcdefg', | ||
// } | ||
}); | ||
``` | ||
<br /> | ||
##### getAllUserList() - [Official docs](https://api.slack.com/methods/users.list) | ||
Recursively lists all users in a Slack team using cursor. | ||
```js | ||
@@ -125,4 +152,8 @@ client.getAllUserList().then(res => { | ||
<br /> | ||
##### getUserInfo(userId) - [Official docs](https://api.slack.com/methods/users.info) | ||
Gets information about an user. | ||
###### userId | ||
@@ -143,2 +174,4 @@ | ||
<br /> | ||
#### Channels API | ||
@@ -148,2 +181,4 @@ | ||
Lists all channels in a Slack team. | ||
```js | ||
@@ -161,2 +196,4 @@ client.getChannelList().then(res => { | ||
Gets information about a channel. | ||
###### channelId | ||
@@ -177,2 +214,4 @@ | ||
<br /> | ||
## Webhook Client | ||
@@ -194,2 +233,4 @@ | ||
<br /> | ||
### API Reference | ||
@@ -199,2 +240,4 @@ | ||
<br /> | ||
#### Send API - [Official docs](https://api.slack.com/docs/messages) | ||
@@ -212,2 +255,4 @@ | ||
<br /> | ||
##### sendText(text) | ||
@@ -223,2 +268,4 @@ | ||
<br /> | ||
##### sendAttachments(attachments) - [Official docs](https://api.slack.com/docs/message-attachments) | ||
@@ -259,2 +306,4 @@ | ||
<br /> | ||
##### sendAttachment(attachment) - [Official docs](https://api.slack.com/docs/message-attachments) | ||
@@ -261,0 +310,0 @@ |
@@ -1,7 +0,6 @@ | ||
import { SlackClient, SlackWebhookClient, SlackOAuthClient } from '../index'; | ||
import { SlackWebhookClient, SlackOAuthClient } from '../index'; | ||
it('should export api correctly', () => { | ||
expect(SlackClient).toBeDefined(); | ||
expect(SlackWebhookClient).toBeDefined(); | ||
expect(SlackOAuthClient).toBeDefined(); | ||
}); |
@@ -301,4 +301,102 @@ import querystring from 'querystring'; | ||
expect(res).toEqual(members); | ||
expect(res).toEqual({ members, next: 'dXNlcjpVMEc5V0ZYTlo=' }); | ||
}); | ||
it('support no cursor in reply', async () => { | ||
const { client, mock } = createMock(); | ||
const members = [ | ||
{ | ||
id: 'U023BECGF', | ||
team_id: 'T021F9ZE2', | ||
name: 'bobby', | ||
deleted: false, | ||
color: '9f69e7', | ||
real_name: 'Bobby Tables', | ||
tz: 'America/Los_Angeles', | ||
tz_label: 'Pacific Daylight Time', | ||
tz_offset: -25200, | ||
profile: { | ||
avatar_hash: 'ge3b51ca72de', | ||
current_status: ':mountain_railway: riding a train', | ||
first_name: 'Bobby', | ||
last_name: 'Tables', | ||
real_name: 'Bobby Tables', | ||
email: 'bobby@slack.com', | ||
skype: 'my-skype-name', | ||
phone: '+1 (123) 456 7890', | ||
image_24: 'https://...', | ||
image_32: 'https://...', | ||
image_48: 'https://...', | ||
image_72: 'https://...', | ||
image_192: 'https://...', | ||
}, | ||
is_admin: true, | ||
is_owner: true, | ||
updated: 1490054400, | ||
has_2fa: false, | ||
}, | ||
{ | ||
id: 'W07QCRPA4', | ||
team_id: 'T0G9PQBBK', | ||
name: 'glinda', | ||
deleted: false, | ||
color: '9f69e7', | ||
real_name: 'Glinda Southgood', | ||
tz: 'America/Los_Angeles', | ||
tz_label: 'Pacific Daylight Time', | ||
tz_offset: -25200, | ||
profile: { | ||
avatar_hash: '8fbdd10b41c6', | ||
image_24: 'https://a.slack-edge.com...png', | ||
image_32: 'https://a.slack-edge.com...png', | ||
image_48: 'https://a.slack-edge.com...png', | ||
image_72: 'https://a.slack-edge.com...png', | ||
image_192: 'https://a.slack-edge.com...png', | ||
image_512: 'https://a.slack-edge.com...png', | ||
image_1024: 'https://a.slack-edge.com...png', | ||
image_original: 'https://a.slack-edge.com...png', | ||
first_name: 'Glinda', | ||
last_name: 'Southgood', | ||
title: 'Glinda the Good', | ||
phone: '', | ||
skype: '', | ||
real_name: 'Glinda Southgood', | ||
real_name_normalized: 'Glinda Southgood', | ||
email: 'glenda@south.oz.coven', | ||
}, | ||
is_admin: true, | ||
is_owner: false, | ||
is_primary_owner: false, | ||
is_restricted: false, | ||
is_ultra_restricted: false, | ||
is_bot: false, | ||
updated: 1480527098, | ||
has_2fa: false, | ||
}, | ||
]; | ||
const reply = { | ||
ok: true, | ||
members, | ||
cache_ts: 1498777272, | ||
}; | ||
mock | ||
.onPost( | ||
'/users.list', | ||
querystring.stringify({ | ||
cursor: undefined, | ||
token: TOKEN, | ||
}), | ||
{ | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
} | ||
) | ||
.reply(200, reply); | ||
const res = await client.getUserList(); | ||
expect(res).toEqual({ members, next: undefined }); | ||
}); | ||
}); | ||
@@ -305,0 +403,0 @@ |
@@ -5,5 +5,4 @@ import SlackWebhookClient from './SlackWebhookClient'; | ||
module.exports = { | ||
SlackClient: SlackWebhookClient, | ||
SlackOAuthClient, | ||
SlackWebhookClient, | ||
}; |
@@ -66,3 +66,6 @@ import querystring from 'querystring'; | ||
getUserList = (cursor?: string): Promise<Array<User>> => | ||
this.callMethod('users.list', { cursor }).then(data => data.members); | ||
this.callMethod('users.list', { cursor }).then(data => ({ | ||
members: data.members, | ||
next: data.response_metadata && data.response_metadata.next_cursor, | ||
})); | ||
@@ -76,7 +79,5 @@ getAllUserList = async (): Promise<Array<User>> => { | ||
members: users, | ||
response_metadata: { next_cursor: next } = {}, | ||
next, | ||
// eslint-disable-next-line no-await-in-loop | ||
} = await this.callMethod('users.list', { | ||
cursor: continuationCursor, | ||
}); | ||
} = await this.getUserList(continuationCursor); | ||
allUsers = allUsers.concat(users); | ||
@@ -83,0 +84,0 @@ continuationCursor = next; |
36649
963
309