maintenance-bot/modules/notifiers/xmpp.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-05-27 14:13:46 +02:00
import settings from "../../libs/settings.js"
import { client, jid, xml } from "@xmpp/client"
import debug from "@xmpp/debug"
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
2022-05-27 14:48:43 +02:00
log("Sending messages to:", receivers)
2022-05-27 14:13:46 +02:00
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 => {
log(`Logged in as ${address.toString()}`)
})
await xmpp.start().catch(console.error)
return sendMessage
}