diff --git a/README.md b/README.md index 42f1da0..38277d8 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # 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. +Simple Bot to receive Webhooks, parse and send them as Message to a XMPP-account. The idea result +from the missing support of [Kuma](https://github.com/louislam/uptime-kuma) for sending Notifications with +XMPP. ## Install @@ -34,9 +35,18 @@ XMPP_TO= \/api/v1/webhook/\/\ +## health + +\/api/v1/health + ## Parsers -There is only one Parser Class for Kuma, but it is possible to add Additional Classes. +Existing Parsers, + +* Kuma / Getting Notifications from Kuma +* Text / Getting "text" Parameter in JSON-Body + +## Custom Parsers 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 261f224..da3352e 100644 --- a/packages/server/bootstrap.js +++ b/packages/server/bootstrap.js @@ -1,6 +1,7 @@ import fastify from 'fastify' import dotenv from 'dotenv' import path from 'path' +import { EventEmitter } from 'events' // getting .env dotenv.config({ path: path.join(path.resolve(), '/../../.env') }) @@ -8,70 +9,33 @@ dotenv.config({ path: path.join(path.resolve(), '/../../.env') }) // create server const server = fastify() -/** - * xmpp - * - * import client for xmpp, adding events for handling - * - */ -import { client, xml } from '@xmpp/client' -import { EventEmitter } from 'events' - -// create eventemitter for sending messages +// adding eventEmitter server.decorate('eventEmitter', new EventEmitter()) -const xmpp = client({ +// adding xmpp +import xmpp from './plugins/xmpp.js' + +server.register(xmpp, { service: process.env.XMPP_SERVICE, domain: process.env.XMPP_DOMAIN, username: process.env.XMPP_USERNAME, password: process.env.XMPP_PASSWORD }) -xmpp.on('error', (error) => { - console.error(error) -}) - -xmpp.on('online', (address) => -{ - console.log('connected to ' + address) - - // add event if client going online - server.eventEmitter.on('send-message', async (data) => - { - // Sends a chat message to itself - const message = xml( - 'message', - { - type: 'chat', - to: process.env.XMPP_TO - }, - xml('body', {}, data.message) - ) - - await xmpp.send(message) - }) -}) - -xmpp.on('offline', (error) => { - console.log('offline') - - // remove event if client going offline - server.eventEmitter.off('send-message') -}) - -xmpp.start().catch(console.error) - - /** * add routes * * */ import webhookHttp from './http/api/webhook.js' +import healthHttp from './http/api/health.js' server .register(webhookHttp, { 'prefix': '/api/webhook' }) + .register(healthHttp, { + 'prefix': '/api/health' + }) export default server \ No newline at end of file diff --git a/packages/server/http/api/health.js b/packages/server/http/api/health.js new file mode 100644 index 0000000..6a83f7c --- /dev/null +++ b/packages/server/http/api/health.js @@ -0,0 +1,25 @@ +/** + * handle health + * + * @author Björn Hase + * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 + * @link https://gitea.node001.net/HerrHase/tellme-bot.git + * + */ + +export default async function(fastify, opts) +{ + /** + * getting post getting allowed parser class and send over xmpp + * + * @param {object} request + * @param {object} response + * + */ + fastify.get('/v1', async function (request, reply) + { + reply + .code(200) + .send() + }) +} \ No newline at end of file diff --git a/packages/server/http/api/webhook.js b/packages/server/http/api/webhook.js index 298255b..19dbe06 100644 --- a/packages/server/http/api/webhook.js +++ b/packages/server/http/api/webhook.js @@ -1,9 +1,9 @@ /** * handle webhook * - * @author Björn Hase, Tentakelfabrik + * @author Björn Hase * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 - * @link https://gitea.tentakelfabrik.de:tentakelfabrik/tellme-bot.git + * @link https://gitea.node001.net/HerrHase/tellme-bot.git * */ diff --git a/packages/server/package.json b/packages/server/package.json index cb49a73..ee193f7 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -1,4 +1,5 @@ { + "private": true, "name": "server", "version": "0.1.1", "scripts": { diff --git a/packages/server/parsers/kuma.js b/packages/server/parsers/kuma.js index da6a66e..1d77d9d 100644 --- a/packages/server/parsers/kuma.js +++ b/packages/server/parsers/kuma.js @@ -4,9 +4,9 @@ import DOMPurify from 'isomorphic-dompurify' /** * Parser for Kuma, getting only error message * - * @author Björn Hase, Tentakelfabrik + * @author Björn Hase * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 - * @link https://gitea.tentakelfabrik.de:tentakelfabrik/tellme-bot.git + * @link https://gitea.node001.net/HerrHase/tellme-bot.git * */ class Kuma extends Parser diff --git a/packages/server/parsers/parser.js b/packages/server/parsers/parser.js index 5c3d994..c8afe6e 100644 --- a/packages/server/parsers/parser.js +++ b/packages/server/parsers/parser.js @@ -2,9 +2,9 @@ * 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 + * @author Björn Hase * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 - * @link https://gitea.tentakelfabrik.de:tentakelfabrik/tellme-bot.git + * @link https://gitea.node001.net/HerrHase/tellme-bot.git * */ class Parser diff --git a/packages/server/parsers/text.js b/packages/server/parsers/text.js index 218ecb6..b54bf9f 100644 --- a/packages/server/parsers/text.js +++ b/packages/server/parsers/text.js @@ -4,9 +4,9 @@ import DOMPurify from 'isomorphic-dompurify' /** * Parser for "text" in Json, is used by slack * - * @author Björn Hase, Tentakelfabrik + * @author Björn Hase * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 - * @link https://gitea.tentakelfabrik.de:tentakelfabrik/tellme-bot.git + * @link https://gitea.node001.net/HerrHase/tellme-bot.git * */ class Text extends Parser diff --git a/packages/server/plugins/xmpp.js b/packages/server/plugins/xmpp.js new file mode 100644 index 0000000..fc3daf0 --- /dev/null +++ b/packages/server/plugins/xmpp.js @@ -0,0 +1,69 @@ +/** + * plugin for handle xmpp in tellme-bot + * + * create event send-message + * + * @author Björn Hase + * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 + * @link https://gitea.node001.net/HerrHase/tellme-bot.git + * + */ + +import { client, xml } from '@xmpp/client' + +export default function (fastify, options, done) { + + /** + * handler for send-message event + * + * @param {object} data + * + */ + async function handleSendMessage(data) { + + // Sends a chat message to itself + const message = xml( + 'message', + { + type: 'chat', + to: options.to + }, + xml('body', {}, data.message) + ) + + await xmpp.send(message) + } + + const xmpp = client({ + service: options.service, + domain: options.domain, + username: options.username, + password: options.password + }) + + // handle if client has errors + xmpp.on('error', (error) => { + console.error(error) + }) + + // handle if client goes online + xmpp.on('online', (address) => + { + console.log('connected to ' + address) + + // check for event and remove it + if (fastify.eventEmitter.listeners('send-message').length > 0) { + fastify.eventEmitter.off('send-message', handleSendMessage) + } + + // add event if client going online + fastify.eventEmitter.on('send-message', handleSendMessage) + }) + + // connect + xmpp + .start() + .catch(console.log) + + done() +} \ No newline at end of file diff --git a/packages/server/schemas/webhook.js b/packages/server/schemas/webhook.js deleted file mode 100644 index 7ca8a51..0000000 --- a/packages/server/schemas/webhook.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * - * - * - */ - -export default { - schema: { - body: { - username: { type: 'string' }, - password: { type: 'string' } - } - } -} \ No newline at end of file