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

71 lines
1.3 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
server.decorate('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
server.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'
3 years ago
})
export default server