You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tellme-bot/packages/server/bootstrap.js

73 lines
1.5 KiB

3 years ago
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'
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()
3 years ago
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) =>
3 years ago
{
console.log('connected to ' + address)
3 years ago
eventEmitter.on('send-message', async (data) =>
{
3 years ago
// Sends a chat message to itself
const message = xml(
'message',
{
type: 'chat',
3 years ago
to: process.env.XMPP_TO
3 years ago
},
xml('body', {}, data.message)
)
await xmpp.send(message)
})
3 years ago
})
xmpp.on('offline', (error) => {
console.log('offline')
})
xmpp.start().catch(console.error)
3 years ago
/**
* add routes
*
*
*/
import webhookHttp from './http/api/webhook.js'
server
.register(webhookHttp, {
'prefix': '/api/webhook',
'eventEmitter': eventEmitter // @TODO shift to a more fastify-way
3 years ago
})
export default server