change eventemitter, add readme

develop
HerrHase 2 years ago
parent 64dfe666d2
commit 9068f7edd1

@ -1,2 +1,43 @@
# tellme-bot # 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
<your-domain>/api/v1/webhook/<parser>/<api-token>
## 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.

@ -13,6 +13,12 @@ const server = fastify()
* *
*/ */
import { client, xml } from '@xmpp/client' 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({ const xmpp = client({
service: process.env.XMPP_SERVICE, service: process.env.XMPP_SERVICE,
@ -25,31 +31,12 @@ xmpp.on('error', (error) => {
console.error(error) console.error(error)
}) })
xmpp.on('online', (address) => { 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) =>
{ {
if (xmpp.status === 'online') { console.log('connected to ' + address)
eventEmitter.on('send-message', async (data) =>
{
// Sends a chat message to itself // Sends a chat message to itself
const message = xml( const message = xml(
'message', 'message',
@ -61,9 +48,15 @@ eventEmitter.on('send-xmpp', async (data) =>
) )
await xmpp.send(message) await xmpp.send(message)
} })
}) })
xmpp.on('offline', (error) => {
console.log('offline')
})
xmpp.start().catch(console.error)
/** /**
* add routes * add routes
* *
@ -74,7 +67,7 @@ import webhookHttp from './http/api/webhook.js'
server server
.register(webhookHttp, { .register(webhookHttp, {
'prefix': '/api/webhook', 'prefix': '/api/webhook',
'eventEmitter': eventEmitter 'eventEmitter': eventEmitter // @TODO shift to a more fastify-way
}) })
export default server export default server

@ -1,11 +1,8 @@
import DOMPurify from 'isomorphic-dompurify'
import { EventEmitter } from 'events'
/** /**
* handle auth * handle webhook
* *
* @author Björn Hase, Tentakelfabrik * @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 * @link https://gitea.tentakelfabrik.de:tentakelfabrik/tellme-bot.git
* *
*/ */
@ -13,7 +10,7 @@ import { EventEmitter } from 'events'
export default async function(fastify, opts) export default async function(fastify, opts)
{ {
/** /**
* auth * getting post getting allowed parser class and send over xmpp
* *
* @param {object} request * @param {object} request
* @param {object} response * @param {object} response
@ -36,13 +33,14 @@ export default async function(fastify, opts)
.send() .send()
} }
// getting parser and set body to parser
const Parser = await import('./../../parsers/' + request.params.parser + '.js') const Parser = await import('./../../parsers/' + request.params.parser + '.js')
const parser = new Parser.default(request.body) const parser = new Parser.default(request.body)
const result = parser.run() const result = parser.run()
// send event for send xmpp // send event for send xmpp
opts.eventEmitter.emit('send-xmpp', { opts.eventEmitter.emit('send-message', {
'message': result 'message': result
}) })

@ -1,15 +1,22 @@
import Parser from './parser.js' 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 class Kuma extends Parser
{ {
parse() parse()
{ {
this.message = this.body.msg // check for msg and clean it
if (this.body.msg) {
this.message = DOMPurify.sanitize(this.body.msg)
}
} }
} }

@ -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 class Parser

Loading…
Cancel
Save