develop
HerrHase 2 years ago
parent 67219aa1f2
commit 043fba246a

@ -0,0 +1,11 @@
APP_DEBUG=false
APP_PORT=8510
APP_API_TOKEN=
APP_API_ALLOWED_PARSERS=kuma
XMPP_SERVICE=
XMPP_DOMAIN=
XMPP_USERNAME=
XMPP_PASSWORD=
XMPP_TO=

@ -0,0 +1,10 @@
{
"private": true,
"name": "tellme-bot",
"workspaces": [
"packages/*"
],
"scripts": {
"start": "yarn workspace server run start"
}
}

@ -0,0 +1,80 @@
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, xml } from '@xmpp/client'
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)
})
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') {
// Sends a chat message to itself
const message = xml(
'message',
{
type: 'chat',
to: env.XMPP_TO
},
xml('body', {}, data.message)
)
await xmpp.send(message)
}
})
/**
* add routes
*
*
*/
import webhookHttp from './http/api/webhook.js'
server
.register(webhookHttp, {
'prefix': '/api/webhook',
'eventEmitter': eventEmitter
})
export default server

@ -0,0 +1,53 @@
import DOMPurify from 'isomorphic-dompurify'
import { EventEmitter } from 'events'
/**
* handle auth
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.tentakelfabrik.de:tentakelfabrik/tellme-bot.git
*
*/
export default async function(fastify, opts)
{
/**
* auth
*
* @param {object} request
* @param {object} response
*
*/
fastify.post('/v1/:parser([a-zA-Z0-9]{0,255})/:token([a-zA-Z0-9])', async function (request, reply)
{
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) {
return reply
.code(404)
.send()
}
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', {
'message': result
})
reply
.code(200)
.send()
})
}

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

File diff suppressed because it is too large Load Diff

@ -0,0 +1,15 @@
{
"name": "server",
"version": "0.1.0",
"scripts": {
"start": "node index.js"
},
"type": "module",
"dependencies": {
"@xmpp/client": "^0.13.0",
"dotenv": "^10.0.0",
"fastify": "^3.24.0",
"fastify-formbody": "^5.2.0",
"isomorphic-dompurify": "^0.16.0"
}
}

@ -0,0 +1,16 @@
import Parser from './parser.js'
/**
*
*
*
*/
class Kuma extends Parser
{
parse()
{
this.message = this.body.msg
}
}
export default Kuma

@ -0,0 +1,34 @@
/**
*
*
*
*/
class Parser
{
/**
*
*
*/
constructor(body)
{
// body from webhook
this.body = body
// message
this.message = undefined
}
/**
* run parser, call parse-function and return message
*
* @return string
*
*/
run()
{
this.parse()
return this.message
}
}
export default Parser

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

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