slackbot-api
v1.13.2
Published
Simple, consistent and customizable Slack bot API
Downloads
48
Readme
slackbot-api
Simple, understandable Slack Bot API.
#Quick overview ##initialize
import Bot from 'slackbot-api';
let bot = new Bot({
token: process.env.SLACK_BOT_TOKEN // YOUR TOKEN HERE
});
##hear Listen on all incoming messages matching a pattern.
bot.hear(/hi (.*)/, message => {
let name = message.match[1]; // message.match = message.text.exec(regex);
message.reply('Hello!');
})
##listen Listen on all incoming messages mentioning the bot (with or without @)
bot.listen(/help/, message => {
message.reply('You\'ve come to the right person!');
});
##sendMessage
bot.sendMessage('general', 'Hello guys! wassup?');
bot.sendMessage('general', 'Hello', {
unfurl_links: false,
// extra parameters, see https://api.slack.com/methods/chat.postMessage
});
##deleteMessage
let msg = await bot.sendMessage('general', 'Hello guys! wassup?');
msg.delete();
// or
bot.deleteMessage('general', msg.ts);
##updateMessage
let msg = await bot.sendMessage('general', 'i can haz cakez');
msg.update('Yarrrrr!');
// or
bot.updateMessage('general', msg.ts, 'Yarrrrr!');
##react Add reactions to messages.
bot.listen(/rocket/, message => {
message.react('rocket');
})
bot.react('general', msg.ts, 'rocket');
##icon Set bot's profile icon.
bot.icon('warning');
##random Choose an argument randomly. Arguments may also be arrays.
bot.listen(/hey/, message => {
message.reply(bot.random('Hi', 'Hello', 'Wassup'));
})
##on Listen on events.
bot.on('channel_joined', event => {
bot.sendMessage(event.channel.id, 'Hello guys! Thanks for inviting me.');
});
##message events You can also listen on individual messages' events
bot.listen(/list/, message => {
message.on('update', msg => {
msg.reply(`Updated from ${message.text} to ${msg.text}`);
})
message.on('delete', msg => {
msg.reply('Are you hiding something?');
});
message.on('reaction_added', ...);
message.on('reaction_removed', ...);
});
##find Find a channel, user, IM, whatever by it's id or name.
let user = bot.find('mdibaiee');
let channel = bot.find('general');
let group = bot.find('my-secret-group');
let byId = bot.find('U0123456');
##Modifiers In order to create advanced plugins/tasks, you might need to modify behaviour of a function, in order to do that, bolt provides you modifiers.
There are three types of modifiers:
###preprocess Used to modify arguments of a function:
// Allow string patterns
bot.modifiers.preprocess('listen', (pattern, fn) => {
if (typeof pattern === 'string') {
let regex = new RegExp(pattern);
return [regex, fn];
}
return [pattern, fn];
});
###postprocess Used to modify return value of a function:
bot.modifiers.postproess('listen', (bot) => {
return 'Hey, I\'m listen and I\'m returning this!');
})
###middleware Used to decide whether a function's main action should be called or not:
bot.modifiers.middleware('hear', context => {
// Our bot must be polite!
if (context.message.indexOf(BAD_WORD) > -1)
return Promise.reject();
return Promise.resolve();
});