3 Commits
Author SHA1 Message Date
discountry 445e634aa1 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.
2026-01-12 12:39:37 +08:00
discountry 4bb1fee995 Refactor Telegram notification handling in MakerPointsEngine
- Introduced a dedicated `notify` method to streamline notification sending and improve logging for Telegram notifications.
- Added a new environment variable check for enabling debug logging of Telegram notifications.
- Enhanced logging to include detailed information about notification attempts, including masked sensitive data for security.
- Updated various notification calls to utilize the new `notify` method, ensuring consistent logging and functionality.
2026-01-12 12:23:24 +08:00
discountry aad14395e0 Enhance Telegram notification functionality
- Introduced functions to mask sensitive information and preview notification text for improved logging and security.
- Added detailed logging for notification sending process, including configuration details and response handling.
- Implemented checks to prevent sending notifications when bot token or chat ID is missing, with appropriate warnings logged.
2026-01-12 12:12:14 +08:00
2 changed files with 19 additions and 11 deletions
+8 -4
View File
@@ -21,6 +21,8 @@ const TYPE_EMOJI: Record<string, string> = {
custom: "📢", custom: "📢",
}; };
const LOG_PREFIX = "[Telegram]";
function formatNotificationMessage(notification: TradeNotification, accountLabel?: string): string { function formatNotificationMessage(notification: TradeNotification, accountLabel?: string): string {
const levelEmoji = LEVEL_EMOJI[notification.level] ?? ""; const levelEmoji = LEVEL_EMOJI[notification.level] ?? "";
const typeEmoji = TYPE_EMOJI[notification.type] ?? ""; const typeEmoji = TYPE_EMOJI[notification.type] ?? "";
@@ -94,12 +96,14 @@ export class TelegramNotifier implements NotificationSender {
}), }),
}); });
if (!response.ok) { if (response.ok) {
const errorText = await response.text().catch(() => "unknown error"); console.info(`${LOG_PREFIX} Notification sent (status ${response.status}).`);
console.error(`[Telegram] Failed to send notification: ${response.status} ${errorText}`); return;
} }
const errorText = await response.text().catch(() => "unknown error");
console.error(`${LOG_PREFIX} Failed to send notification: ${response.status} ${errorText}`);
} catch (error) { } catch (error) {
console.error(`[Telegram] Failed to send notification: ${error instanceof Error ? error.message : String(error)}`); console.error(`${LOG_PREFIX} Failed to send notification: ${error instanceof Error ? error.message : String(error)}`);
} }
} }
} }
+11 -7
View File
@@ -645,7 +645,7 @@ export class MakerPointsEngine {
"stop", "stop",
`触发止损: 未实现亏损 ${position.unrealizedProfit.toFixed(4)} USDT` `触发止损: 未实现亏损 ${position.unrealizedProfit.toFixed(4)} USDT`
); );
this.notifier.send({ this.notify({
type: "stop_loss", type: "stop_loss",
level: "error", level: "error",
symbol: this.config.symbol, symbol: this.config.symbol,
@@ -802,6 +802,10 @@ export class MakerPointsEngine {
return getMidOrLast(this.depthSnapshot, this.tickerSnapshot); return getMidOrLast(this.depthSnapshot, this.tickerSnapshot);
} }
private notify(notification: TradeNotification): void {
this.notifier.send(notification);
}
private logReadinessBlockers(): void { private logReadinessBlockers(): void {
if (!this.feedStatus.account && !this.readinessLogged.account) { if (!this.feedStatus.account && !this.readinessLogged.account) {
this.tradeLog.push("info", t("log.maker.waitAccount")); this.tradeLog.push("info", t("log.maker.waitAccount"));
@@ -887,7 +891,7 @@ export class MakerPointsEngine {
const reference = this.getReferencePrice() ?? 0; const reference = this.getReferencePrice() ?? 0;
if (prevSide === "FLAT" && currentSide !== "FLAT") { if (prevSide === "FLAT" && currentSide !== "FLAT") {
this.notifier.send({ this.notify({
type: "position_opened", type: "position_opened",
level: "info", level: "info",
symbol: this.config.symbol, symbol: this.config.symbol,
@@ -902,7 +906,7 @@ export class MakerPointsEngine {
} else if (currentSide === "FLAT" && prevSide !== "FLAT") { } else if (currentSide === "FLAT" && prevSide !== "FLAT") {
const pnl = position.unrealizedProfit; const pnl = position.unrealizedProfit;
const closeType = this.tokenExpiredCloseOnlyMode ? "Token过期平仓" : "平仓"; const closeType = this.tokenExpiredCloseOnlyMode ? "Token过期平仓" : "平仓";
this.notifier.send({ this.notify({
type: "position_closed", type: "position_closed",
level: "success", level: "success",
symbol: this.config.symbol, symbol: this.config.symbol,
@@ -917,7 +921,7 @@ export class MakerPointsEngine {
} else if (currentSide === prevSide && absChange > EPS) { } else if (currentSide === prevSide && absChange > EPS) {
const isIncrease = Math.abs(currentAmt) > Math.abs(prevAmt); const isIncrease = Math.abs(currentAmt) > Math.abs(prevAmt);
if (isIncrease) { if (isIncrease) {
this.notifier.send({ this.notify({
type: "order_filled", type: "order_filled",
level: "info", level: "info",
symbol: this.config.symbol, symbol: this.config.symbol,
@@ -930,7 +934,7 @@ export class MakerPointsEngine {
}, },
}); });
} else { } else {
this.notifier.send({ this.notify({
type: "order_filled", type: "order_filled",
level: "info", level: "info",
symbol: this.config.symbol, symbol: this.config.symbol,
@@ -944,7 +948,7 @@ export class MakerPointsEngine {
}); });
} }
} else if (currentSide !== prevSide && currentSide !== "FLAT" && prevSide !== "FLAT") { } else if (currentSide !== prevSide && currentSide !== "FLAT" && prevSide !== "FLAT") {
this.notifier.send({ this.notify({
type: "position_opened", type: "position_opened",
level: "info", level: "info",
symbol: this.config.symbol, symbol: this.config.symbol,
@@ -995,7 +999,7 @@ export class MakerPointsEngine {
} }
if (!this.tokenExpiryNotified) { if (!this.tokenExpiryNotified) {
this.notifier.send({ this.notify({
type: "token_expired", type: "token_expired",
level: "warn", level: "warn",
symbol: this.config.symbol, symbol: this.config.symbol,