slack-basebot
v2.0.1
Published
BaseBot for Slack
Downloads
4
Readme
Slack BaseBot
Install
npm install slack-basebot --save
Simple Example
"use strict";
const BaseBot = require('slack-basebot');
const MyBot = new BaseBot('xob-slacktoken','MyBotName', {debug:true});
MyBot.onDirectQuestion(/please help me/i, msg => {
//== Will trigger when someone says `MyBotName please help me` or with a mention like `please help me @MyBotName`;
//== We can react with an emoji
msg.react('heart');
//== We can auto-randomly react with an emoji
msg.react(['heart','black_heart','green_heart']);
//== And/or reply (as often as we like)
msg.reply(`What can I help you with ${msg.user.name}?`);
//== Randomize the output with an array:
msg.reply(['Sometimes I say foo','That other time I will say bar']);
//== Furthermore we have some more user, channel and message information
console.info('@User:', msg.user);
console.info('#Channel', msg.channel);
console.info('Is this a DM?', msg.isDirect);
console.info('Full message ', msg.message);
console.info('@user said: ', msg.text);
})
MyBot.onPhrase(/squirl/gi, msg => {
//== This will trigger everytime anyone says something containing squirl.
msg.react('squirrel');
//== Pretend we are typing for 3 seconds
msg.typing(3 * 1000).then(() => {
msg.reply('I took me 3 seconds to write this.');
});
});
MyBot.setHelpText(() => `Hi I'm ${MyBot.tag}. I can do stuff for you.`);
MyBot.always(msg => console.info('Triggered on every incoming message'));
MyBot.onNothing(msg => console.info('Triggered on DirectQuestion/mention when no rules are matched.'));
//== Any sub-event can be listened on (see https://api.slack.com/events/message)
MyBot.on('channel_leave', event => console.info('Event happened', event));
//== On ChannelJoin of this bot
this.onChannelJoin(event => this.sendMessage(`Thanks <@${event.inviter}> for inviting me to <#${event.channel}>. How can I help you?`, event.channel));
Inheritance Example
"use strict";
const BaseBot = require('slack-basebot');
class MyBot extends BaseBot {
constructor() {
super('xob-slacktoken','MyBotName', {debug:true});
this.start();
}
start() {
this.onDirectQuestion(/please help me/i, msg => { /* Same as example above */})
this.onPhrase(/squirl/gi, msg => { /* Same as example above */});
this.setHelpText(() => `Hi I'm ${MyBot.tag}. I can do stuff for you.`);
this.always(msg => console.info('Triggered on every incoming message'));
this.onNothing(msg => console.info('Triggered on DirectQuestion/mention when no rules are matched.'));
}
}