develop
HerrHase 3 years ago
parent 76f0166725
commit 0864d775c7

@ -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-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.
## Install ## Install
@ -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
* 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**.

@ -1,6 +1,7 @@
import fastify from 'fastify' import fastify from 'fastify'
import dotenv from 'dotenv' import dotenv from 'dotenv'
import path from 'path' import path from 'path'
import { EventEmitter } from 'events'
// getting .env // getting .env
dotenv.config({ path: path.join(path.resolve(), '/../../.env') }) dotenv.config({ path: path.join(path.resolve(), '/../../.env') })
@ -8,70 +9,33 @@ dotenv.config({ path: path.join(path.resolve(), '/../../.env') })
// create server // create server
const server = fastify() const server = fastify()
/** // adding eventEmitter
* 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()) server.decorate('eventEmitter', new EventEmitter())
const xmpp = client({ // adding xmpp
import xmpp from './plugins/xmpp.js'
server.register(xmpp, {
service: process.env.XMPP_SERVICE, service: process.env.XMPP_SERVICE,
domain: process.env.XMPP_DOMAIN, domain: process.env.XMPP_DOMAIN,
username: process.env.XMPP_USERNAME, username: process.env.XMPP_USERNAME,
password: process.env.XMPP_PASSWORD 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 * add routes
* *
* *
*/ */
import webhookHttp from './http/api/webhook.js' import webhookHttp from './http/api/webhook.js'
import healthHttp from './http/api/health.js'
server server
.register(webhookHttp, { .register(webhookHttp, {
'prefix': '/api/webhook' 'prefix': '/api/webhook'
}) })
.register(healthHttp, {
'prefix': '/api/health'
})
export default server export default server

@ -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()
})
}

@ -1,9 +1,9 @@
/** /**
* 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
* *
*/ */

@ -1,4 +1,5 @@
{ {
"private": true,
"name": "server", "name": "server",
"version": "0.1.1", "version": "0.1.1",
"scripts": { "scripts": {

@ -4,9 +4,9 @@ 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

@ -2,9 +2,9 @@
* 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

@ -4,9 +4,9 @@ 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

@ -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()
}

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