Simple Mattermost Bot 🤡ðŸ¤
Uses Webhooks
- Prerequisites: Node.js
- Install Waldorf
sudo npm install -g waldorf
- Create a directory for the Scripts e.g.
mkdir /opt/waldorf
-
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
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.
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)]);
});
- log
Log to stdout/stderr. Messages are prefixed with a timestamp and the calling scripts path.
- 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
- subscribeCallback :
function
Log to stdout/stderr. Messages are prefixed with a timestamp and the calling scripts path.
Kind: global class
Log a debug message
Kind: static method of log
Type |
---|
* |
Log an info message
Kind: static method of log
Type |
---|
* |
Log a warning message
Kind: static method of log
Type |
---|
* |
Log an error message
Kind: static method of log
Type |
---|
* |
Send Text to a Channel
Kind: global function
Param | Type |
---|---|
channel | string |
text | string |
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}`));
Remove a Subscription
Kind: global function
Param | Type |
---|---|
id | subscriptionId |
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);
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 |
MIT (c) Copyright Sebastian Raff