Skip to content

konecnyna/slack-cat

Repository files navigation

SlackCat

license Build Status

When you want slack to turn into a tire fire while also being productive at the same time.

Getting Started

It's ezpz. You can look at the example implementation here.

  1. Recommended Node Version: lts/carbon (v8.11)
  2. run npm install https://github.com/konecnyna/slack-cat.git --save (this assumes you did an npm init in the directory)
  3. Add the configure the configuration file, database path (useful for backing up data), and path to your modules. If you don't have any modules pass an empty string ''.
  4. Add your slack api key to config.json file in the root dir (See below for example).
  5. node index.js
  6. In Slack invite @SlackCat to a channel or direct message and run ?ping. If everything is working the bot should respond with ?pong.
const SlackCat = require('slack-cat');
const Path = require('path');


const configPath = Path.join(__dirname, 'config.json')
const dbPath = Path.join(__dirname, 'db.sqlite');
const modules = Path.join(__dirname, '/modules');

new SlackCat(modules,configPath, dbPath).start();

Example config.json:

{
    "slack_verification_token": "xxx",
    "slack_access_token": "xxx",
    "darksky_api": "xxx",
    "google_geocode_api": "xxx",
    "pager_duty_api": "xxx",
    "jira_api": {
    	"host": "xxx",
    	"username": "xxx",
    	"password": "xxx"
    },
    "modules_blacklist": [],
    "stock": "xxx",
    "port": 3000,
    "node_env": "production"
}

Modules

Look at the modules in the modules/default folder for examples. If there are core modules that you wouldn't like in slackcat you can add them to the "modules_blacklist".

For example: "modules_blacklist": ["poop"] in config.json file would exclude the ?poop command from slackcat.

Usage:

Assuming the above module was stored in the modules/users/helloworld path

?hellworld or ?hello_world2 would return Hello World!!!

Example Module:

Modules will have an index.js file nested in a folder in the modules directory. The folder name containing in the index.js file will be the default command name.

For example:

├── modules/
│    └── hrviolation/
|        └── index.js

Sending ?hrviolation will trigger the index.js in the hrviolation directory. You can add custom aliases for commands by overriding the aliases() method in BaseModule.

Example Module index.js file:

'use strict';

module.exports = class HelloWorld extends BaseModule {
  handle(data) {
    this.bot.postMessage(
      data.channel,
      'Hello World!!!',
      {
        icon_emoji: ':cat:'
      }
    );
  }

  help() {
    return "Usage: `?helloworld2` should print:\n'Hello World Number 2!!!'";
  }

  aliases() {
    return ['hello_world2']; // Now helloworld (folder name) or helloworld2 will trigger this module.
  }

  getType() {
    [return BaseModule.TYPES.MODULE];
  }
};
Type Desc Required Method
MODULE This is the default type. Use this when making a new cmd and call with ?<your_cmd_name> <args> handle(data)
OVERFLOW_CMD This module type is to handle commands that the router doesn't know how to handle. See handleOverflow(data) for an example. handleReaction(data)
REACTION This module is for handling when a user reacts to a post with an emoji. See the Reactions module for more info. handleReaction(data)
MEMBER_JOINED_CHANNEL This module is for handling when a user joins a channel handleMemberJoin(data)
RAW_INPUT This module is for handling any text being posted in the channel handleRawInput(data)
ENDPOINT This module handling server side createRoutes(data)
DIALOG This creates a dialog for a user to interact with createRoutes(app) onDialogSubmit(data)

Database

Slackcat has the ability to use many types of databases. By default it will use a light weight sqlite3 database however you can define your own:

"db": {
  "host": "postgres-db",
  "port": "5432",
  "dbName": "slackcat",
  "username": "**************",
  "password": "*************",
  "dialect": "postgres"
}

Dialogs

There is some more complexities with setting up dialogs in slack detailed here

Debugging

If you are testing you will often get rate limited. In order to test your cmd from the command line run

node index.js <cmd> <args>

eg: node index.js ?summon test

Will summon a google image matching test keyword.

If you want to test an endpoint module you can pass --with-server to spin up the express debug server.

Environments

NODE_ENV="production" node index.js will start the app in production mode which will use db.sqlite. Otherwise it will use db-dev.sqlite. This is useful in tests as not to corrupt production data.