From f04503e38e7cd29012e4c2ba814d4d41b8451464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LAGACHE?= Date: Mon, 2 Mar 2026 10:27:21 +0100 Subject: [PATCH] add: topic channel id support --- .../notifications/forms/telegram.form.tsx | 16 ++++ .../notifications/providers/telegram.ts | 89 +++++++++++-------- 2 files changed, 67 insertions(+), 38 deletions(-) diff --git a/src/components/wrappers/dashboard/admin/channels/channel/channel-form/providers/notifications/forms/telegram.form.tsx b/src/components/wrappers/dashboard/admin/channels/channel/channel-form/providers/notifications/forms/telegram.form.tsx index b13f5778..2468d3e4 100644 --- a/src/components/wrappers/dashboard/admin/channels/channel/channel-form/providers/notifications/forms/telegram.form.tsx +++ b/src/components/wrappers/dashboard/admin/channels/channel/channel-form/providers/notifications/forms/telegram.form.tsx @@ -43,6 +43,22 @@ export const NotifierTelegramForm = ({form}: NotifierTelegramFormProps) => { )} /> + ( + + Telegram Topic ID + + + +

+ Unique identifier for the target message thread (topic) of the forum; for forum supergroups only +

+ +
+ )} + /> ) } diff --git a/src/features/notifications/providers/telegram.ts b/src/features/notifications/providers/telegram.ts index 29fa758d..0b3d1573 100644 --- a/src/features/notifications/providers/telegram.ts +++ b/src/features/notifications/providers/telegram.ts @@ -1,49 +1,62 @@ -import type {EventPayload, DispatchResult} from '../types'; +import type { EventPayload, DispatchResult } from "../types"; export async function sendTelegram( - config: { telegramBotToken: string; telegramChatId: string }, - payload: EventPayload + config: { + telegramBotToken: string; + telegramChatId: string; + telegramTopicId?: string; + }, + payload: EventPayload, ): Promise { - const {telegramBotToken, telegramChatId} = config; + const { telegramBotToken, telegramChatId, telegramTopicId } = config; - // Helper to escape HTML characters - const escapeHtml = (unsafe: string) => { - return unsafe - .replace(/&/g, "&") - .replace(//g, ">") - .replace(/"/g, """) - .replace(/'/g, "'"); - }; + // Helper to escape HTML characters + const escapeHtml = (unsafe: string) => { + return unsafe + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); + }; - const title = escapeHtml(payload.title); - const message = escapeHtml(payload.message); - const level = escapeHtml(payload.level.toUpperCase()); - const dataString = payload.data ? escapeHtml(JSON.stringify(payload.data, null, 2).substring(0, 1000)) : ''; + const title = escapeHtml(payload.title); + const message = escapeHtml(payload.message); + const level = escapeHtml(payload.level.toUpperCase()); + const dataString = payload.data + ? escapeHtml(JSON.stringify(payload.data, null, 2).substring(0, 1000)) + : ""; - const text = `${title}\n\n${message}\n\nLevel: ${level}${payload.data ? `\n\nData:\n
${dataString}
` : ''}`; + const text = `${title}\n\n${message}\n\nLevel: ${level}${payload.data ? `\n\nData:\n
${dataString}
` : ""}`; - const body = { - chat_id: telegramChatId, - text, - parse_mode: 'HTML', - }; + const body: Record = { + chat_id: telegramChatId, + text, + parse_mode: "HTML", + }; - const res = await fetch(`https://api.telegram.org/bot${telegramBotToken}/sendMessage`, { - method: 'POST', - body: JSON.stringify(body), - headers: {'Content-Type': 'application/json'}, - }); + if (telegramTopicId) { + body.message_thread_id = Number(telegramTopicId); + } - if (!res.ok) { - const err = await res.text(); - throw new Error(`Telegram error: ${res.status} ${err}`); - } + const res = await fetch( + `https://api.telegram.org/bot${telegramBotToken}/sendMessage`, + { + method: "POST", + body: JSON.stringify(body), + headers: { "Content-Type": "application/json" }, + }, + ); - return { - success: true, - provider: 'telegram', - message: 'Sent to Telegram', - response: await res.text(), - }; + if (!res.ok) { + const err = await res.text(); + throw new Error(`Telegram error: ${res.status} ${err}`); + } + + return { + success: true, + provider: "telegram", + message: "Sent to Telegram", + response: await res.text(), + }; }