Nybble Processing Mainframe

    waldorf

    1.0.2 • Public • Published

    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

    log

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

    Functions

    pub(channel, text)

    Send Text to a Channel

    sub(pattern, callback)subscriptionId

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

    unsub(id)

    Remove a Subscription

    schedule(pattern, [options], callback)

    Schedule recurring and one-shot events

    Typedefs

    subscribeCallback : function

    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

    Param Type
    channel string
    text string

    sub(pattern, callback) ⇒ subscriptionId

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

    Kind: global function

    Param Type
    pattern string | RegExp
    callback subscribeCallback

    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

    Param Type
    id subscriptionId

    schedule(pattern, [options], callback)

    Schedule recurring and one-shot events

    Kind: global function

    Param Type Description
    pattern string | 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
    [options] Object
    [options.random] number random delay execution in seconds. Has to be positive
    callback function is 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

    Param Type Description
    text string | Array.<string> text or .match(RegExp) array
    user string the Name of the User that said something
    channel string the Channel where something was said

    License

    MIT (c) Copyright Sebastian Raff

    Install

    npm i waldorf

    DownloadsWeekly Downloads

    1

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    28.6 kB

    Total Files

    9

    Last publish

    Collaborators

    • hobbyquaker