Slack Express Middleware
Usage
const {PORT, SCOPE, TOKEN, CLIENT_ID, CLIENT_SECRET} = process.env,
slack = require('express-slack'),
express = require('express'),
app = express();
app.use('/slack', slack({
scope: SCOPE,
token: TOKEN,
store: 'data.json'
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET
}));
slack.on('/test', (payload, bot) => {
bot.reply('works!');
});
app.listen(PORT, () => {
console.log(`Server started on ${PORT}`);
});
API
Middleware
const slack = require('express-slack'),
express = require('express'),
app = express();
app.use('/slack', slack({
scope: 'bot,commands',
token: 'gIkuvaNzQIHg97ATvDxqgjtO',
store: 'data/team.json'
client_id: 'XXXXXXXXXXXX',
client_secret: 'XXXXXXXXXXXX'
}));
Events
slack.on('message', (payload, bot) => { });
slack.on('slash_command', (payload, bot) => { });
slack.on('googlebot', (payload, bot) => { });
slack.on('googlebot', '/test', 'slash_command', (payload, bot) => { });
slack.on('*', (payload, bot) => { });
Event | Description |
---|
***** | All events |
message | All RTM events |
slash_command | All Slash Commands |
event | All Event API callbacks |
webhook | All WebHook callbacks |
interactive_message | All Interactive message callbacks |
[/command] | Any specific slash command |
[event type] | Any specific event type |
[trigger word] | Any trigger from outgoing webhooks |
Bot
Bots are preloaded with the appropriate token and are context aware. So you can reply to messages and send ephemeral updates to a message.
slack.on('message', (payload, bot) => {
bot.replyPrivate('loading...');
bot.reply({
text: 'Everything is working!',
attachments: [{
title: "Slack API Documentation",
title_link: "https://api.slack.com/",
text: "Optional text that appears within the attachment",
fields: [{
title: "Priority",
value: "High",
short: false
}]
}]
});
bot.send('channels.info', { channel: 'C1234567890' }).then(data => {
});
});
Methods | Description |
---|
say | Send a message |
reply | Send a public reply to the event |
replyPrivate | Send an ephemeral reply to the event |
send | Call any Slack API endpoint |
Data Store
A key/value store to maintain team/bot information and store custom setings. The store follows the same interface of a single BotKit Store
slack.store.all().then(results => {
});
slack.store.get(id).then(record => {
});
Methods | Description |
---|
get | Get a single record by id |
all | Get all saved records |
save | Save a record |
Client
The Slack client is a way to call the API outside of an event.
let message = {
unfurl_links: true,
channel: 'C1QD223DS1',
token: 'xoxb-12345678900-ABCD1234567890',
text: "I am a test message http://slack.com",
attachments: [{
text: "And here's an attachment!"
}]
}
slack.send('chat.postMessage', message).then(data => {
});
slack.send('https://hooks.slack.com/services/T0000/B000/XXXX', message);
Instances
let instance = slack.client({
unfurl_links: true,
channel: 'C1QD223DS1',
token: 'xoxb-12345678900-ABCD1234567890'
});
let message = {
text: "I am a test message http://slack.com",
attachments: [{
text: "And here's an attachment!"
}]
};
instance.send('chat.postMessage', message);