feat: add TELEGRAM_NOTIFICATION configuration switch

Add support for enabling/disabling Telegram notifications independently from
bot token presence. Configuration respects priority: environment variable >
settings.json > default (enabled for backward compatibility).
This commit is contained in:
weishu
2026-01-07 17:04:10 +08:00
parent 0f29ee182c
commit dfc5359aed
4 changed files with 31 additions and 1 deletions
+18
View File
@@ -12,6 +12,7 @@ import { getSettingsFile, readSettings, writeSettings } from './settings'
export interface ServerSettings {
telegramBotToken: string | null
telegramNotification: boolean
webappHost: string
webappPort: number
webappUrl: string
@@ -22,6 +23,7 @@ export interface ServerSettingsResult {
settings: ServerSettings
sources: {
telegramBotToken: 'env' | 'file' | 'default'
telegramNotification: 'env' | 'file' | 'default'
webappHost: 'env' | 'file' | 'default'
webappPort: 'env' | 'file' | 'default'
webappUrl: 'env' | 'file' | 'default'
@@ -84,6 +86,7 @@ export async function loadServerSettings(dataDir: string): Promise<ServerSetting
let needsSave = false
const sources: ServerSettingsResult['sources'] = {
telegramBotToken: 'default',
telegramNotification: 'default',
webappHost: 'default',
webappPort: 'default',
webappUrl: 'default',
@@ -104,6 +107,20 @@ export async function loadServerSettings(dataDir: string): Promise<ServerSetting
sources.telegramBotToken = 'file'
}
// telegramNotification: env > file > true (default enabled for backward compatibility)
let telegramNotification = true
if (process.env.TELEGRAM_NOTIFICATION !== undefined) {
telegramNotification = process.env.TELEGRAM_NOTIFICATION === 'true'
sources.telegramNotification = 'env'
if (settings.telegramNotification === undefined) {
settings.telegramNotification = telegramNotification
needsSave = true
}
} else if (settings.telegramNotification !== undefined) {
telegramNotification = settings.telegramNotification
sources.telegramNotification = 'file'
}
// webappHost: env > file > 127.0.0.1
let webappHost = '127.0.0.1'
if (process.env.WEBAPP_HOST) {
@@ -174,6 +191,7 @@ export async function loadServerSettings(dataDir: string): Promise<ServerSetting
return {
settings: {
telegramBotToken,
telegramNotification,
webappHost,
webappPort,
webappUrl,
+1
View File
@@ -13,6 +13,7 @@ export interface Settings {
}
// Server configuration (persisted from environment variables)
telegramBotToken?: string
telegramNotification?: boolean
webappHost?: string
webappPort?: number
webappUrl?: string
+6
View File
@@ -8,6 +8,7 @@
* Optional environment variables:
* - CLI_API_TOKEN: Shared secret for hapi CLI authentication (auto-generated if not set)
* - TELEGRAM_BOT_TOKEN: Telegram Bot API token from @BotFather
* - TELEGRAM_NOTIFICATION: Enable/disable Telegram notifications (default: true)
* - WEBAPP_PORT: Port for Mini App HTTP server (default: 3006)
* - WEBAPP_URL: Public URL for Telegram Mini App
* - CORS_ORIGINS: Comma-separated CORS origins
@@ -27,6 +28,7 @@ export type ConfigSource = 'env' | 'file' | 'default'
export interface ConfigSources {
telegramBotToken: ConfigSource
telegramNotification: ConfigSource
webappHost: ConfigSource
webappPort: ConfigSource
webappUrl: ConfigSource
@@ -41,6 +43,9 @@ class Configuration {
/** Telegram bot enabled status (token present) */
public readonly telegramEnabled: boolean
/** Telegram notifications enabled */
public readonly telegramNotification: boolean
/** CLI auth token (shared secret) */
public cliApiToken: string
@@ -88,6 +93,7 @@ class Configuration {
// Apply server settings
this.telegramBotToken = serverSettings.telegramBotToken
this.telegramEnabled = Boolean(this.telegramBotToken)
this.telegramNotification = serverSettings.telegramNotification
this.webappHost = serverSettings.webappHost
this.webappPort = serverSettings.webappPort
this.miniAppUrl = serverSettings.webappUrl
+6 -1
View File
@@ -77,6 +77,8 @@ async function main() {
} else {
const tokenSource = formatSource(config.sources.telegramBotToken)
console.log(`[Server] Telegram: enabled (${tokenSource})`)
const notificationSource = formatSource(config.sources.telegramNotification)
console.log(`[Server] Telegram notifications: ${config.telegramNotification ? 'enabled' : 'disabled'} (${notificationSource})`)
}
const store = new Store(config.dbPath)
@@ -111,7 +113,10 @@ async function main() {
miniAppUrl: config.miniAppUrl,
store
})
notificationChannels.push(happyBot)
// Only add to notification channels if notifications are enabled
if (config.telegramNotification) {
notificationChannels.push(happyBot)
}
}
notificationHub = new NotificationHub(syncEngine, notificationChannels)