feature-more-notifications (#5)
Reviewed-on: https://git.t000-n.de/tbehrendt/ts3gotify/pulls/5
This commit is contained in:
@@ -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_URL | string | | YES | URL of the Gotify server, including http... |
|
||||||
| GOTIFY_TOKEN | string | | YES | App Token for this application on Gotify. |
|
| GOTIFY_TOKEN | string | | YES | App Token for this application on Gotify. |
|
||||||
| GOTIFY_TITLE | string | ts3gotify | NO | Title of Gotify notifications. |
|
| 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' |
|
||||||
|
|||||||
76
src/app.ts
76
src/app.ts
@@ -1,5 +1,5 @@
|
|||||||
import { Gotify } from "gotify";
|
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";
|
import { createLogger, transports, format } from "winston";
|
||||||
|
|
||||||
const logger = createLogger({
|
const logger = createLogger({
|
||||||
@@ -15,6 +15,46 @@ const gotify = new Gotify({
|
|||||||
server: process.env.GOTIFY_URL,
|
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({
|
TeamSpeak.connect({
|
||||||
host: process.env.TS3_HOST || "info",
|
host: process.env.TS3_HOST || "info",
|
||||||
queryport: process.env.TS3_QUERY_PORT || 10011,
|
queryport: process.env.TS3_QUERY_PORT || 10011,
|
||||||
@@ -24,19 +64,33 @@ TeamSpeak.connect({
|
|||||||
password: process.env.TS3_PASSWORD,
|
password: process.env.TS3_PASSWORD,
|
||||||
nickname: process.env.TS3_NICKNAME || "ts3gotify",
|
nickname: process.env.TS3_NICKNAME || "ts3gotify",
|
||||||
}).then((teamspeak) => {
|
}).then((teamspeak) => {
|
||||||
|
const mode = getModes()
|
||||||
|
|
||||||
logger.info("connected to TS3")
|
logger.info("connected to TS3")
|
||||||
|
|
||||||
teamspeak.on("clientconnect", (event) => {
|
if (mode.connect) {
|
||||||
logger.debug(`${event.client.nickname} connected`)
|
teamspeak.on("clientconnect", (event) => {
|
||||||
|
handleMessage(`${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.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 () => {
|
teamspeak.on("close", async () => {
|
||||||
logger.debug("disconnected, trying to reconnect...")
|
logger.debug("disconnected, trying to reconnect...")
|
||||||
|
|||||||
1
src/environment.d.ts
vendored
1
src/environment.d.ts
vendored
@@ -13,6 +13,7 @@ declare global {
|
|||||||
GOTIFY_URL: string;
|
GOTIFY_URL: string;
|
||||||
GOTIFY_TOKEN: string;
|
GOTIFY_TOKEN: string;
|
||||||
GOTIFY_TITLE?: string;
|
GOTIFY_TITLE?: string;
|
||||||
|
MODE?: Array<"connect" | "disconnect" | "moved" | "message">
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user