Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hobbyquaker/waldorf
Simple Mattermost Bot 🤡ðŸ¤
https://github.com/hobbyquaker/waldorf
bot chat javascript mattermost nodejs
Last synced: 3 months ago
JSON representation
Simple Mattermost Bot 🤡ðŸ¤
- Host: GitHub
- URL: https://github.com/hobbyquaker/waldorf
- Owner: hobbyquaker
- Created: 2017-03-22T16:54:28.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-12T19:20:10.000Z (over 6 years ago)
- Last Synced: 2024-08-03T04:25:33.295Z (3 months ago)
- Topics: bot, chat, javascript, mattermost, nodejs
- Language: JavaScript
- Homepage:
- Size: 11.7 KB
- Stars: 14
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Waldorf
[mit-badge]: https://img.shields.io/badge/License-MIT-blue.svg?style=flat
[mit-url]: LICENSE
[![License][mit-badge]][mit-url]
[![NPM version](https://badge.fury.io/js/waldorf.svg)](http://badge.fury.io/js/waldorf)
[![Dependency Status](https://david-dm.org/hobbyquaker/waldorf/status.svg)](https://david-dm.org/hobbyquaker/waldorf)
[![devDependency Status](https://david-dm.org/hobbyquaker/waldorf/dev-status.svg)](https://david-dm.org/hobbyquaker/waldorf?type=dev)> Simple Mattermost Bot 🤡ðŸ¤
Uses Webhooks
# Installation
* Prerequisites: [Node.js](https://nodejs.org)
* 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 WaldorfSee `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](http://pm2.keymetrics.io/) 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:
```javascript
// Stupid :)
schedule('37 13 * * *', () => pub('town-square', '1337 time!!1! 🤓'));
``````javascript
// Respond "Hi @user" when someone says "Hello" or "hallo" ...
sub(/[Hh][ea]llo/, (match, user, channel) => pub(channel, `Hi @${user}`));
``````javascript
// 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](#log)
* [.debug()](#log.debug)
* [.info()](#log.info)
* [.warn()](#log.warn)
* [.error()](#log.error)
### log.debug()
Log a debug message
**Kind**: static method of [log](#log)
| Type |
| --- |
| \*
|
### log.info()
Log an info message
**Kind**: static method of [log](#log)
| Type |
| --- |
| \*
|
### log.warn()
Log a warning message
**Kind**: static method of [log](#log)
| Type |
| --- |
| \*
|
### log.error()
Log an error message
**Kind**: static method of [log](#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](#subscribeCallback)
|
**Example**
```js
// 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](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**
```js
// 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