1.0.2 • Published 6 years ago

waldorf v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Waldorf

License NPM version Dependency Status devDependency Status

Simple Mattermost Bot 🤡🤠

Uses Webhooks

Installation

  • Prerequisites: Node.js
  • Install Waldorf sudo npm install -g waldorf
  • Create a directory for the Scripts e.g. mkdir /opt/waldorf

Mattermost Setup

  • In the System Console - Integrations - Custom Integrations:
    • Enable Incoming Webhooks
    • Enable Outgoing Webhooks
    • Enable Integrations to Override Usernames

  • Create the Webhooks in the Team Settings - Integrations
    • an Incoming Webhook for every Channel where Waldorf should be able to say something
    • an Outgoing Webhook, you don't need to select a Channel here - then Waldorf will be able to subscribe to messages in every Channel. Define desired Trigger Words, e.g. "@waldorf". As Callback URL you need to supply the IP Address and the Port where Waldorf listens, if Waldorf runs on the same server as Mattermost you can use e.g. http://127.0.0.1:31337

Start Waldorf

See waldorf --help for available options.

Example Start command:

waldorf -u http://127.0.0.1:8065/hooks/ \
    -n waldorf \
    -s /opt/waldorf \
    -t s1zz8e1wxzgwjfmsnz3c43dnpa \
    -c ij6osdf3ofnidp199ronuinwne:town-square \
    -c hiirtud1spfwmfegd3pejamzsr:another-channel 

The -t option supplies the Secret Mattermost generated for the Outgoing Webhook, the -c options define Channels and the Secrets of the Incoming Webhooks.

I suggest to use PM2 to start Waldorf.

Scripts

Just place Javascript Files in the /opt/waldorf folder and mind that you have to restart Waldorf when you change or add Scripts there.

Example Scripts:

// Stupid :)
schedule('37 13 * * *', () => pub('town-square', '1337 time!!1! 🤓'));
// Respond "Hi @user" when someone says "Hello" or "hallo" ...
sub(/[Hh][ea]llo/, (match, user, channel) => pub(channel, `Hi @${user}`));
// simple quote script
const fs = require('fs');
const file = '/opt/waldorf/quotes.json';

let quotes = [];

if (fs.existsSync(file)) quotes = JSON.parse(fs.readFileSync(file));

sub(/\!addquote (.*)/, (match, user, channel) => {
    quotes.push(match[1]);
    fs.writeFileSync(file, JSON.stringify(quotes));
});

sub(/\!randomquote/, (text, user, channel) => {
    pub(channel, '> ' + quotes[Math.floor(Math.random() * quotes.length)]);
});

Script API

Classes

Functions

Typedefs

log

Log to stdout/stderr. Messages are prefixed with a timestamp and the calling scripts path.

Kind: global class

log.debug()

Log a debug message

Kind: static method of log

Type
*

log.info()

Log an info message

Kind: static method of log

Type
*

log.warn()

Log a warning message

Kind: static method of log

Type
*

log.error()

Log an error message

Kind: static method of log

Type
*

pub(channel, text)

Send Text to a Channel

Kind: global function

ParamType
channelstring
textstring

sub(pattern, callback) ⇒ subscriptionId

Add a Subscription that calls a Callback when pattern matches text said in a Channel

Kind: global function

ParamType
patternstring | RegExp
callbacksubscribeCallback

Example

// Respond "Hi @User" when someone says "Hello" or "hello"
sub(/[Hh]ello/, (match, user, channel) => pub(`Hi @${user}`));

unsub(id)

Remove a Subscription

Kind: global function

ParamType
idsubscriptionId

schedule(pattern, options, callback)

Schedule recurring and one-shot events

Kind: global function

ParamTypeDescription
patternstring | Date | Object | Array.<mixed>pattern or array of patterns. May be cron style string, Date object or node-schedule object literal. See https://github.com/tejasmanohar/node-schedule/wiki
optionsObject
options.randomnumberrandom delay execution in seconds. Has to be positive
callbackfunctionis called with no arguments

Example

// every full Hour.
schedule('0 * * * *', callback);

// Monday till friday, random between 7:30am an 8:00am
schedule('30 7 * * 1-5', {random: 30 * 60}, callback);

// once on 21. December 2018 at 5:30am
schedule(new Date(2018, 12, 21, 5, 30, 0), callback);

// every Sunday at 2:30pm
schedule({hour: 14, minute: 30, dayOfWeek: 0}, callback);

subscribeCallback : function

Kind: global typedef

ParamTypeDescription
textstring | Array.<string>text or .match(RegExp) array
userstringthe Name of the User that said something
channelstringthe Channel where something was said

License

MIT (c) Copyright Sebastian Raff