diff --git a/README.md b/README.md index 64901da..28dbf9f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,43 @@ # tellme-bot +Small bot to handle Webhooks, Parse and Send them with Xmpp. This is a Prototype and only exists +because (Kuma)[https://github.com/louislam/uptime-kuma] offers no Notifications for Xmpp. + +## Install + +``` +yarn install + +yarn start + +or + +pm2 start yarn start --name=tellme-bot +``` + +## Konfiguration + +``` +APP_DEBUG=false +APP_PORT= + +APP_API_TOKEN= +APP_API_ALLOWED_PARSERS=kuma + +XMPP_SERVICE= +XMPP_DOMAIN= +XMPP_USERNAME= +XMPP_PASSWORD= +XMPP_TO= +``` + +## Webhook + +/api/v1/webhook// + +## Parsers + +There is only one Parser Class for Kuma, but it is possible to add Additional Classes. + +Create a new Class, extends is with parser.js, add a parser-function and add the name of the file in the .env to +APP_API_ALLOWED_PARSERS. diff --git a/packages/server/bootstrap.js b/packages/server/bootstrap.js index 8e0100d..5e66333 100644 --- a/packages/server/bootstrap.js +++ b/packages/server/bootstrap.js @@ -13,6 +13,12 @@ const server = fastify() * */ import { client, xml } from '@xmpp/client' +import { EventEmitter } from 'events' + + +// create eventemitter for sending messages +// @TODO find a better solution, was only to use it with online event, but not working as expected +const eventEmitter = new EventEmitter() const xmpp = client({ service: process.env.XMPP_SERVICE, @@ -25,31 +31,12 @@ xmpp.on('error', (error) => { console.error(error) }) -xmpp.on('online', (address) => { - console.log('connected to ' + address) -}) - -xmpp.on('offline', (error) => { - console.log('offline') -}) - -xmpp.start().catch(console.error) - -/** - * add eventemitter - * - * @TODO find a better solution, was only to use it with online event, but not working as expected - * - */ - -import { EventEmitter } from 'events' - -const eventEmitter = new EventEmitter() - -eventEmitter.on('send-xmpp', async (data) => +xmpp.on('online', (address) => { - if (xmpp.status === 'online') { + console.log('connected to ' + address) + eventEmitter.on('send-message', async (data) => + { // Sends a chat message to itself const message = xml( 'message', @@ -61,9 +48,15 @@ eventEmitter.on('send-xmpp', async (data) => ) await xmpp.send(message) - } + }) }) +xmpp.on('offline', (error) => { + console.log('offline') +}) + +xmpp.start().catch(console.error) + /** * add routes * @@ -74,7 +67,7 @@ import webhookHttp from './http/api/webhook.js' server .register(webhookHttp, { 'prefix': '/api/webhook', - 'eventEmitter': eventEmitter + 'eventEmitter': eventEmitter // @TODO shift to a more fastify-way }) export default server \ No newline at end of file diff --git a/packages/server/http/api/webhook.js b/packages/server/http/api/webhook.js index c74db39..e17f5a3 100644 --- a/packages/server/http/api/webhook.js +++ b/packages/server/http/api/webhook.js @@ -1,11 +1,8 @@ -import DOMPurify from 'isomorphic-dompurify' -import { EventEmitter } from 'events' - /** - * handle auth + * handle webhook * * @author Björn Hase, Tentakelfabrik - * @license http://opensource.org/licenses/MIT The MIT License + * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 * @link https://gitea.tentakelfabrik.de:tentakelfabrik/tellme-bot.git * */ @@ -13,7 +10,7 @@ import { EventEmitter } from 'events' export default async function(fastify, opts) { /** - * auth + * getting post getting allowed parser class and send over xmpp * * @param {object} request * @param {object} response @@ -36,13 +33,14 @@ export default async function(fastify, opts) .send() } + // getting parser and set body to parser const Parser = await import('./../../parsers/' + request.params.parser + '.js') const parser = new Parser.default(request.body) const result = parser.run() // send event for send xmpp - opts.eventEmitter.emit('send-xmpp', { + opts.eventEmitter.emit('send-message', { 'message': result }) diff --git a/packages/server/parsers/kuma.js b/packages/server/parsers/kuma.js index 9ecdf51..da6a66e 100644 --- a/packages/server/parsers/kuma.js +++ b/packages/server/parsers/kuma.js @@ -1,15 +1,22 @@ import Parser from './parser.js' +import DOMPurify from 'isomorphic-dompurify' /** + * Parser for Kuma, getting only error message * - * + * @author Björn Hase, Tentakelfabrik + * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 + * @link https://gitea.tentakelfabrik.de:tentakelfabrik/tellme-bot.git * */ class Kuma extends Parser { parse() { - this.message = this.body.msg + // check for msg and clean it + if (this.body.msg) { + this.message = DOMPurify.sanitize(this.body.msg) + } } } diff --git a/packages/server/parsers/parser.js b/packages/server/parsers/parser.js index 21042e7..5c3d994 100644 --- a/packages/server/parsers/parser.js +++ b/packages/server/parsers/parser.js @@ -1,6 +1,10 @@ /** + * Basic Class for Parsering Body from Webhook, + * extends Class need to write a parse function and add result to message variable * - * + * @author Björn Hase, Tentakelfabrik + * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 + * @link https://gitea.tentakelfabrik.de:tentakelfabrik/tellme-bot.git * */ class Parser