slack-bot-commands

1.0.4 • Public • Published

Slack Bot commands

A quick and easy way to get set up with a slack bot, by having an extensible class that listens to messages to run commands.

Getting Set Up

  1. Create yourself a slack app, (see instructions on slack), and get a slack team ID to pass to your bot.

  2. Create an index:

const token = process.env.SLACK_TOKEN;
const commands = require('./commands');
const slackBot = require('slack-bot-commands')
 
let res =  slackBot(commands, token);
 
module.exports = res;

The res will be a function that when called, will set up a service worker listening for messages on the app. All messages that are not bot messages, will be passed to your commands.

  1. Building your commands.

We have a class that can be exported and extended to create your commands, and then the base classes can be returned in an array as the commands to the SlackBot function.

const { Command } = require('slack-bot-commands');
 
class ourCommand extends Command {
    constructor(message) {
        super(message);
        this.testRegex = /^some action/i;
        this.name = 'Our New Action';
    }
 
    action() {
        return 'This message will be printed when the message matches the regex';
    }
}
  1. Behind the scenes

The Command class has a 'test' method, which will be called. If it returns true, the action will be called, and the result will be passed back to slack. All commands are tested for every non-bot slack message.

Other Notes

There is a 'help' command by default, which will list all the commands available, what their test regexes are, and their descriptions.

Hidden export:

const { CommandTester } = require('slack-bot-commands');
const someCommand = require('./someCommand');
 
CommandTester(someCommand, {
    text: 'our slack message',
    user: '1234',
    channel: '5678',
})

This will test the command, tell you if it doesn't pass the test, and if it passes, run the action, logging the result of the action. This can help you iterate your commands without clogging up a test slack channel.

Dependents (0)

Package Sidebar

Install

npm i slack-bot-commands

Weekly Downloads

1

Version

1.0.4

License

MIT

Last publish

Collaborators

  • noviny