Compare commits

...

6 Commits
0.1.1 ... main

@ -1,7 +1,8 @@
# tellme-bot # tellme-bot
Small bot to handle Webhooks, Parse and Send them with Xmpp. This is a Prototype and only exists Simple Bot to receive Webhooks, parse and send them as Message to a [XMPP](https://xmpp.org/)-account. The idea result
because [Kuma](https://github.com/louislam/uptime-kuma) offers no Notifications for Xmpp. from the missing support of [Kuma](https://github.com/louislam/uptime-kuma) for sending Notifications with
[XMPP](https://xmpp.org/).
## Install ## Install
@ -11,7 +12,7 @@ yarn start
or or
pm2 start yarn start --name=tellme-bot pm2 --name=tellme-bot start npm -- start
``` ```
## Konfiguration ## Konfiguration
@ -34,9 +35,18 @@ XMPP_TO=
\<domain\>/api/v1/webhook/\<parser\>/\<api-token\> \<domain\>/api/v1/webhook/\<parser\>/\<api-token\>
## health
\<domain\>/api/v1/health
## Parsers ## Parsers
There is only one Parser Class for Kuma, but it is possible to add Additional Classes. Existing Parsers,
* Kuma / Getting Notifications from [Kuma](https://github.com/louislam/uptime-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 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**. **APP_API_ALLOWED_PARSERS**.

6272
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,88 @@
import fastify from 'fastify'
import dotenv from 'dotenv'
import path from 'path'
import { EventEmitter } from 'events'
import formBody from '@fastify/formbody'
// getting .env
dotenv.config({ path: path.join(path.resolve(), '/../../.env') })
// create server
const server = fastify()
// adding eventEmitter
server.decorate('eventEmitter', new EventEmitter())
// adding formBody
server.register(formBody)
/**
* add 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,
to: process.env.XMPP_TO
})
/**
* add helmet
*
*/
import helmet from '@fastify/helmet'
/***
server.register(
helmet, {
referrerPolicy: {
policy: ['origin']
}
}
)*/
/**
* add rateLimit
*
*/
import rateLimit from '@fastify/rate-limit'
const rateLimitSettings = {
max: 100,
timeWindow: 60000
}
if (process.env.APP_RATE_LIMIT_MAX) {
rateLimitSettings.max = process.env.APP_RATE_LIMIT_MAX
}
if (process.env.APP_RATE_LIMIT_TIMEWINDOW) {
rateLimitSettings.timeWindow = process.env.APP_RATE_LIMIT_TIMEWINDOW
}
server.register(rateLimit, rateLimitSettings)
/**
* 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

@ -1,77 +0,0 @@
import fastify from 'fastify'
import dotenv from 'dotenv'
import path from 'path'
// getting .env
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
server.decorate('eventEmitter', new EventEmitter())
const xmpp = client({
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'
server
.register(webhookHttp, {
'prefix': '/api/webhook'
})
export default server

@ -0,0 +1,40 @@
import DOMPurify from 'isomorphic-dompurify'
/**
*
* handle parser
*
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/HerrHase/super-fastify-directus.git
*
*/
async function parserHandler(request, response) {
// getting allowed parsers from .env as array
const allowedParsers = process.env.APP_API_ALLOWED_PARSERS.split(',')
// getting name from request
const parserName = DOMPurify.sanitize(request.params.parser)
// if parser not found send 404
if (allowedParsers.indexOf(parserName) === -1) {
console.log('Parsers: "' + parserName + '" not found!')
return response
.code(404)
.send()
}
// getting parser and set body to parser
const Parser = await import('./../parsers/' + parserName + '.js')
response.locals = {
parser: new Parser.default(request.body)
}
}
export default parserHandler

@ -0,0 +1,23 @@
import DOMPurify from 'isomorphic-dompurify'
/**
* handle token
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/HerrHase/super-fastify-directus.git
*
*/
async function tokenHandler(request, response) {
const token = DOMPurify.sanitize(request.params.token)
if (token !== process.env.APP_API_TOKEN) {
return response
.code(401)
.send()
}
}
export default tokenHandler

@ -0,0 +1,24 @@
/**
* 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, response) {
response
.code(200)
.send()
})
}

@ -1,14 +1,20 @@
import tokenHandler from './../../handlers/token.js'
import parserHandler from './../../handlers/parser.js'
/** /**
* handle webhook * handle webhook
* *
* @author Björn Hase, Tentakelfabrik * @author Björn Hase
* @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 * @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
* *
*/ */
export default async function(fastify, opts) export default async function(fastify, options) {
{
fastify.addHook('preHandler', tokenHandler)
fastify.addHook('preHandler', parserHandler)
/** /**
* getting post getting allowed parser class and send over xmpp * getting post getting allowed parser class and send over xmpp
* *
@ -16,36 +22,19 @@ export default async function(fastify, opts)
* @param {object} response * @param {object} response
* *
*/ */
fastify.post('/v1/:parser([a-zA-Z0-9]{0,255})/:token([a-zA-Z0-9])', async function (request, reply) fastify.post('/v1/:parser/:token', async function (request, response) {
{
if (request.params.token !== process.env.APP_API_TOKEN) {
return reply
.code(401)
.send()
}
// getting allowed parsers from .env as array
const allowedParsers = process.env.APP_API_ALLOWED_PARSERS.split(',')
if (allowedParsers.indexOf(request.params.parser) === -1) { // getting parser from preHandler: parserHandler
return reply const result = response.locals.parser.run()
.code(404)
.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 // send event for send xmpp
fastify.eventEmitter.emit('send-message', { fastify.eventEmitter.emit('send-message', {
'message': result 'message': result
}) })
reply response
.code(200) .code(200)
.send() .send()
}) })
}
}

@ -1,14 +1,18 @@
import server from './bootstrap.js' import server from './_bootstrap.js'
// let it rain // let it rain
const start = async () => { const start = async () => {
try { try {
await server.listen(process.env.APP_PORT) await server.listen({
console.log('Server is running on port ' + process.env.APP_PORT) port: process.env.APP_PORT
})
console.log('Server: start')
console.log('Server: running on port ' + process.env.APP_PORT)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
process.exit(1) process.exit(1)
} }
} }
start() start()

File diff suppressed because it is too large Load Diff

@ -1,15 +1,19 @@
{ {
"private": true,
"name": "server", "name": "server",
"version": "0.1.1", "version": "0.1.0",
"scripts": { "scripts": {
"start": "node index.js" "start": "node index.js"
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@xmpp/client": "^0.13.0", "@fastify/formbody": "^7.4.0",
"@fastify/helmet": "^11.0.0",
"@fastify/rate-limit": "^8.0.1",
"@xmpp/client": "^0.13.1",
"dotenv": "^10.0.0", "dotenv": "^10.0.0",
"fastify": "^3.24.0", "fastify": "^4.19.2",
"fastify-formbody": "^5.2.0", "fastify-plugin": "^4.5.0",
"isomorphic-dompurify": "^0.16.0" "isomorphic-dompurify": "^1.7.0"
} }
} }

@ -2,19 +2,19 @@
* Basic Class for Parsering Body from Webhook, * Basic Class for Parsering Body from Webhook,
* extends Class need to write a parse function and add result to message variable * 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 * @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 class Parser {
{
/** /**
* *
* *
*/ */
constructor(body) constructor(body) {
{
// body from webhook // body from webhook
this.body = body this.body = body
@ -28,11 +28,10 @@ class Parser
* @return string * @return string
* *
*/ */
run() run() {
{
this.parse() this.parse()
return this.message return this.message
} }
} }
export default Parser export default Parser

@ -1,18 +1,22 @@
import Parser from './parser.js' import Parser from './_parser.js'
import DOMPurify from 'isomorphic-dompurify' import DOMPurify from 'isomorphic-dompurify'
/** /**
* Parser for Kuma, getting only error message * 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 * @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 class Kuma extends Parser {
{
parse() /**
{ *
*
*/
parse() {
// check for msg and clean it // check for msg and clean it
if (this.body.msg) { if (this.body.msg) {
this.message = DOMPurify.sanitize(this.body.msg) this.message = DOMPurify.sanitize(this.body.msg)
@ -20,4 +24,4 @@ class Kuma extends Parser
} }
} }
export default Kuma export default Kuma

@ -1,18 +1,22 @@
import Parser from './parser.js' import Parser from './_parser.js'
import DOMPurify from 'isomorphic-dompurify' import DOMPurify from 'isomorphic-dompurify'
/** /**
* Parser for "text" in Json, is used by slack * 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 * @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 class Text extends Parser {
{
parse() /**
{ *
*
*/
parse() {
// check for msg and clean it // check for msg and clean it
if (this.body.text) { if (this.body.text) {
this.message = DOMPurify.sanitize(this.body.text) this.message = DOMPurify.sanitize(this.body.text)
@ -20,4 +24,4 @@ class Text extends Parser
} }
} }
export default Text export default Text

@ -0,0 +1,73 @@
/**
* 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) {
let send = false
// Sends a chat message to itself
const message = xml(
'message', {
type: 'chat',
to: options.to
},
xml('body', {}, data.message)
)
try {
await xmpp.send(message)
} catch (error) {
console.log(error)
}
}
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('Xmpp: 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()
}

@ -1,14 +0,0 @@
/**
*
*
*
*/
export default {
schema: {
body: {
username: { type: 'string' },
password: { type: 'string' }
}
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save