From e3e699db4e31f5a7c80617a8f61488de8d0021b1 Mon Sep 17 00:00:00 2001 From: Timo Behrendt Date: Sun, 11 Dec 2022 21:16:34 +0000 Subject: [PATCH] feature-more-notifications (#5) Reviewed-on: https://git.t000-n.de/tbehrendt/ts3gotify/pulls/5 --- README.md | 3 +- src/app.ts | 76 +++++++++++++++++++++++++++++++++++++------- src/environment.d.ts | 1 + 3 files changed, 68 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index be69e82..2f1fd79 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,5 @@ Sends a message to Gotify when a client connects to the server. | TS3_NICKNAME | string | ts3gotify | NO | Nickname for this application connecting to the TS3 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. | \ No newline at end of file +| 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' | diff --git a/src/app.ts b/src/app.ts index f1c12e6..f985a2c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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...") diff --git a/src/environment.d.ts b/src/environment.d.ts index 8d6af4d..ecf206c 100644 --- a/src/environment.d.ts +++ b/src/environment.d.ts @@ -13,6 +13,7 @@ declare global { GOTIFY_URL: string; GOTIFY_TOKEN: string; GOTIFY_TITLE?: string; + MODE?: Array<"connect" | "disconnect" | "moved" | "message"> } } }