maintenance-bot/modules/notifiers/xmpp.js
2022-05-28 16:10:37 +02:00

46 lines
1.3 KiB
JavaScript

import settings from "../../libs/settings.js"
import { client, xml } from "@xmpp/client"
import getLogger from "../../libs/logger.js"
const log = getLogger("XMPP", settings.notifiers.xmpp.color)
export async function start() {
log("Starting up XMPP notifier...")
const xmpp = client({
service: settings.notifiers.xmpp.sender.host,
username: settings.notifiers.xmpp.sender.user,
password: settings.notifiers.xmpp.sender.pass
})
xmpp.on("error", err => console.error(err))
xmpp.on("offline", () => console.log("OFFLINE!"))
async function sendMessage(msg) {
const receivers = settings.notifiers.xmpp.receivers
log("Sending messages to:", receivers, "\nMessage:", msg)
const stanzas = receivers.map(
address => xml(
"message",
{ to: address, type: "chat" },
xml(
"body",
null,
msg
)
)
)
await xmpp.sendMany(stanzas).catch(console.error)
}
xmpp.on("online", address => {
xmpp.send(xml("presence", { type: "available" }))
log(`Logged in as ${address.toString()}`)
})
await xmpp.start().catch(console.error)
return sendMessage
}