From 445e634aa102b2507152bb434725ab907e20f4e3 Mon Sep 17 00:00:00 2001 From: discountry Date: Mon, 12 Jan 2026 12:39:37 +0800 Subject: [PATCH] Refactor Telegram notification handling and remove unused functions - Removed deprecated functions for masking sensitive data and previewing text, streamlining the Telegram notification process. - Simplified logging by eliminating unnecessary console outputs related to notification configuration and sending. - Updated the `TelegramNotifier` class to enhance clarity and maintainability, focusing on essential notification functionality. --- src/notifications/telegram.ts | 60 +++-------------------------- src/strategy/maker-points-engine.ts | 43 --------------------- 2 files changed, 5 insertions(+), 98 deletions(-) diff --git a/src/notifications/telegram.ts b/src/notifications/telegram.ts index c71b902..41aa54f 100644 --- a/src/notifications/telegram.ts +++ b/src/notifications/telegram.ts @@ -22,28 +22,6 @@ const TYPE_EMOJI: Record = { }; const LOG_PREFIX = "[Telegram]"; -const PREVIEW_LIMIT = 240; - -function maskSecret(value: string, revealStart = 4, revealEnd = 4): string { - if (!value) return "missing"; - if (value.length <= revealStart + revealEnd + 2) { - return `${value.slice(0, 1)}...${value.slice(-1)} (len=${value.length})`; - } - return `${value.slice(0, revealStart)}...${value.slice(-revealEnd)} (len=${value.length})`; -} - -function maskChatId(value: string): string { - if (!value) return "missing"; - if (value.length <= 4) return `***${value}`; - return `***${value.slice(-4)}`; -} - -function previewText(text: string): string { - if (text.length <= PREVIEW_LIMIT) { - return text; - } - return `${text.slice(0, PREVIEW_LIMIT)}...`; -} function formatNotificationMessage(notification: TradeNotification, accountLabel?: string): string { const levelEmoji = LEVEL_EMOJI[notification.level] ?? ""; @@ -79,7 +57,6 @@ export class TelegramNotifier implements NotificationSender { private readonly config: TelegramConfig; private readonly baseUrl: string; private sendQueue: Promise = Promise.resolve(); - private disabledLogged = false; constructor(config: Partial = {}) { this.config = { @@ -89,12 +66,6 @@ export class TelegramNotifier implements NotificationSender { accountLabel: config.accountLabel, }; this.baseUrl = `https://api.telegram.org/bot${this.config.botToken}`; - console.info(`${LOG_PREFIX} Notifier config`, { - enabled: this.config.enabled, - botToken: maskSecret(this.config.botToken), - chatId: maskChatId(this.config.chatId), - accountLabel: this.config.accountLabel ?? "none", - }); } isEnabled(): boolean { @@ -103,22 +74,9 @@ export class TelegramNotifier implements NotificationSender { async send(notification: TradeNotification): Promise { if (!this.isEnabled()) { - if (!this.disabledLogged) { - console.warn(`${LOG_PREFIX} Skipping send (missing bot token or chat id).`, { - botToken: maskSecret(this.config.botToken), - chatId: maskChatId(this.config.chatId), - }); - this.disabledLogged = true; - } return; } - console.info(`${LOG_PREFIX} Queueing notification`, { - type: notification.type, - level: notification.level, - title: notification.title, - chatId: maskChatId(this.config.chatId), - }); this.sendQueue = this.sendQueue .then(() => this.doSend(notification)) .catch(() => {}); @@ -129,11 +87,6 @@ export class TelegramNotifier implements NotificationSender { const url = `${this.baseUrl}/sendMessage`; try { - console.info(`${LOG_PREFIX} Sending notification`, { - chatId: maskChatId(this.config.chatId), - textLength: text.length, - textPreview: previewText(text), - }); const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, @@ -143,15 +96,12 @@ export class TelegramNotifier implements NotificationSender { }), }); - const responseText = await response.text().catch(() => "unknown response"); - console.info(`${LOG_PREFIX} Response`, { - ok: response.ok, - status: response.status, - body: responseText, - }); - if (!response.ok) { - console.error(`${LOG_PREFIX} Failed to send notification: ${response.status} ${responseText}`); + if (response.ok) { + console.info(`${LOG_PREFIX} Notification sent (status ${response.status}).`); + return; } + const errorText = await response.text().catch(() => "unknown error"); + console.error(`${LOG_PREFIX} Failed to send notification: ${response.status} ${errorText}`); } catch (error) { console.error(`${LOG_PREFIX} Failed to send notification: ${error instanceof Error ? error.message : String(error)}`); } diff --git a/src/strategy/maker-points-engine.ts b/src/strategy/maker-points-engine.ts index 0c07cee..ce39d2d 100644 --- a/src/strategy/maker-points-engine.ts +++ b/src/strategy/maker-points-engine.ts @@ -84,7 +84,6 @@ type MakerPointsListener = (snapshot: MakerPointsSnapshot) => void; const EPS = 1e-5; const INSUFFICIENT_BALANCE_COOLDOWN_MS = 15_000; const STOP_LOSS_COOLDOWN_MS = 10_000; -const TELEGRAM_LOG_PREFIX = "[Telegram]"; export class MakerPointsEngine { private accountSnapshot: AsterAccountSnapshot | null = null; @@ -103,7 +102,6 @@ export class MakerPointsEngine { private readonly rateLimit: RateLimitController; private readonly binanceDepth: BinanceDepthTracker; private readonly notifier: NotificationSender; - private readonly telegramDebugLog: boolean; private priceTick: number = 0.1; private qtyStep: number = 0.001; @@ -157,8 +155,6 @@ export class MakerPointsEngine { this.tradeLog.push(type, detail) ); this.notifier = createTelegramNotifier(); - this.telegramDebugLog = isTruthyEnv(process.env.TELEGRAM_DEBUG_LOG); - this.tradeLog.push("info", formatTelegramConfigLog(this.notifier.isEnabled())); this.priceTick = Math.max(1e-9, this.config.priceTick); this.qtyStep = Math.max(1e-9, this.config.qtyStep); this.binanceDepth = new BinanceDepthTracker(resolveBinanceSymbol(this.config.symbol), { @@ -807,9 +803,6 @@ export class MakerPointsEngine { } private notify(notification: TradeNotification): void { - if (this.telegramDebugLog) { - this.tradeLog.push("info", formatTelegramSendLog(notification, this.notifier.isEnabled())); - } this.notifier.send(notification); } @@ -1058,42 +1051,6 @@ export class MakerPointsEngine { } } -function isTruthyEnv(value: string | undefined): boolean { - if (!value) return false; - const normalized = value.trim().toLowerCase(); - return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on"; -} - -function maskSecret(value: string | undefined, revealStart = 4, revealEnd = 4): string { - if (!value) return "missing"; - const trimmed = value.trim(); - if (!trimmed) return "missing"; - if (trimmed.length <= revealStart + revealEnd + 2) { - return `${trimmed.slice(0, 1)}...${trimmed.slice(-1)}(len=${trimmed.length})`; - } - return `${trimmed.slice(0, revealStart)}...${trimmed.slice(-revealEnd)}(len=${trimmed.length})`; -} - -function maskChatId(value: string | undefined): string { - if (!value) return "missing"; - const trimmed = value.trim(); - if (!trimmed) return "missing"; - if (trimmed.length <= 4) return `***${trimmed}`; - return `***${trimmed.slice(-4)}`; -} - -function formatTelegramConfigLog(enabled: boolean): string { - const botToken = maskSecret(process.env.TELEGRAM_BOT_TOKEN); - const chatId = maskChatId(process.env.TELEGRAM_CHAT_ID); - const accountLabel = process.env.TELEGRAM_ACCOUNT_LABEL ?? "none"; - return `${TELEGRAM_LOG_PREFIX} 配置: ${enabled ? "启用" : "未启用"} botToken=${botToken} chatId=${chatId} label=${accountLabel}`; -} - -function formatTelegramSendLog(notification: TradeNotification, enabled: boolean): string { - const chatId = maskChatId(process.env.TELEGRAM_CHAT_ID); - return `${TELEGRAM_LOG_PREFIX} 发送尝试: enabled=${enabled} type=${notification.type} level=${notification.level} title=${notification.title} symbol=${notification.symbol} chatId=${chatId}`; -} - function resolveBinanceSymbol(symbol: string): string { const parts = parseSymbolParts(symbol); const base = (parts.base ?? symbol).replace(/[^a-zA-Z0-9]/g, "").toUpperCase();