SlackClient
Usage
Get the SlackOAuthClient
instance using the getClient
function:
const { getClient } = require('bottender');
const client = getClient('slack');
// `client` is a `SlackOAuthClient` instance
const accountInfo = await context.client.getAccountInfo();
Or, get the SlackOAuthClient
instance from the context
:
async function MyAction(context) {
if (context.platform === 'slack') {
// `context.client` is a `SlackOAuthClient` instance
const accountInfo = await context.client.getAccountInfo();
}
}
Error Handling
SlackOAuthClient
uses axios as HTTP client. We use axios-error package to wrap API error instances for better formatting error messages. Calling console.log
with the error instance returns the 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.
client.callMethod(method, body).catch((error) => {
console.log(error); // the formatted error message
console.log(error.stack); // stack trace of the error
console.log(error.config); // axios request config
console.log(error.request); // axios HTTP request
console.log(error.response); // axios HTTP response
});
Methods
All methods return a Promise.
Call Available Methods
callMethod(method, body)
- Official docs
Call any API methods which follow slack calling conventions.
Param | Type | Description |
---|---|---|
method | String | One of API Methods |
body | Object | Body that the method needs. |
Example:
client.callMethod('chat.postMessage', { channel: 'C8763', text: 'Hello!' });
Chat API
postMessage(channel, message [, options])
- Official docs
Send a message to a channel.
Param | Type | Description |
---|---|---|
channel | String | Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name. |
message | String | Object | The message to be sent, can be text message or attachment message. |
options | Object | Other optional parameters. |
options.accessToken | String | Custom access token of the request. |
Example:
client.postMessage('C8763', { text: 'Hello!' });
client.postMessage('C8763', { attachments: [someAttachments] });
client.postMessage('C8763', 'Hello!');
client.postMessage('C8763', 'Hello!', { asUser: true });
If you send message with attachments
, messaging-api-slack
will automatically stringify the attachments
field for you.
client.postMessage(
'C8763',
{
text: 'Hello!',
attachments: [
{
text: 'Choose a game to play',
fallback: 'You are unable to choose a game',
callbackId: 'wopr_game',
color: '#3AA3E3',
attachmentType: 'default',
actions: [
{
name: 'game',
text: 'Chess',
type: 'button',
value: 'chess',
},
],
},
],
},
{
asUser: true,
}
);
postEphemeral(channel, user, message [, options])
- Official docs
Send an ephemeral message to a user in a channel.
Param | Type | Description |
---|---|---|
channel | String | Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name. |
user | String | id of the user who will receive the ephemeral message. The user should be in the channel specified by the channel argument. |
message | String | Object | The message to be sent, can be text message or attachment message. |
options | Object | Other optional parameters. |
options.accessToken | String | Custom access token of the request. |
Example:
client.postEphemeral('C8763', 'U56781234', { text: 'Hello!' });
client.postEphemeral('C8763', 'U56781234', { attachments: [someAttachments] });
client.postEphemeral('C8763', 'U56781234', 'Hello!');
client.postEphemeral('C8763', 'U56781234', 'Hello!', { asUser: true });
Users API
getUserList(options?)
- Official docs
Lists all users in a Slack team.
Param | Type | Description |
---|---|---|
options | Object | Other optional parameters. |
options.cursor | String | Paginate through collections of data by setting the cursor parameter to a nextCursor attribute returned by a previous request's responseMetadata . |
options.accessToken | String | Custom access token of the request. |
Example:
client.getUserList({ cursor }).then((res) => {
console.log(res);
// {
// members: [
// { ... },
// { ... },
// ],
// next: 'abcdefg',
// }
});
getAllUserList(options?)
- Official docs
Recursively lists all users in a Slack team using cursor.
Param | Type | Description |
---|---|---|
options | Object | Other optional parameters. |
options.accessToken | String | Custom access token of the request. |
Example:
client.getAllUserList().then((res) => {
console.log(res);
// [
// { ... },
// { ... },
// ]
});
getUserInfo(userId, options?)
- Official docs
Gets information about an user.
Param | Type | Description |
---|---|---|
userId | String | User to get info on. |
options | Object | Other optional parameters. |
options.accessToken | String | Custom access token of the request. |
Example:
client.getUserInfo(userId).then((res) => {
console.log(res);
// {
// id: 'U123456',
// name: 'bobby',
// ...
// }
});
Channels API
getChannelList(options?)
- Official docs
Param | Type | Description |
---|---|---|
options | Object | Other optional parameters. |
options.accessToken | String | Custom access token of the request. |
Lists all channels in a Slack team.
Example:
client.getChannelList().then((res) => {
console.log(res);
// [
// { ... },
// { ... },
// ]
});
getChannelInfo(channelId, options?)
- Official docs
Gets information about a channel.
Param | Type | Description |
---|---|---|
channelId | String | Channel to get info on. |
options | Object | Other optional parameters. |
options.accessToken | String | Custom access token of the request. |
Example:
client.getChannelInfo(channelId).then((res) => {
console.log(res);
// {
// id: 'C8763',
// name: 'fun',
// ...
// }
});
Conversasions API
getConversationInfo(channelId, options?)
- Official docs
Retrieve information about a conversation.
Param | Type | Description |
---|---|---|
channelId | String | Channel to get info on. |
options | Object | Other optional parameters. |
options.accessToken | String | Custom access token of the request. |
Example:
client.getConversationInfo(channelId).then((res) => {
console.log(res);
// {
// id: 'C8763',
// name: 'fun',
// ...
// }
});
getConversationMembers(channelId, options?)
- Official docs
Retrieve members of a conversation.
Param | Type | Description |
---|---|---|
channelId | String | Channel to get info on. |
options | Object | Optional arguments. |
options.accessToken | String | Custom access token of the request. |
Example:
client.getConversationMembers(channelId, { cursor: 'xxx' });
client.getConversationMembers(channelId).then((res) => {
console.log(res);
// {
// members: ['U061F7AUR', 'U0C0NS9HN'],
// next: 'cursor',
// }
});
getAllConversationMembers(channelId, options?)
- Official docs
Recursively retrieve members of a conversation using cursor.
Param | Type | Description |
---|---|---|
channelId | String | Channel to get info on. |
options | Object | Other optional parameters. |
options.accessToken | String | Custom access token of the request. |
Example:
client.getAllConversationMembers(channelId).then((res) => {
console.log(res);
// ['U061F7AUR', 'U0C0NS9HN', ...]
});
getConversationList(options?)
- Official docs
Lists all channels in a Slack team.
Param | Type | Description |
---|---|---|
options | Object | Optional arguments. |
options.accessToken | String | Custom access token of the request. |
Example:
client.getConversationList({ cursor: 'xxx' });
client.getConversationList().then((res) => {
console.log(res);
// {
// channels: [
// {
// id: 'C012AB3CD',
// name: 'general',
// ...
// },
// {
// id: 'C012AB3C5',
// name: 'random',
// ...
// },
// ],
// next: 'cursor',
// }
});
getAllConversationList(options?)
- Official docs
Recursively lists all channels in a Slack team using cursor.
Param | Type | Description |
---|---|---|
options | Object | Optional arguments. |
options.accessToken | String | Custom access token of the request. |
Example:
client.getAllConversationList().then((res) => {
console.log(res);
// [
// {
// id: 'C012AB3CD',
// name: 'general',
// ...
// },
// {
// id: 'C012AB3C5',
// name: 'random',
// ...
// },
// ],
});
Debug Tips
Log Requests Details
To enable default request debugger, use following DEBUG
env variable:
DEBUG=messaging-api-slack
Test
Send Requests to Your Dummy Server
To avoid sending requests to the real Slack server, provide the origin
option in your bottender.js.config
file:
module.exports = {
channels: {
viber: {
enabled: true,
path: '/webhooks/slack',
accessToken: process.env.SLACK_ACCESS_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
origin:
process.env.NODE_ENV === 'test'
? 'https://mydummytestserver.com'
: undefined,
},
},
};
Warning: Don't do this on the production server.