feature-more-notifications (#5)

Reviewed-on: https://git.t000-n.de/tbehrendt/ts3gotify/pulls/5
This commit is contained in:
2022-12-11 21:16:34 +00:00
parent e07373667f
commit e3e699db4e
3 changed files with 68 additions and 12 deletions

View File

@@ -15,3 +15,4 @@ Sends a message to Gotify when a client connects to the server.
| GOTIFY_URL | string | | YES | URL of the Gotify server, including http... |
| GOTIFY_TOKEN | string | | YES | App Token for this application on Gotify. |
| GOTIFY_TITLE | string | ts3gotify | NO | Title of Gotify notifications. |
| MODE | array | ['connect'] | NO | Types of notifications ts3gotify sends as JSON array. Available modes: 'connect', 'disconnect', 'moved', 'message' |

View File

@@ -1,5 +1,5 @@
import { Gotify } from "gotify";
import { QueryProtocol, TeamSpeak } from "ts3-nodejs-library"
import { QueryProtocol, TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library"
import { createLogger, transports, format } from "winston";
const logger = createLogger({
@@ -15,6 +15,46 @@ const gotify = new Gotify({
server: process.env.GOTIFY_URL,
})
const gotifyConfig = {
app: process.env.GOTIFY_TOKEN,
title: process.env.GOTIFY_TITLE || "ts3gotify"
}
function getModes() {
const modeIsProvided = process.env.MODE != undefined
return {
connect: modeIsProvided ? process.env.MODE?.includes("connect") || false : true,
disconnect: process.env.MODE?.includes("disconnect") || false,
moved: process.env.MODE?.includes("moved") || false,
message: process.env.MODE?.includes("message") || false
}
}
function sendNotification(message: string) {
gotify.send({
...gotifyConfig,
message: message,
}).catch((error: Error) => {
logger.error(`Error sending message to gotify: ${error.message}`)
})
}
function resolveMessageTarget(target: TextMessageTargetMode): string {
if (target === 1) {
return "Client"
} else if (target === 2) {
return "Channel"
} else {
return "Server"
}
}
function handleMessage(message: string) {
logger.debug(message)
sendNotification(message)
}
TeamSpeak.connect({
host: process.env.TS3_HOST || "info",
queryport: process.env.TS3_QUERY_PORT || 10011,
@@ -24,19 +64,33 @@ TeamSpeak.connect({
password: process.env.TS3_PASSWORD,
nickname: process.env.TS3_NICKNAME || "ts3gotify",
}).then((teamspeak) => {
const mode = getModes()
logger.info("connected to TS3")
teamspeak.on("clientconnect", (event) => {
logger.debug(`${event.client.nickname} connected`)
gotify.send({
app: process.env.GOTIFY_TOKEN,
title: process.env.GOTIFY_TITLE || "ts3gotify",
message: `${event.client.nickname} connected`,
}).catch((error: Error) => {
logger.error(`Error sending message to gotify: ${error.message}`)
if (mode.connect) {
teamspeak.on("clientconnect", (event) => {
handleMessage(`${event.client.nickname} connected`)
})
})
}
if (mode.disconnect) {
teamspeak.on("clientdisconnect", (event) => {
handleMessage(`${event.client?.nickname} disconnected`)
})
}
if (mode.message) {
teamspeak.on("textmessage", (event) => {
handleMessage(`${event.invoker.nickname} wrote ${event.msg} to a ${resolveMessageTarget(event.targetmode)}`)
})
}
if (mode.moved) {
teamspeak.on("clientmoved", (event) => {
handleMessage(`${event.client.nickname} got moved to ${event.channel.name}`)
})
}
teamspeak.on("close", async () => {
logger.debug("disconnected, trying to reconnect...")

View File

@@ -13,6 +13,7 @@ declare global {
GOTIFY_URL: string;
GOTIFY_TOKEN: string;
GOTIFY_TITLE?: string;
MODE?: Array<"connect" | "disconnect" | "moved" | "message">
}
}
}