From fbe7ab47210056c190df844d52ccbd3dae6d80e1 Mon Sep 17 00:00:00 2001 From: Braxen Date: Fri, 26 Jan 2024 19:06:16 +0100 Subject: [PATCH] update ntfy and telegram notifier options --- server/src/Core/ClientBroker.ts | 2 +- server/src/Core/Notifiers/Ntfy.ts | 51 +++++++++++++++++++++++---- server/src/Core/Notifiers/Telegram.ts | 50 +++++++++++++++++++++++--- 3 files changed, 91 insertions(+), 12 deletions(-) diff --git a/server/src/Core/ClientBroker.ts b/server/src/Core/ClientBroker.ts index 1003d5a3..3af0400c 100644 --- a/server/src/Core/ClientBroker.ts +++ b/server/src/Core/ClientBroker.ts @@ -266,7 +266,7 @@ export class ClientBroker { NotificationProvider.NTFY ) ) { - NtfyNotify(title, body, icon, category, url); + NtfyNotify({ title, body, icon, category, url }); } if ( diff --git a/server/src/Core/Notifiers/Ntfy.ts b/server/src/Core/Notifiers/Ntfy.ts index 4932754b..c1ee9f09 100644 --- a/server/src/Core/Notifiers/Ntfy.ts +++ b/server/src/Core/Notifiers/Ntfy.ts @@ -4,23 +4,62 @@ import chalk from "chalk"; import { Config } from "../Config"; import { LOGLEVEL, log } from "../Log"; -export default function notify( - title: string, +type Action = { + action: "view" | "http"; + label: string; + url: string; + clear?: boolean; + body?: string; +}; + +// action=, label=, paramN=... [; action=, label=, ...] +function buildActions(actions: Action[]) { + return actions + .map((action) => { + return `action=${action.action}, label=${action.label}, ${ + action.url ? `url=${action.url}, ` : "" + }${action.clear ? `clear=${action.clear}, ` : ""}${ + action.body ? `body='${action.body}', ` : "" + }`; + }) + .join("; "); +} + +export default function notify({ + title, body = "", icon = "", - category: NotificationCategory, // change this? + category, // change this? url = "", - emoji = "" -) { + emoji = "", + actions = [], +}: { + title: string; + body?: string; + icon?: string; + category: NotificationCategory; + url?: string; + emoji?: string; + actions?: Action[]; +}) { const ntfyUrl = Config.getInstance().cfg("notifications.ntfy.url"); + if (url) { + actions.push({ + action: "http", + label: "Open", + url: url, + }); + } + if (ntfyUrl) { axios .request({ url: ntfyUrl, headers: { Title: title, - Actions: url ? `view, Open, ${url}` : undefined, + // Actions: url ? `view, Open, ${url}` : undefined, + Actions: buildActions(actions), Icon: icon ?? undefined, Tags: emoji ? `${emoji},${category}` : category, }, diff --git a/server/src/Core/Notifiers/Telegram.ts b/server/src/Core/Notifiers/Telegram.ts index ce1e9fcd..b72c677e 100644 --- a/server/src/Core/Notifiers/Telegram.ts +++ b/server/src/Core/Notifiers/Telegram.ts @@ -4,16 +4,56 @@ import chalk from "chalk"; import { Config } from "../Config"; import { LOGLEVEL, log } from "../Log"; +interface TelegramSendMessagePayloadEntity { + type: + | "mention" + | "hashtag" + | "cashtag" + | "bot_command" + | "url" + | "email" + | "phone_number" + | "bold" + | "italic" + | "underline" + | "strikethrough" + | "code" + | "pre"; + offset: number; + length: number; + url?: string; + user?: unknown; + language?: string; + custom_emoji_id?: string; +} + interface TelegramSendMessagePayload { - chat_id: number; + chat_id: number | string; + message_thread_id?: number; text: string; parse_mode?: "MarkdownV2" | "Markdown" | "HTML"; - entities?: unknown; - disable_web_page_preview?: boolean; + entities?: TelegramSendMessagePayloadEntity[]; + link_preview_options?: { + is_disabled?: boolean; + url?: string; + prefer_small_media?: boolean; + prefer_large_media?: boolean; + show_above_text?: boolean; + }; + // disable_web_page_preview?: boolean; disable_notification?: boolean; protect_content?: boolean; - reply_to_message_id?: number; - allow_sending_without_reply?: boolean; + // reply_to_message_id?: number; + // allow_sending_without_reply?: boolean; + reply_parameters?: { + message_id: number; + chat_id?: number | string; + allow_sending_without_reply?: boolean; + quote?: string; + quote_parse_mode?: "MarkdownV2" | "Markdown" | "HTML"; + quote_entities?: unknown; + quote_position?: number; + }; reply_markup?: unknown; }